package com.dxhy.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.config.Tax;
import com.dxhy.base.model.BaseDrgxReqVO;
import com.dxhy.base.service.fpxxrz.BaseCxrzfpService;
import com.dxhy.base.service.fpxxrz.BaseRzFpxxService;
import com.dxhy.base.service.invoicecheck.*;
import com.dxhy.base.service.tdxrecordinvoice.BaseIRzTDxRecordInvoiceService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author kangzq
 * @date 2019年4月24日
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong", "AlibabaLowerCamelCaseVariableNaming"})
@RestController
@Slf4j
public class BaseInvoiceController extends AbstractController {
    /**
     * 认证发票查询service
     *
     */
    @Resource
    private BaseRzFpxxService baseRzFpxxService;

    /**
     * 扫描勾选查询service
     */
    @Resource
    private BaseInvoiceScanCheckService baseInvoiceScanCheckService;

    /**
     * 勾选service
     */
    @Resource
    private BaseInvoiceCheckService baseInvoiceCheckService;

    /**
     * 手工查询service
     */
    @Resource
    private BaseInvoiceManualCheckService baseInvoiceManualCheckService;

    /**
     * 导入查询service
     */
    @Resource
    private BaseInvoiceImportCheckService baseInvoiceImportCheckService;

    /**
     * 已勾选发票查询service
     */
    @Resource
    private BaseYgxfpqrService baseYgxfpqrService;
    /**
     * 撤销已认证发票
     */
    @Resource
    private BaseCxrzfpService baseCxrzfpService;

    /**
     * 已勾选发票撤销勾选service
     */
    @Resource
    private BaseCxygxfpService baseCxygxfpService;

    /**
     * 发票认证service
     */
    @Resource
    private BaseIRzTDxRecordInvoiceService baseIRzTDxRecordInvoiceService;
    @Resource
    private BaseIntelligentizeCheckService baseIntelligentizeCheckService;

    private static final Pattern PATTERN_DATE = Pattern.compile(
        "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$");
    private static final Pattern PATTERN_AMOUNT = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");

