package com.dxhy.base.controller;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.base.entity.BaseReturnData;
import com.dxhy.base.entity.BaseReturnEntity;
import com.dxhy.base.entity.BaseTDxRecordInvoice;
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.constant.DbConstant;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.InvoiceUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.vo.Tax;
import com.dxhy.common.vo.TaxOrgCode;
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 javax.xml.ws.Response;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @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 companyCode = (String) map.get("companyCode");
        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 sortPzbh = String.valueOf(map.get("sortPzbh"));
//        String sortBzdh = String.valueOf(map.get("sortBzdh"));
//        String sortKprq = String.valueOf(map.get("sortKprq"));
        String taxRate = String.valueOf(map.get("taxRate"));
        log.info("taxRate={}",taxRate);
        String inAccountStatus = (String)map.get("inAccountStatus");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        String bzdh = (String) map.get("bzdh");
        String bzr = (String) map.get("bzr");
        Map<String, Object> pramsMap = new HashMap<>(25);

        pramsMap.put("bzdh", bzdh);
        pramsMap.put("bzr",bzr);
        pramsMap.put("inAccountStatus", inAccountStatus);
        pramsMap.put("qszt", map.get("qszt"));
        pramsMap.put("wrzfpStart", map.get("wrzfpStart"));
        pramsMap.put("wrzfpEnd", map.get("wrzfpEnd"));
        pramsMap.put("imageNumber", map.get("imageNumber"));
        pramsMap.put("postingTimeStart", map.get("postingTimeStart"));
        pramsMap.put("postingTimeEnd", map.get("postingTimeEnd"));
        if (map.get("accountPeriod") != null && !"".equals(map.get("accountPeriod"))) {
            String accountPeriod = map.get("accountPeriod").toString().substring(0,7);
            pramsMap.put("accountPeriod", accountPeriod);
            log.info("accountPeriod:{}",accountPeriod);
        }
//        pramsMap.put("accountPeriodStart", map.get("accountPeriodStart"));
//        pramsMap.put("accountPeriodEnd", map.get("accountPeriodEnd"));
        pramsMap.put("snVoucherNumber", map.get("snVoucherNumber"));
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
//        if(StringUtils.isNotEmpty(sortPzbh)) {
//            pramsMap.put("sortPzbh", sortPzbh);
//        }
//        if(StringUtils.isNotEmpty(sortBzdh)) {
//            pramsMap.put("sortBzdh", sortBzdh);
//        }
//        if(StringUtils.isNotEmpty(sortKprq)) {
//            pramsMap.put("sortKprq", sortKprq);
//        }
        pramsMap.put("xfmc", xfmc);
        pramsMap.put("qsr", qsr);
        if (StringUtils.isNotBlank(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());
        pramsMap.put("payment", map.get("payment"));
        pramsMap.put("paymentStatus", map.get("paymentStatus"));

        if(map.get("paymentDateStart") != null) {
            if (StringUtils.isNotEmpty(map.get("paymentDateStart").toString())) {
                String paymentDateStart = DateUtils.getMorning(DateUtils.strToDate(map.get("paymentDateStart").toString()));
                pramsMap.put("paymentDateStart", paymentDateStart);
            }
        }
        if(map.get("paymentEnd") != null) {
            if (StringUtils.isNotEmpty(map.get("paymentEnd").toString())) {
                String paymentDateEnd = DateUtils.getNight(DateUtils.strToDate(map.get("paymentEnd").toString()));
                pramsMap.put("paymentDateEnd", paymentDateEnd);
            }
        }

        pramsMap.put("voucherNumber", map.get("voucherNumber"));
        List<Tax> taxList = getUserInfo().getOrg();
        pramsMap.put("taxList", taxList);
        if (!StringUtils.isNotBlank(gfsh)){
            pramsMap.put("sign","88");
        }
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
//            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            gfshList = getGfshList(gfshList,getUserInfo().getCompanyCodes());
        }
        pramsMap.put("gfsh", gfshList);

        boolean flag = checkList(gfshList);

        if (flag){
            pramsMap.put("sign","88");
            log.info("登陆账号无税号");
        }
        //所属组织
