package com.dxhy.base.service.sqdktj;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.base.dao.BaseTDxDkCountDao;
import com.dxhy.base.entity.BaseTDxDkCount;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.ApplyStatusEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;

/**
 * 撤销抵扣统计service实现类
 *
 * @author kangzq
 * @date 2019年5月11日
 * @version 1.0
 */
@SuppressWarnings("AlibabaCollectionInitShouldAssignCapacity")
@Service
public class BaseCancelApplicationServiceImpl extends MpBaseServiceImpl<BaseTDxDkCountDao, BaseTDxDkCount>
    implements BaseCancelApplicationService {

    @Resource
    private BaseDktjService baseDktjService;

    @Resource
    private BaseStatisticalEnterpriseService baseStatisticalEnterpriseService;

    @Override
    public String deleteByGfshAndSkssq(Map<String, Object> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        String[] gfshs = pramsMap.get("gfshs").toString().split(",");
        String[] skssqs = pramsMap.get("skssqs").toString().split(",");

        JSONObject json;
        json = baseDktjService.selectCxtjHjxx(pramsMap);

        for (int i = 0; i < gfshs.length; i++) {

            Map<String, Object> pramMap = new HashMap<>();
            pramMap.put("taxno", gfshs[i]);
            pramMap.put("tj_month", skssqs[i]);
            // 删除抵扣统计明细

            Map<String, Object> updatepramMap = new HashMap<>(16);
            updatepramMap.put("gfsh", pramMap.get("taxno"));
            updatepramMap.put("skssq", pramMap.get("tj_month"));
            updatepramMap.put("dbName", pramsMap.get("dbName"));

            // 更新相关的数据
            BaseTDxDkCount delentity = new BaseTDxDkCount();
            delentity.setApplyStatus(ApplyStatusEnum.WSQ.getApplyStatusDm());
            delentity.setApplyType("1");
            delentity.setCxtjStatus("1");
            delentity.setApplyStatus("0");
            delentity.setCxtjUser((String)pramsMap.get("czr"));
            baseStatisticalEnterpriseService.updateByGfshAndSkssq(updatepramMap, delentity);
        }
        json.put("tsxx", "已申请撤销统计，请稍后查询撤销结果");
        return super.responseResult(json);
    }

    @Override
    public String deleteQsByGfshAndSkssq(Map<String, Object> pramsMap) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        String[] gfshs = pramsMap.get("gfshs").toString().split(",");
        String[] skssqs = pramsMap.get("skssqs").toString().split(",");

        JSONObject json;
        json = baseDktjService.selectCxtjHjxx(pramsMap);

        for (int i = 0; i < gfshs.length; i++) {

            Map<String, Object> pramMap = new HashMap<>();
            pramMap.put("taxno", gfshs[i]);
            pramMap.put("tj_month", skssqs[i]);
            // 删除抵扣统计明细

            Map<String, Object> updatepramMap = new HashMap<>(16);
            updatepramMap.put("gfsh", pramMap.get("taxno"));
            updatepramMap.put("skssq", pramMap.get("tj_month"));
            updatepramMap.put("dbName", pramsMap.get("dbName"));

            // 更新相关的数据
            BaseTDxDkCount delentity = new BaseTDxDkCount();
            delentity.setApplyStatus(ApplyStatusEnum.WSQ.getApplyStatusDm());
            delentity.setApplyType("1");
            delentity.setCxqsStatus("1");
            delentity.setApplyStatus("0");
            delentity.setCxqrUser((String)pramsMap.get("czr"));
            baseStatisticalEnterpriseService.updateByGfshAndSkssq(updatepramMap, delentity);
        }
        json.put("tsxx", "已申请撤销确认，请稍后查询撤销结果");
        return super.responseResult(json);
    }
}