    /**
     * 发票认证综合查询入口
     *
     * @return 返回结果
     */
    @PostMapping("/get/rzhzlb")
    @ResponseBody
    @SysLog("调用发票认证综合查询入口接口")
    public Object listRzFpxx(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String invoiceType = (String)map.get("invoiceType");
        String invoiceSource = (String)map.get("invoiceSource");
        String kpksrq = (String)map.get("kpksrq");
        String kpjsrq = (String)map.get("kpjsrq");
        String qszt = (String)map.get("qszt");
        String rzzt = (String)map.get("rzzt");
        String rzclzt = (String)map.get("rzclzt");
        String rzlx = (String)map.get("rzlx");
        String skssq = (String)map.get("skssq");
        String qsfs = (String)map.get("qsfs");
        String invoiceStatus = (String)map.get("invoiceStatus");
        String qsksrq = (String)map.get("qsksrq");
        String qsjsrq = (String)map.get("qsjsrq");
        String rzksrq = (String)map.get("rzksrq");
        String rzjsrq = (String)map.get("rzjsrq");
        String invoiceNo = (String)map.get("invoiceNo");
        String xfmc = (String)map.get("xfmc");
        String businessCode = (String)map.get("businessCode");
        String qsr = (String)map.get("qsr");
        String rzhr = (String)map.get("rzhr");
        String inAccountStatus = (String)map.get("inAccountStatus");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        Map<String, Object> pramsMap = new HashMap<>(25);
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
        pramsMap.put("xfmc", xfmc);
        pramsMap.put("qsr", qsr);
        if (StringUtils.isNotBlank(rzhr)) {
            pramsMap.put("rzhr", rzhr);
            /*String[] split = rzhr.split(",");
            if (!Arrays.asList(split).contains("")) {
                String[] split1 = rzhr.split(",");
                pramsMap.put("rzhr", Arrays.asList(split1));
            }*/
        }

        if (!StringUtils.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                pramsMap.put("invoiceNo", invoiceNo);
            }else {
                pramsMap.put("eleInvoiceNo", invoiceNo);
            }
        }
        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        pramsMap.put("gfsh", gfshList);
        if (!StringHelper.isBlank(invoiceType)) {
            if (!"99".equals(invoiceType)) {
                pramsMap.put("invoiceType", invoiceType);
            }
        } else {
            return ResponseEntity.ok(R.error("发票类型不能为空！"));
        }
        if (invoiceSource != null) {
            if (!"99".equals(invoiceSource)) {
                pramsMap.put("invoiceSource", invoiceSource);
            }
        }
        if (StringUtils.isNotBlank(rzlx)) {
            if (!"99".equals(rzlx)) {
                pramsMap.put("rzlx", rzlx);
            } else {
                pramsMap.put("rzlx", "");
            }
        }
        pramsMap.put("businessCode", "99");
        if (!StringHelper.isBlank(qszt)) {
            // 已签收校验签收方式和签收起止日期
            if (!"99".equals(qszt)) {
                pramsMap.put("qszt", qszt);
                if ("1".equals(qszt)) {
                    if (!StringHelper.isBlank(qsfs)) {
                        if (!"99".equals(qsfs)) {
                            pramsMap.put("qsfs", qsfs);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("签收方式不能为空！"));
                    }
                    if (!StringHelper.isBlank(qsksrq)) {
                        pramsMap.put("qsksrq", qsksrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收开始日期不能为空！"));
                    }
                    if (!StringHelper.isBlank(qsjsrq)) {
                        pramsMap.put("qsjsrq", qsjsrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收结束日期不能为空！"));
                    }
                    pramsMap.put("businessCode", businessCode);
                }

            }
        } else {
            return ResponseEntity.ok(R.error("签收状态不能为空！"));
        }
        if (!StringHelper.isBlank(rzzt)) {
            // 已认证传 认证起止日期和税款所属期做判断
            if (!"99".equals(rzzt)) {
                pramsMap.put("rzzt", rzzt);
                if (!"0".equals(rzzt)) {
                    if (!StringHelper.isBlank(rzksrq)) {
                        pramsMap.put("rzksrq", rzksrq);
                    } else {
                        return ResponseEntity.ok(R.error("认证开始日期不能为空！"));
                    }
                    if (!StringHelper.isBlank(rzjsrq)) {
                        pramsMap.put("rzjsrq", rzjsrq);
                    } else {
                        return ResponseEntity.ok(R.error("认证结束日期不能为空！"));
                    }
                    if (!StringHelper.isBlank(skssq)) {
                        pramsMap.put("skssq", skssq);
                    }
                }
            }
        } else {
            return ResponseEntity.ok(R.error("认证状态不能为空！"));
        }
        if (!StringHelper.isBlank(rzclzt)) {
            if (!"99".equals(rzclzt)) {
                pramsMap.put("rzclzt", rzclzt);
            } else {
                pramsMap.put("rzclzt", "");
            }
        } else {
            return ResponseEntity.ok(R.error("认证状态不能为空！"));
        }
        if (!StringHelper.isBlank(invoiceStatus)) {
            if (!"99".equals(invoiceStatus)) {
                pramsMap.put("invoiceStatus", invoiceStatus);
            } else {
                pramsMap.put("invoiceStatus", "");
            }
        }

        if (!StringHelper.isBlank(kpksrq)) {
            pramsMap.put("kpksrq", kpksrq);
        } else {
            return ResponseEntity.ok(R.error("开票开始日期不能为空！"));
        }
        if (!StringHelper.isBlank(kpjsrq)) {
            pramsMap.put("kpjsrq", kpjsrq);
        } else {
            return ResponseEntity.ok(R.error("开票结束日期不能为空！"));
        }
        if (StringUtils.isNotBlank(inAccountStatus)) {
            pramsMap.put("inAccountStatus", inAccountStatus);
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseRzFpxxService.selectByPramsMap(pramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 撤销认证
     *
     * @param map
     *            请求参数 id逗号分隔
     * @return 返回结果
     */
    @PostMapping("/rzzhcx/cxrz")
    @SysLog("调用撤销认证接口")
    public Object cxrz(@RequestBody Map<String, Object> map) {
        String cxids = (String)map.get("cxids");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("name", getUserName());
        if (!StringHelper.isBlank(cxids)) {
            pramsMap.put("cxids", cxids);
        } else {
            return ResponseEntity.ok(R.error("请选择需要撤销认证的数据！"));
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", baseCxrzfpService.updateByIds(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     *
     * 扫码勾选录入查询
     */
    @PostMapping("/smgx/smgxcx")
    @SysLog("调用扫码勾选录入校验接口")
    public Object invoiceScanCheck(@RequestBody Map<String, Object> map) {
        String invoiceCode = (String) map.get("invoiceCode");
        String invoiceNo = (String) map.get("invoiceNo");
        String invoiceAmount = (String) map.get("invoiceAmount");
        String invoiceDate = (String) map.get("invoiceDate");
        String checkCode = (String) map.get("checkCode");

        List<String> gfshList;
        Map<String, Object> pramsMap = new HashMap<>(16);
        gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("gfsh", gfshList);
        List<Tax> taxList = getUserInfo().getOrg();
        pramsMap.put("taxList", taxList);
        String qsflag = getUserInfo().getQsStatus();
        pramsMap.put("qsflag", qsflag);
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
        pramsMap.put("invoiceCode", invoiceCode);
        pramsMap.put("invoiceNo", invoiceNo);
        pramsMap.put("invoiceType", (String) map.get("invoiceType"));
        if (!StringUtils.isBlank(checkCode)) {
            pramsMap.put("checkCode", checkCode);
        }
        if (!StringUtils.isBlank(invoiceAmount) && !"0".equals(invoiceAmount)) {
            // 判断小数点后2位的数字的正则表达式
            Matcher match = PATTERN_AMOUNT.matcher(invoiceAmount);
            if (match.matches()) {
                pramsMap.put("invoiceAmount", invoiceAmount);
            } else {
                return ResponseEntity.ok(R.error("金额格式错误！"));
            }
        }
        if (!StringUtils.isBlank(invoiceDate)) {
            // 判断日期 yyyy-mm-dd 的正则表达式（能够区分真实日期，区分闰年）
            Matcher match = PATTERN_DATE.matcher(invoiceDate);
            if (match.matches()) {
                pramsMap.put("invoiceDate", invoiceDate);
            } else {
                return ResponseEntity.ok(R.error(" 开票日期格式错误！"));
            }
        } else {
            return ResponseEntity.ok(R.error("开票日期不能为空！"));
        }

        try {
            return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", baseInvoiceScanCheckService.selectByInvoiceScanCheck(pramsMap))));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 手工勾选查询 qsfs 默认传9
     *
     * @return 返回结果
     */
    @PostMapping("/sggx/sggxcx")
    @SysLog("调用手工勾选查询接口")
    @ResponseBody
    public Object sggxcx(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String invoiceType = (String)map.get("invoiceType");
        String invoiceSource = (String)map.get("invoiceSource");
        String kpksrq = (String)map.get("kpksrq");
        String kpjsrq = (String)map.get("kpjsrq");
        String qszt = (String)map.get("qszt");
        String qsfs = (String)map.get("qsfs");
        String businessCode = (String)map.get("businessCode");
        String invoiceNo = (String)map.get("invoiceNo");
        String qsksrq = (String)map.get("qsksrq");
        String qsjsrq = (String)map.get("qsjsrq");
        String xfmc = (String)map.get("xfmc");
        String position = (String)map.get("position");
        String fpzt = (String)map.get("invoiceStatus");
        String sfkrz = (String)map.get("sfkrz");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        String qsr = (String)map.get("qsr");
        String company = (String)map.get("company");
        String menuId = (String)map.get("menuId");

        Map<String, Object> pramsMap = new HashMap<>(18);
        pramsMap.put("company", company);
        pramsMap.put("menuId", menuId);
        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<Tax> taxList = getUserInfo().getOrg();
        pramsMap.put("taxList", taxList);
        pramsMap.put("qsflag", getUserInfo().getQsStatus());
        pramsMap.put("payment", map.get("payment"));
        pramsMap.put("paymentStatus", map.get("paymentStatus"));
        pramsMap.put("paymentDateStart", map.get("paymentDateStart"));
        pramsMap.put("paymentDateEnd", map.get("paymentDateEnd"));
        pramsMap.put("settlementNo", map.get("settlementNo"));
        pramsMap.put("voucherNumber", map.get("voucherNumber"));
        String inAccountStatus = (String)map.get("inAccountStatus");

        if (sfkrz != null) {
            if (!"99".equals(sfkrz)) {
                pramsMap.put("sfkrz", sfkrz);
            }
        }
        if (invoiceSource != null) {
            if (!"99".equals(invoiceSource)) {
                pramsMap.put("invoiceSource", invoiceSource);
            }
        }
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        pramsMap.put("gfsh", gfshList);

        if (!StringUtils.isBlank(invoiceType)) {
            if (!"99".equals(invoiceType)) {
                pramsMap.put("invoiceType", invoiceType);
            }
        } else {
            return ResponseEntity.ok(R.error("发票类型不能为空！"));
        }
        if (StringUtils.isNotBlank(fpzt)) {
            pramsMap.put("fpzt", fpzt);
        } else {
            return ResponseEntity.ok(R.error("发票状态不能为空"));
        }
        pramsMap.put("businessCode", "99");
        if (!StringUtils.isBlank(qszt)) {
            if (!"99".equals(qszt)) {
                pramsMap.put("qszt", qszt);
                if ("1".equals(qszt)) {
                    if (!StringUtils.isBlank(qsksrq)) {
                        pramsMap.put("qsksrq", qsksrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收开始日期不能为空！"));
                    }
                    if (!StringUtils.isBlank(qsjsrq)) {
                        pramsMap.put("qsjsrq", qsjsrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收结束日期不能为空！"));
                    }
                    if (!StringUtils.isBlank(qsfs)) {
                        // 表示全部
                        if (!"99".equals(qsfs)) {
                            pramsMap.put("qsfs", qsfs);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("签收方式不能为空！"));
                    }
                    pramsMap.put("businessCode", businessCode);
                }
            }
        } else {
            return ResponseEntity.ok(R.error("签收状态不能为空！"));
        }
        String qsflag = getUserInfo().getQsStatus();
        if ("1".equals(qsflag)) {
            pramsMap.put("qszt", "1");
        }
        if (!StringUtils.isBlank(kpksrq)) {
            pramsMap.put("kpksrq", kpksrq);
        } else {
            return ResponseEntity.ok(R.error("开票开始日期不能为空！"));
        }
        if (!StringUtils.isBlank(kpjsrq)) {
            pramsMap.put("kpjsrq", kpjsrq);
        } else {
            return ResponseEntity.ok(R.error("开票结束日期不能为空！"));
        }

        if (!StringUtils.isBlank(position)) {
            pramsMap.put("username", getUserName());
            pramsMap.put("position", position);
        }

        if (!StringUtils.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                pramsMap.put("invoiceNo", invoiceNo);
            }else {
                pramsMap.put("eleInvoiceNo", invoiceNo);
            }
        }
        if (!StringUtils.isBlank(xfmc)) {
            pramsMap.put("xfmc", xfmc);
        }
        if (!StringUtils.isBlank(qsr)) {
            pramsMap.put("qsr", qsr);
        }
        if (StringUtils.isNotBlank(inAccountStatus)) {
            pramsMap.put("inAccountStatus", inAccountStatus);
        }
        try {
            return ResponseEntity
                .ok(R.ok().put("data", baseInvoiceManualCheckService.selectByManualCheck(pramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 智能勾选查询 qsfs 默认传9
     *
     * @return 返回结果
     */
    @PostMapping("/sggx/zngxcx")
    @SysLog("调用智能勾选查询接口")
    @ResponseBody
    public Object zngxcx(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String invoiceType = (String)map.get("invoiceType");
        String invoiceSource = (String)map.get("invoiceSource");
        String kpksrq = (String)map.get("kpksrq");
        String kpjsrq = (String)map.get("kpjsrq");
        String qszt = (String)map.get("qszt");
        String qsfs = (String)map.get("qsfs");
        String businessCode = (String)map.get("businessCode");
        String qsksrq = (String)map.get("qsksrq");
        String qsjsrq = (String)map.get("qsjsrq");
        String xfmc = (String)map.get("xfmc");
        String maxTax = (String)map.get("maxTax");
        String floatTax = (String)map.get("floatTax");
        String yxsxfs = (String)map.get("yxsxfs");
        String qsr = (String)map.get("qsr");
        String invoiceNo = (String)map.get("invoiceNo");
        Map<String, Object> pramsMap = new HashMap<>(18);

        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<Tax> taxList = getUserInfo().getOrg();
        pramsMap.put("taxList", taxList);
        pramsMap.put("qsflag", getUserInfo().getQsStatus());
        if (StringUtils.isNotBlank(maxTax)) {
            pramsMap.put("maxTax", maxTax);
        } else {
            return ResponseEntity.ok(R.error("税额上限不能为空！"));
        }
        if (StringUtils.isNotBlank(floatTax)) {
            pramsMap.put("floatTax", floatTax);
        } else {
            return ResponseEntity.ok(R.error("税额容差不能为空！"));
        }
        if (StringUtils.isNotBlank(yxsxfs)) {
            pramsMap.put("yxsxfs", yxsxfs);
        } else {
            return ResponseEntity.ok(R.error("优先筛选方式不能为空！"));
        }
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }
        if (invoiceSource != null) {
            if (!"99".equals(invoiceSource)) {
                pramsMap.put("invoiceSource", invoiceSource);
            }
        }
        if (!StringUtils.isBlank(invoiceType)) {
            if (!"99".equals(invoiceType)) {
                pramsMap.put("invoiceType", invoiceType);
            }
        } else {
            return ResponseEntity.ok(R.error("发票类型不能为空！"));
        }
        pramsMap.put("businessCode", "99");
        if (!StringUtils.isBlank(qszt)) {
            if (!"99".equals(qszt)) {
                pramsMap.put("qszt", qszt);
                if ("1".equals(qszt)) {
                    if (!StringUtils.isBlank(qsksrq)) {
                        pramsMap.put("qsksrq", qsksrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收开始日期不能为空！"));
                    }
                    if (!StringUtils.isBlank(qsjsrq)) {
                        pramsMap.put("qsjsrq", qsjsrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收结束日期不能为空！"));
                    }
                    if (!StringUtils.isBlank(qsr)) {
                        pramsMap.put("qsr", qsr);
                    }
                    if (!StringUtils.isBlank(qsfs)) {
                        // 表示全部
                        if (!"99".equals(qsfs)) {
                            pramsMap.put("qsfs", qsfs);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("签收方式不能为空！"));
                    }
                    pramsMap.put("businessCode", businessCode);
                }
            }
        } else {
            return ResponseEntity.ok(R.error("签收状态不能为空！"));
        }
        String qsflag = getUserInfo().getQsStatus();
        if ("1".equals(qsflag)) {
            pramsMap.put("qszt", "1");
        }
        if (!StringUtils.isBlank(kpksrq)) {
            pramsMap.put("kpksrq", kpksrq);
        } else {
            return ResponseEntity.ok(R.error("开票开始日期不能为空！"));
        }
        if (!StringUtils.isBlank(kpjsrq)) {
            pramsMap.put("kpjsrq", kpjsrq);
        } else {
            return ResponseEntity.ok(R.error("开票结束日期不能为空！"));
        }

        if (!StringUtils.isBlank(xfmc)) {
            pramsMap.put("xfmc", xfmc);
        }
        if (!StringUtils.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                pramsMap.put("invoiceNo", invoiceNo);
            }else {
                pramsMap.put("eleInvoiceNo", invoiceNo);
            }
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseIntelligentizeCheckService.beforezngx(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 智能勾选入库 qsfs 默认传9
     *
     * @return 返回结果
     */
    @PostMapping("/sggx/insertZngx")
    @SysLog("智能勾选发票入库接口")
    @ResponseBody
    public Object insertZngx(@RequestBody Map<String, Object> map) {
        List<String> idlist = (List<String>)map.get("ids");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("userName", getUserName());
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
        pramsMap.put("gxType", map.get("gxType"));
        if (idlist != null) {
            pramsMap.put("ids", idlist);
        } else {
            return ResponseEntity.ok(R.error("请选择要操作的发票！"));
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseIntelligentizeCheckService.updatezngx(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 导入勾选查询
     *
     * @return 返回结果
     */
    @PostMapping("/drgx/drgxcx")
    @SysLog("调用导入勾选查询接口")
    public Object listDrgx(@RequestBody String listDrgxReqVo) {

        JSONObject listjson = JSONObject.parseObject(listDrgxReqVo);
        // 首先把字符串转成 JSONArray 对象
        JSONArray jsonarray = JSONArray.parseArray(listjson.getString("datalist"));
        String company = listjson.getString("company");
        String menuId = listjson.getString("menuId");
        List<BaseDrgxReqVO> list = JSONObject.parseArray(jsonarray.toJSONString(), BaseDrgxReqVO.class);
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList;
        gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        pramsMap.put("gfsh", gfshList);
        String qsflag = getUserInfo().getQsStatus();
        pramsMap.put("qsflag", qsflag);
        pramsMap.put("company", company);
        pramsMap.put("menuId", menuId);
        if (list == null || list.size() == 0) {
            return ResponseEntity.ok(R.error("请上传excel文件！"));
        }
        pramsMap.put("listReqVo", list);

        try {
            return ResponseEntity.ok(R.ok().put("data", baseInvoiceImportCheckService.selectByDrgxReqVo(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 扫码，手工，导入勾选操做入口
     *
     * 0-手工勾选 1-扫码勾选 2-导入勾选 3- 智能勾选 4-手工认证 5-扫码认证 6-导入认证
     *
     * @return 返回结果
     */
    @PostMapping("/gx/invoiceCheck")
    @SysLog("调用扫码，手工，导入勾选操做入口接口")
    public Object invoiceCheck(@RequestBody Map<String, Object> map) {
        String ids = (String)map.get("ids");
        String gxType = String.valueOf((int)map.get("gxType"));
        String ses = (String)map.get("ses");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList;
        gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        pramsMap.put("gfsh", gfshList);
        pramsMap.put("userAccount", getLoginName());
        pramsMap.put("userName", getUserName());
        if (!StringHelper.isBlank(gxType)) {
            pramsMap.put("gxType", gxType);
        } else {
            return ResponseEntity.ok(R.error("选择勾选类型！"));
        }
        if (!StringHelper.isBlank(ids)) {
            pramsMap.put("ids", ids);
        } else {
            return ResponseEntity.ok(R.error("选择需要勾选发票！"));
        }
        if (!StringHelper.isBlank(ses)) {
            pramsMap.put("ses", ses);
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseInvoiceCheckService.updateInvoiceCheck(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 已勾选发票待确认列表
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/ygxfp/jgxfpcx")
    @SysLog("调用已勾选发票待确认列表接口")
    public Object jgxfpcx(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String invoiceType = (String)map.get("invoiceType");
        String invoiceSource = (String)map.get("invoiceSource");
        String gxksrq = (String)map.get("gxksrq");
        String gxjsrq = (String)map.get("gxjsrq");
        String businessCode = (String)map.get("businessCode");
        String invoiceNo = (String)map.get("invoiceNo");
        String xfmc = (String)map.get("xfmc");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<Tax> taxList = getUserInfo().getOrg();
        pramsMap.put("taxList", taxList);
        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("payment", map.get("payment"));
        pramsMap.put("paymentStatus", map.get("paymentStatus"));
        pramsMap.put("paymentDateStart", map.get("paymentDateStart"));
        pramsMap.put("paymentDateEnd", map.get("paymentDateEnd"));
        pramsMap.put("qsr", map.get("qsr"));
        pramsMap.put("gxUser", map.get("gxUser"));
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
        pramsMap.put("voucherNumber", map.get("voucherNumber"));
        String qsflag = getUserInfo().getQsStatus();
        if ("1".equals(qsflag)) {
            pramsMap.put("qszt", qsflag);
        }
        pramsMap.put("businessCode", businessCode);

        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        pramsMap.put("gfsh", gfshList);

        if (!StringUtils.isBlank(invoiceType)) {
            if (!"99".equals(invoiceType)) {
                pramsMap.put("invoiceType", invoiceType);
            }
        } else {
            return ResponseEntity.ok(R.error("发票类型不能为空！"));
        }
        if (invoiceSource != null) {
            if (!"99".equals(invoiceSource)) {
                pramsMap.put("invoiceSource", invoiceSource);
            }
        }

        if (!StringUtils.isBlank(gxksrq)) {
            pramsMap.put("gxksrq", gxksrq);
        } else {
            return ResponseEntity.ok(R.error("勾选开始日期不能为空！"));
        }
        if (!StringUtils.isBlank(gxjsrq)) {
            pramsMap.put("gxjsrq", gxjsrq);
        } else {
            return ResponseEntity.ok(R.error("勾选结束日期不能为空！"));
        }

        if (!StringUtils.isBlank(xfmc)) {
            pramsMap.put("xfmc", xfmc);
        }
        if (!StringUtils.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                pramsMap.put("invoiceNo", invoiceNo);
            }else {
                pramsMap.put("eleInvoiceNo", invoiceNo);
            }
        }

        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", baseYgxfpqrService.selectByGfshAndKprq(pramsMap, curr, size))));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 撤销已经勾选的发票
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/gxfp/cxgx")
    @SysLog("调用撤销已经勾选的发票接口")
    public Object cxgx(@RequestBody Map<String, Object> map) {
        String cxids = (String)map.get("cxids");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("userName", getUserName());
        if (!StringHelper.isBlank(cxids) && !"0".equals(cxids)) {
            pramsMap.put("cxids", cxids);
        } else {
            return ResponseEntity.ok(R.error("请选择需要撤销勾选的数据！"));
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", baseCxygxfpService.updateByIds(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 一键提交
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/gxfp/tjrz")
    @SysLog("调用一键提交接口")
    public Object fprz(@RequestBody Map<String, Object> map) {

        String uuids = (String)map.get("uuids");
        String dkses = (String)map.get("dkses");
        String gxType = (String)map.get("gxType");
        int curr = 0;
        int size = 50;
        int count = 0;
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList;
        gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        pramsMap.put("gfsh", gfshList);
        pramsMap.put("loginName", getLoginName());
        pramsMap.put("userName", getUserName());
        if (!StringHelper.isBlank(uuids)) {
            pramsMap.put("uuids", uuids);
        } else {
            return ResponseEntity.ok(R.error("请选择需要提交认证的数据！"));
        }
        if (!StringHelper.isBlank(dkses)) {
            pramsMap.put("dkses", dkses);
        } else {
            return ResponseEntity.ok(R.error("请选择需要提交认证的数据！"));
        }
        if (!StringHelper.isBlank(gxType)) {
            pramsMap.put("gxType", gxType);
        } else {
            return ResponseEntity.ok(R.error("认证方式不能为空！"));
        }

        try {
            return ResponseEntity
                .ok(R.ok().put("data", baseIRzTDxRecordInvoiceService.identifiByIds(pramsMap, curr, size, count)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