//        List<String> companyCodeList = new ArrayList<>();
//        if (StringUtils.isNotBlank(companyCode)){
//            companyCodeList.add(companyCode);
//        }else {
//            companyCodeList = getUserInfo().getCompanyCodes();
//        }
        if (StringHelper.isNotBlank(companyCode) && "99".equals(companyCode)){
            pramsMap.put("companyCodes",getUserInfo().getCompanyCodes());
            log.info("组织机构====>{}",pramsMap.get("companyCodes"));
        }
        if (StringUtils.isNotBlank(companyCode)){
            pramsMap.put("companyCode",companyCode);
        }

        if (StringUtils.isNotBlank(taxRate) && !"99".equals(taxRate)){
//            List<String> rates = new ArrayList<>();
//            rates = convertRates(taxRate,rates);
            pramsMap.put("taxRate",taxRate);
        }

        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)) {
                        qsksrq = DateUtils.getMorning(DateUtils.strToDate(qsksrq));
                        pramsMap.put("qsksrq", qsksrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收开始日期不能为空！"));
                    }
                    if (!StringHelper.isBlank(qsjsrq)) {
                        qsjsrq = DateUtils.getNight(DateUtils.strToDate(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)) {
                        rzksrq = DateUtils.getMorning(DateUtils.strToDate(rzksrq));
                        pramsMap.put("rzksrq", rzksrq);
                    } else {
                        return ResponseEntity.ok(R.error("认证开始日期不能为空！"));
                    }
                    if (!StringHelper.isBlank(rzjsrq)) {
                        rzjsrq = DateUtils.getMorning(DateUtils.strToDate(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)) {
            String morning = DateUtils.getMorning(DateUtils.strToDate(kpksrq));
            pramsMap.put("kpksrq", morning);
        } else {
            return ResponseEntity.ok(R.error("开票开始日期不能为空！"));
        }
        if (!StringHelper.isBlank(kpjsrq)) {
            String night = DateUtils.getMorning(DateUtils.strToDate(kpjsrq));
            pramsMap.put("kpjsrq", night);
        } else {
            return ResponseEntity.ok(R.error("开票结束日期不能为空！"));
        }
        if (StringUtils.isNotBlank(inAccountStatus)) {
            pramsMap.put("inAccountStatus", inAccountStatus);
        }

        if(pramsMap.get("imageNumber") != null){
            pramsMap.put("imageNumber",pramsMap.get("imageNumber"));
        }

        if(pramsMap.get("snVoucherNumber") != null){
            pramsMap.put("snVoucherNumber",pramsMap.get("snVoucherNumber"));
        }

        if(pramsMap.get("accountPeriod") != null ){
            if(pramsMap.get("accountPeriod").toString().length() > 7) {
                String accountPeriod = pramsMap.get("accountPeriod").toString().substring(0, 7);
                pramsMap.put("accountPeriod", accountPeriod);
            }else{
                pramsMap.put("accountPeriod", pramsMap.get("accountPeriod"));
            }
        }

        if (pramsMap.get("postingTimeStart") != null && !"".equals(pramsMap.get("postingTimeStart"))) {
            pramsMap.put("postingTimeStart",
                    DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("postingTimeStart").toString())));
        }
        if (pramsMap.get("postingTimeEnd") != null && !"".equals(pramsMap.get("postingTimeEnd"))) {
            pramsMap.put("postingTimeEnd",
                    DateUtils.getNight(DateUtils.strToDate(pramsMap.get("postingTimeEnd").toString())));
        }

        log.info("请求列表的参数为:{}",pramsMap);

        try {
            return ResponseEntity.ok(R.ok().put("data", baseRzFpxxService.selectByPramsMap(pramsMap, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            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) {
//        List<TaxOrgCode> orgCodes = getUserInfo().getOrgCodes();
//        log.info("user信息========>{}",getUserInfo());
//        if (orgCodes != null && orgCodes.size() > 0){
//            for (int i = 0; i < orgCodes.size(); i++) {
//                log.info("orgcode=======>{}",orgCodes.get(i));
//            }
//        }else {
//            log.info("未能获得机构代码");
//        }

        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 sortPzbh = String.valueOf(map.get("sortPzbh"));
        String sortBzdh = String.valueOf(map.get("sortBzdh"));
        String sortKprq = String.valueOf(map.get("sortKprq"));
        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");
        String bzdh = (String) map.get("bzdh");
        String inAccountStatus = (String) map.get("inAccountStatus");
        String companyCode = (String) map.get("companyCode");
        String suspendStatus = (String) map.get("suspendStastus");
        String suspendDate = (String) map.get("suspendDate");
        String bzr = (String) map.get("bzr");
        String taxRate = String.valueOf(map.get("taxRate"));
        if (StringUtils.isNotEmpty(suspendDate) && suspendDate.length() >7){
            suspendDate = suspendDate.substring(0,7);
        }

//        //所属机构列表
//        List<String> orgCodes = null;
//        if (map.get("orgCodes") !=null){
//            orgCodes = (List<String>) map.get("orgCodes");
//        }else {
//            List<TaxOrgCode> taxOrgCodes  = getUserInfo().getOrgCodes();
//            if (taxOrgCodes != null && taxOrgCodes.size() > 0){
//                for (TaxOrgCode taxOrgCode : taxOrgCodes) {
//                    orgCodes.add(taxOrgCode.getOrgCode());
//                }
//            }
//        }


        Map<String, Object> pramsMap = new HashMap<>(32);
//        pramsMap.put("orgCodes",orgCodes);
        pramsMap.put("bzdh", bzdh);
        pramsMap.put("inAccountStatus", inAccountStatus);
        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"));
        pramsMap.put("snVoucherNumber", map.get("snVoucherNumber"));
        pramsMap.put("postingTimeEnd", map.get("postingTimeEnd"));
        pramsMap.put("postingTimeStart", map.get("postingTimeStart"));
        pramsMap.put("imageNumber", map.get("imageNumber"));
        pramsMap.put("suspendStatus",suspendStatus);
        pramsMap.put("suspendDate",suspendDate);
        pramsMap.put("sortPzbh",sortPzbh);
        pramsMap.put("sortBzdh",sortBzdh);
        pramsMap.put("sortKprq",sortKprq);
        pramsMap.put("bzr",bzr);
        if(StringUtils.isNotEmpty(sortPzbh)) {
            pramsMap.put("sortPzbh", sortPzbh);
        }
        if(StringUtils.isNotEmpty(sortBzdh)) {
            pramsMap.put("sortBzdh", sortBzdh);
        }
        if(StringUtils.isNotEmpty(sortKprq)) {
            pramsMap.put("sortKprq", sortKprq);
        }

        if (map.get("accountPeriod") != null && !"".equals(map.get("accountPeriod"))) {
            String accountPeriod = map.get("accountPeriod").toString().substring(0,7);
            pramsMap.put("accountPeriod", accountPeriod);
            log.info("accountPeriod:{}",accountPeriod);
        }
//        pramsMap.put("accountPeriod", map.get("accountPeriod"));
//        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);
            }
        }
        if (!StringUtils.isNotBlank(gfsh)){
            pramsMap.put("sign","88");
        }

        if (StringUtils.isNotBlank(taxRate) && !"99".equals(taxRate)){
//            List<String> rates = new ArrayList<>();
//            rates = convertRates(taxRate,rates);
            pramsMap.put("taxRate",taxRate);
        }

        //所属组织
//        List<String> companyCodeList = new ArrayList<>();
//        if (StringUtils.isNotBlank(companyCode) && !"99".equals(companyCode)){
//            companyCodeList.add(companyCode);
//        }else {
//            companyCodeList = getUserInfo().getCompanyCodes();
//            log.info("获取到的组织信息{}",companyCodeList);
//        }
        List<String> companyCodes = getUserInfo().getCompanyCodes();

        if (StringHelper.isNotBlank(companyCode) && "99".equals(companyCode)){
            pramsMap.put("companyCodes",companyCodes);
            log.info("组织机构====>{}",pramsMap.get("companyCodes"));
        }
        if (StringUtils.isNotBlank(companyCode)){
            pramsMap.put("companyCode",companyCode);
        }


        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
            log.info("手工认证有税号");
        } else {
            gfshList = getGfshList(gfshList,companyCodes);
            log.info("手工认证无税号{}",gfshList);
        }
        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)) {
                        qsksrq = DateUtils.getMorning(DateUtils.strToDate(qsksrq));
                        pramsMap.put("qsksrq", qsksrq);
                    } else {
                        return ResponseEntity.ok(R.error("签收开始日期不能为空！"));
                    }
                    if (!StringUtils.isBlank(qsjsrq)) {
                        qsjsrq = DateUtils.getNight(DateUtils.strToDate(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) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    private List<String> getGfshList(List<String> gfshList, List<String> companyCodes) {
        gfshList = baseInvoiceManualCheckService.getGfshList(gfshList,companyCodes);
        return gfshList;
    }

    private List<String> convertRates(String taxRate,List<String> rates) {
        switch (taxRate) {
            case "0":
                rates.add("0");
                break;
            case "1.5":
                rates.add("1.5");
                break;
            case "2":
                rates.add("2");
                rates.add("0.02");
                break;
            case "3":
                rates.add("3");
                rates.add("0.03");
                break;
            case "5":
                rates.add("5");
                rates.add("0.05");
                break;
            case "6":
                rates.add("6");
                rates.add("0.06");
                break;
            case "9":
                rates.add("9");
                rates.add("0.09");
                break;
            case "10":
                rates.add("10");
                rates.add("0.10");
                break;
            case "13":
                rates.add("13");
                rates.add("0.13");
                break;
            case "16":
                rates.add("16");
                rates.add("0.16");
                break;
            case "17":
                rates.add("17");
                rates.add("0.17");
                break;
            default:
                break;
        }
        return rates;
    }

    public boolean checkList(List<String> list) {
        boolean flag =false;
        if (list == null || list.isEmpty()){
            flag = true;
        }else {
            String s = list.toString().replace("[","").replace("]","");
            if (StringHelper.isBlank(s)){
                flag = true;
            }
            if (StringHelper.isNotBlank(s) && s.contains(",")){
                String replace = s.replace(",", "");
                if (StringHelper.isBlank(replace)){
                    flag = true;
                }
            }
        }
        return flag;
    }

    /**
     * 智能勾选查询 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) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }


    /**
     * 智能勾选查询 qsfs 默认传9
     *
     * @return 返回结果
     */
    @PostMapping("/sggx/inTellingCheck")
    @SysLog("调用智能勾选查询接口")
    @ResponseBody
    public Object sgrzzngxcx(@RequestBody Map<String, Object> map) {

        String inAccountStatus = (String) map.get("inAccountStatus");
        String companyCode = (String) map.get("companyCode");
        String gfsh = (String)map.get("gfsh");
        String maxTax = (String) map.get("maxTax");
        String menuId = (String)map.get("menuId");
        String company = (String)map.get("company");
        String isChooseAll = (String) map.get("isChooseAll");

        if (org.apache.commons.lang3.StringUtils.isBlank(isChooseAll)){
            isChooseAll = "N";
        }
        int curr = 0;
        int size = 0;
        if ("N".equalsIgnoreCase(isChooseAll)){

            if (map.get("curr") != null){
                curr = (int) map.get("curr");
            }else {
                ResponseEntity.ok(R.error("分页数据不能为空"));
            }


            if (map.get("size")!= null){
                size = (int) map.get("size");
            }else {
                ResponseEntity.ok(R.error("分页大小不能为空"));
            }
        }

        if (StringUtils.isBlank(inAccountStatus)){
            return ResponseEntity.ok(R.error("入账状态不能为空"));
        }

        if (StringUtils.isBlank(maxTax)){
            return ResponseEntity.ok(R.error("税额上限不能为空"));
        }

        try {
            BigDecimal tax = new BigDecimal(maxTax);
        } catch (NumberFormatException e) {
            return ResponseEntity.ok(R.error("输入税额上限格式不对，请重新输入"));
        }


        Map<String, Object> pramsMap = new HashMap<>(16);

        pramsMap.put("inAccountStatus", inAccountStatus);
        pramsMap.put("company", company);
        pramsMap.put("menuId", menuId);
        pramsMap.put("maxTax",maxTax);
        pramsMap.put("curr",curr);
        pramsMap.put("size",size);
        pramsMap.put("isChooseAll",isChooseAll);
        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<Tax> taxList = getUserInfo().getOrg();
        pramsMap.put("taxList", taxList);
        pramsMap.put("qsflag", getUserInfo().getQsStatus());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }

        //TODO 合并分支后把checkList加上可以让用户不使用上级权限就可以进行认证

        pramsMap.put("gfsh",gfshList);

        pramsMap.put("sign","99");


        if (StringHelper.isNotBlank(companyCode) && "99".equals(companyCode)){
            pramsMap.put("companyCodes",getUserInfo().getCompanyCodes());
            log.info("组织机构====>{}",pramsMap.get("companyCodes"));
        }
        if (StringUtils.isNotBlank(companyCode)){
            pramsMap.put("companyCode",companyCode);
        }



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

    }

    @PostMapping("/sggx/afterInTellingCheck")
    @SysLog("智能勾选后认证")
    @ResponseBody
    public Object afterInTellingCheck(@RequestBody Map<String, Object> map) {

//        String isChooseAll = (String) map.get("isChooseAll");
//
//        if (StringUtils.isBlank(isChooseAll)){
//            isChooseAll = "Y";
//        }

        map.put("isChooseAll","Y");
        int curr = 1;
        int size = 10;
        int count = 0;

        Map<String, Object> pramsMap = new HashMap<>(16);

        String companyCode = (String) map.get("companyCode");
        String gfsh = (String)map.get("gfsh");
        List<String> gfshList = new ArrayList<>();
        if(StringUtils.isNotBlank(gfsh) && "99".equals(gfsh)){
            gfshList=UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }else {
            gfshList.add(gfsh);
        }
        String gxType = (String) map.get("gxType");

        if (StringUtils.isBlank(gxType)){
            return ResponseEntity.ok(R.error("所选发票勾选类型不能为空"));
        }

        map.put("business", getUserInfo().getBusiness());
        map.put("dbName", getUserInfo().getDbName());
        List<Tax> taxList = getUserInfo().getOrg();
        map.put("taxList", taxList);
        map.put("gfsh",gfshList);
        map.put("qsflag", getUserInfo().getQsStatus());
//            List<String> gfshList = new ArrayList<>();
//            if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
//                gfshList.add(gfsh);
//            } else {
//                gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
//            }
//
//            //todo合并分支后把checkList加上可以让用户不使用上级权限就可以进行认证
//
//            map.put("gfsh",gfshList);
//
            map.put("sign","99");

        if (StringHelper.isNotBlank(companyCode) && "99".equals(companyCode)){
            map.put("companyCodes",getUserInfo().getCompanyCodes());
            log.info("组织机构====>{}",map.get("companyCodes"));
        }

        if (StringUtils.isNotBlank(companyCode)){
            map.put("companyCode",companyCode);
        }
        String sgrzzngxcx = baseInvoiceManualCheckService.inTellingCheck(map);
        log.info("sgrzzngxcx======={}",sgrzzngxcx);
        String decodeStr = Base64.decodeStr(sgrzzngxcx);

        BaseReturnEntity baseReturnEntity = JSON.parseObject(decodeStr, BaseReturnEntity.class);
        List<BaseTDxRecordInvoice> dataList = baseReturnEntity.getDataList();
        StringBuilder uuids = new StringBuilder();
        StringBuilder dkses = new StringBuilder();
        if (dataList != null && dataList.size() > 0){
            for (int i = 0; i < dataList.size(); i++) {
                uuids.append(dataList.get(i).getUuid());
                dkses.append(dataList.get(i).getTaxAmount());
                if (i<dataList.size()-1){
                    uuids.append(",");
                    dkses.append(",");
                }
            }
            log.info("uuids====>{}",uuids);
            log.info("dkses====>{}",dkses);
        }else {
            return ResponseEntity.ok(R.error("没有可操作的数据"));

        }
        pramsMap.put("uuids",uuids);
        pramsMap.put("dkses",dkses);
        pramsMap.put("name", getUserName());
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("loginName", getLoginName());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gxType", gxType);
        pramsMap.put("gfsh", gfshList);

        try {
            return ResponseEntity
                    .ok(R.ok().put("data", baseIRzTDxRecordInvoiceService.identifiByIds(pramsMap, curr, size, count)));
        } catch (Exception e) {
            e.printStackTrace();
            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("userAccount",getUserInfo().getLoginname());
        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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            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) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/sgrz/changeSuspend")
    @SysLog("修改暂挂状态")
    @ResponseBody
    public Object changeSuspend(@RequestBody Map<String,Object> map){

        if (map == null){
            return ResponseEntity.ok(R.error("缺少请求参数"));
        }
        log.info("getUserInfo====>{}",getUserInfo());
        List<String> gfshAll = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        if (gfshAll ==null || gfshAll.isEmpty()){
            return ResponseEntity.ok(R.error("购方信息不能为空"));
        }

        JSONObject jsonObject = new JSONObject(map);
        String uuids = jsonObject.getString("uuids");
        if (StringUtils.isEmpty(uuids) ||(uuids.length() == 1 && uuids.contains(","))){
            return ResponseEntity.ok(R.error("请先选择要暂挂的数据"));
        }
        String suspendType = jsonObject.getString("suspendType");
        if (StringUtils.isEmpty(suspendType)){
            return ResponseEntity.ok(R.error("暂挂状态不能为空"));
        }
        String[] uuidArray = uuids.split(",");
        List<String> uuidList = Arrays.asList(uuidArray);

        //查询库中是否包含对应数据
        DynamicContextHolder.push(getUserInfo().getDbName()+ DbConstant.BUSINESS_READ);
        List<String> list = baseIRzTDxRecordInvoiceService.findSuspendStatus(uuidList);
        if (list == null || list.isEmpty()){
            return ResponseEntity.ok(R.error("没有可操作的数据"));
        }

        if (list.contains("1")&& (list.contains("0")||list.contains(null) || list.contains(""))){
            return ResponseEntity.ok(R.error("同时存在暂挂状态为是和否的数据，请重新选择！"));
        }

        Map<String,Object> pramsMap = new HashMap<>();
        pramsMap.put("suspendType",suspendType);
        pramsMap.put("uuids",uuidList);
        pramsMap.put("dbName",getUserInfo().getDbName());
//        pramsMap.put("dbName","business");

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

    }

}
