package com.dxhy.base.controller;

import java.util.*;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.base.service.invoicecheck.BaseInvoiceManualCheckService;
import com.dxhy.base.service.sqdktj.BaseCancelApplicationService;
import com.dxhy.base.service.sqdktj.BaseDktjService;
import com.dxhy.base.service.sqdktj.BaseStatisticalEnterpriseService;
import com.dxhy.base.service.sqdktj.BaseTDxDkCountLogService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.enums.OperaTypeEnum;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;
import com.dxhy.common.vo.Tax;
import com.dxhy.common.vo.TaxOrgCode;

import lombok.extern.slf4j.Slf4j;

/**
 * 抵扣统计相关入口
 *
 * @author kangzq
 * @date 2019年5月11日
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaLowerCamelCaseVariableNaming"})
@RestController
@Slf4j
public class BaseDktjController extends AbstractController {

    /**
     * 统计明细service
     */
    @Resource

    private BaseDktjService baseDktjService;
    /**
     * 企业信息service
     */
    @Resource
    private BaseStatisticalEnterpriseService baseStatisticalEnterpriseService;

    /**
     * 撤销统计色service
     */
    @Resource
    private BaseCancelApplicationService baseCancelApplicationService;

    /**
     * 添加统计表操作记录
     */
    @Resource
    private BaseTDxDkCountLogService baseTDxDkCountLogService;

    @Resource
    private BaseInvoiceManualCheckService baseInvoiceManualCheckService;

    /**
     * 抵扣统计明细查询入口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/get/dktjcx")
    @SysLog("调用抵扣统计明细查询入口接口")
    public Object listDktj(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String skssq = (String)map.get("skssq");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("taxList", getUserInfo().getOrg());
        if (!StringHelper.isBlank(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方信息不能为空！"));
        }
        if (!StringHelper.isBlank(skssq)) {
            pramsMap.put("skssq", skssq);
        } else {
            return ResponseEntity.ok(R.error("税款所属期不能为空！"));
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", baseDktjService.selectByGfsbhAndSkssq(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 抵扣统计和撤销统计查询入口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/get/sqtjqyxxcx")
    @SysLog("调用抵扣统计和撤销统计查询入口接口")
    public Object listStatisticalEnterprise(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String skssq = (String)map.get("skssq");
        String tjzt = (String)map.get("tjzt");
        String qrtjzt = (String)map.get("qrtjzt");
        String tjflag = (String)map.get("tjflag");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        int count = 0;
        if (StringUtils.isBlank(tjflag)) {
            tjflag = "0";
        }

        Map<String, Object> pramsMap = new HashMap<>(16);
        List<TaxOrgCode> orgCodeList = getUserInfo().getOrgCodes();
        pramsMap.put("orgCodeList", orgCodeList);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("tjflag", tjflag);
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
        List<String> gfshList = new ArrayList<>();
        List<Tax> taxList = getUserInfo().getOrg();
        List<Tax> newTaxList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            for (Tax tax : taxList) {
                if (gfsh.equals(tax.getTaxno())) {
                    String upgradeStatus = tax.getUpgradeStatus();
                    if ("1".equals(upgradeStatus)) {
                        gfshList.add(gfsh);
                    }
                }
            }
        } else {
            for (Tax tax : taxList) {
                String upgradeStatus = tax.getUpgradeStatus();
                if ("1".equals(upgradeStatus)) {
                    newTaxList.add(tax);
                }
            }
            gfshList = UserInfoUtil.getGfshAll(newTaxList);
        }
        pramsMap.put("gfsh", gfshList);

        if ("1".equals(tjflag)) {
            pramsMap.put("tjzt", "2");
        }
        if (!StringUtils.isBlank(skssq)) {
            pramsMap.put("skssq", skssq);
        }

        if (!StringUtils.isBlank(tjzt)) {
            if (!"99".equals(tjzt)) {
                pramsMap.put("tjzt", tjzt);
            }
        } else {
            return ResponseEntity.ok(R.error("统计状态不能为空！"));
        }

        if (!StringUtils.isBlank(qrtjzt)) {
            if (!"99".equals(qrtjzt)) {
                pramsMap.put("qrtjzt", qrtjzt);
            }
        } else {
            return ResponseEntity.ok(R.error("确认统计状态不能为空！"));
        }

        try {
            return ResponseEntity.ok(R.ok().put("data",
                baseStatisticalEnterpriseService.selectEnterpriseByGfsbhAndSkssq(pramsMap, curr, size, count)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     *
     * 申请抵扣统计入口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/get/sqdktj")
    @SysLog("调用申请抵扣统计入口接口")
    public Object applicationEnterprise(@RequestBody Map<String, Object> map) {
        String gfshs = (String)map.get("gfshs");
        String skssqs = (String)map.get("skssqs");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());
        String czr = getUserName();
        pramsMap.put("czr", czr);
        pramsMap.put("czlx", OperaTypeEnum.SQTJ.getOperaTypeDm());

        if (!StringHelper.isBlank(gfshs) && !"99".equals(gfshs)) {
            pramsMap.put("gfshs", gfshs);
        } else {
            return ResponseEntity.ok(R.error("购方信息不能为空！"));
        }
        if (!StringHelper.isBlank(skssqs)) {
            pramsMap.put("skssqs", skssqs);
        } else {
            return ResponseEntity.ok(R.error("税款所属期不能为空！"));
        }

        try {
            JSONObject json = baseInvoiceManualCheckService.beforetj(pramsMap);
            boolean flag = (boolean)json.get("flag");
            if (!flag) {
                return ResponseEntity.ok(R.ok().put("data", json.get("rejson").toString()));
            } else {
                baseTDxDkCountLogService.insertBatch(pramsMap);
                return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", baseStatisticalEnterpriseService.updateBySqtj(pramsMap))));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     *
     * 确认抵扣统计入口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/get/qrdktj")
    @SysLog("调用确认抵扣统计入口接口")
    public Object qrtj(@RequestBody Map<String, Object> map) {
        String gfshs = (String)map.get("gfshs");
        String skssqs = (String)map.get("skssqs");
        String password = (String)map.get("password");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("password", password);
        String czr = getUserName();
        pramsMap.put("czr", czr);
        pramsMap.put("czlx", OperaTypeEnum.QRTJ.getOperaTypeDm());

        if (!StringHelper.isBlank(gfshs) && !"99".equals(gfshs)) {
            pramsMap.put("gfshs", gfshs);
        } else {
            return ResponseEntity.ok(R.error("购方信息不能为空！"));
        }
        if (!StringHelper.isBlank(skssqs)) {
            pramsMap.put("skssqs", skssqs);
        } else {
            return ResponseEntity.ok(R.error("税款所属期不能为空！"));
        }
//        if (StringUtils.isNotBlank(password)) {
//            if (gfshs.contains(",")) {
//                return ResponseEntity.ok(R.error("需要填报密码时不可多选！"));
//            }
//        }
        try {
            JSONObject json = baseInvoiceManualCheckService.beforeqr(pramsMap);
            boolean flag = (boolean)json.get("flag");
            if (flag) {
                return ResponseEntity.ok(R.ok().put("data", json.get("rejson").toString()));
            } else {
                baseTDxDkCountLogService.insertBatch(pramsMap);
                return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", baseStatisticalEnterpriseService.updateByQrsqtj(pramsMap))));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 撤销统计入口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/post/cxdktj")
    @SysLog("调用撤销统计入口接口")
    public Object cancelApplicationEnterprise(@RequestBody Map<String, Object> map) {
        String gfshs = (String)map.get("gfshs");
        String skssqs = (String)map.get("skssqs");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());

        String czr = getUserName();
        pramsMap.put("czr", czr);
        pramsMap.put("czlx", OperaTypeEnum.CXTJ.getOperaTypeDm());
        if (!StringHelper.isBlank(gfshs) && !"99".equals(gfshs)) {
            pramsMap.put("gfshs", gfshs);
        } else {
            return ResponseEntity.ok(R.error("购方信息不能为空！"));
        }
        if (!StringHelper.isBlank(skssqs)) {
            pramsMap.put("skssqs", skssqs);
        } else {
            return ResponseEntity.ok(R.error("税款所属期不能为空！"));
        }

        try {
            baseTDxDkCountLogService.insertBatch(pramsMap);
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", baseCancelApplicationService.deleteByGfshAndSkssq(pramsMap))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 撤销确认入口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/post/cxdkqs")
    @SysLog("调用撤销确认入口接口")
    public Object cxdkqs(@RequestBody Map<String, Object> map) {
        String gfshs = (String)map.get("gfshs");
        String skssqs = (String)map.get("skssqs");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());

        String czr = getUserName();
        pramsMap.put("czr", czr);
        pramsMap.put("czlx", "3");
        if (!StringHelper.isBlank(gfshs) && !"99".equals(gfshs)) {
            pramsMap.put("gfshs", gfshs);
        } else {
            return ResponseEntity.ok(R.error("购方信息不能为空！"));
        }
        if (!StringHelper.isBlank(skssqs)) {
            pramsMap.put("skssqs", skssqs);
        } else {
            return ResponseEntity.ok(R.error("税款所属期不能为空！"));
        }

        try {
            baseTDxDkCountLogService.insertBatch(pramsMap);
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", baseCancelApplicationService.deleteQsByGfshAndSkssq(pramsMap))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
