package com.dxhy.sign.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.enums.FplxEnum;
import com.dxhy.common.util.*;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.vo.Business;
import com.dxhy.common.vo.UserInfo;
import com.dxhy.oss.service.FileService;
import com.dxhy.sign.entity.QsExcelEntity;
import com.dxhy.sign.entity.TDxInvoiceImg;
import com.dxhy.sign.service.elec.SignInvoiceImgService;
import com.dxhy.sign.service.fpqs.SignFpqsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * 发票签收模块入口
 *
 * @author ld
 *
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController

@Slf4j
public class SignFpqsController extends AbstractController {
    @Resource
    private SignFpqsService signFpqsService;

    @Resource
    private SignInvoiceImgService signInvoiceImgService;

    @Resource
    private FileService fileService;

    /**
     * 手工签收发票列表查询
     *
     * @param pramsMap
     *            购方税号，全部时传0
     *
     * @return 返回结果
     *
     */
    @RequestMapping("/sgqs/list")
    @ResponseBody
    @SysLog("手工签收--列表查询")
    public ResponseEntity<?> listWqsfp(@RequestBody Map<String, Object> pramsMap) {
        // 入参统一在入口处理
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("gfsh", gfshList);
        if (pramsMap.get("kprqq") != null && !"".equals(pramsMap.get("kprqq"))) {
            pramsMap.put("kprqq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("kprqq").toString())));
        } else {
            return ResponseEntity.ok(R.error("开票开始日期不能为空！"));
        }
        if (pramsMap.get("kprqz") != null && !"".equals(pramsMap.get("kprqz"))) {
            pramsMap.put("kprqz", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("kprqz").toString())));
        } else {
            return ResponseEntity.ok(R.error("开票结束日期不能为空！"));
        }

        if (pramsMap.get("invoiceType") != null && !"".equals(pramsMap.get("invoiceType"))
            && !"99".equals(pramsMap.get("invoiceType"))) {
            pramsMap.put("invoiceType", pramsMap.get("invoiceType"));
        } else {
            pramsMap.put("invoiceType", "");
        }

        pramsMap.put("invoiceNo", pramsMap.get("invoiceNo"));
        //全电号码用后8位查询
        if(ObjectUtils.isNotEmpty(pramsMap.get("invoiceNo"))&&pramsMap.get("invoiceNo").toString().length()==20){
            pramsMap.put("invoiceNo", pramsMap.get("invoiceNo").toString().substring(12,20));
        }
        pramsMap.put("invoiceSource",pramsMap.get("invoiceSource"));
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");

        try {
            if(pramsMap.get("emailAddress") == null){
            return ResponseEntity.ok(R.ok().put("data", signFpqsService.selectWqsfp(pramsMap, curr, size)));
            }else {
                return ResponseEntity.ok(signFpqsService.qsSendEmail(pramsMap));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


    /**
     * 邮箱签收
     *
     * @param pramsMap
     *            购方税号，全部时传0
     *
     * @return 返回结果
     *
     */
    @RequestMapping("/sendEmailByQs")
    @ResponseBody
    @SysLog("手工签收--邮箱签收")
    public ResponseEntity<?> sendEmailByQs(@RequestBody Map<String, Object> pramsMap) {
        if (pramsMap.get("emailAddress") == null || "".equals(pramsMap.get("emailAddress"))) {
            return ResponseEntity.ok(R.error("邮箱地址不能为空！"));
        }
        if (pramsMap.get("ids") == null || "".equals(pramsMap.get("ids"))) {
            return ResponseEntity.ok(R.error("请选择发票数据"));
        }
        return ResponseEntity.ok(signFpqsService.qsSendEmail(pramsMap));
    }

    @RequestMapping("/qsExport")
    public void qsExport(@RequestBody Map<String, Object> pramsMap, HttpServletResponse response){
        if (pramsMap.get("ids") == null || "".equals(pramsMap.get("ids"))) {
//            return ResponseEntity.ok(R.error("请选择发票数据"));
            return;
        }
        List<QsExcelEntity> excelList = signFpqsService.selectWqsfp(pramsMap);
        ExcelWriter excelWriter = null;
        if(excelList != null){
            ByteArrayOutputStream  outputStream = null;
            try {
                String fileName =  URLEncoder.encode("签收数据导出", "UTF-8").replaceAll("\\+", "%20");
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding("utf-8");
                response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
//                excelWriter = EasyExcel.write(outputStream).build();
//                WriteSheet writeSheet = EasyExcel.writerSheet("sheet").head(QsExcelEntity.class).build();
//                excelWriter.write(excelList, writeSheet);
                EasyExcel.write(response.getOutputStream(), QsExcelEntity.class).sheet("sheet").doWrite(excelList);
            }catch(Exception e){
                log.error("签收数据导出，错误日志：{}",e);
            } finally {
                // 千万别忘记finish 会帮忙关闭流
                if (excelWriter != null) {
                    excelWriter.finish();
                }
                if(outputStream != null){
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }

    /**
     * 签收选定的未签收的发票
     *
     * @param postInvoiceDetail
     *            请求参数
     * @return 返回结果
     *
     */
    @PostMapping("/sgqs/fpqs")
    @ResponseBody
    @SysLog("手工签收--发票签收")
    public ResponseEntity<?> qsWqsfp(@RequestBody String postInvoiceDetail) {
        try {
            JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);
            if (!invoiceDetail.containsKey("zsl") || StringHelper.isBlank(invoiceDetail.getString("zsl"))) {
                return ResponseEntity.ok(R.error("签收发票总数不能为空！"));
            }
            JSONArray detailArray;
            if (!invoiceDetail.containsKey("ids")) {
                return ResponseEntity.ok(R.error("签收发票信息不能为空！"));
            } else {
                detailArray = (JSONArray)invoiceDetail.get("ids");
                if (detailArray == null || detailArray.size() != invoiceDetail.getLongValue("zsl")) {
                    return ResponseEntity.ok(R.error("发票明细数量和总数量不一致！"));
                }
            }

            // 入参统一在入口处理
            Map<String, String> pramsMap = new HashMap<>(16);

            pramsMap.put("userId", getUserId());
            pramsMap.put("userName", getUserName());
            pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
            pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("company", getUserInfo().getCompany());

            String businessCode = null;
            if (invoiceDetail.containsKey("businessCode")) {
                businessCode = invoiceDetail.getString("businessCode");
            }

            if (StringUtils.isNoneBlank(businessCode) && !"99".equals(businessCode)) {
                List<Business> businessList = getUserInfo().getBusiness();
                for (Business business : businessList) {
                    if (business.getBusinessCode().equals(businessCode)) {
                        pramsMap.put("businessName", business.getBusinessName());
                    }
                }
            } else {
                businessCode = "";
            }

            if (!StringHelper.isBlank(businessCode)) {
                pramsMap.put("businessCode", businessCode);
            }
            // 业务单元参数设置
            UserInfo userInfo = getUserInfo();
            return ResponseEntity.ok(R.ok().put("data", signFpqsService.qsWqsfp(detailArray, pramsMap, userInfo)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 扫码签收签收发票
     *
     * @param pramsMap
     *            发票类型
     *
     * @return 返回结果
     *
     */
    @PostMapping("/smqs/fpqs")
    @ResponseBody
    @SysLog("扫码签收")
    public ResponseEntity<?> smqsQsfp(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());

        if (StringUtils.isNoneBlank(pramsMap.get("businessCode")) && !"99".equals(pramsMap.get("businessCode"))) {
            List<Business> businessList = getUserInfo().getBusiness();
            for (Business business : businessList) {
                if (business.getBusinessCode().equals(pramsMap.get("businessCode"))) {
                    pramsMap.put("businessName", business.getBusinessName());
                }
            }
        }
        String invoiceType = pramsMap.get("invoiceType");
        String invoiceCode = pramsMap.get("invoiceCode");
        String invoiceNo = pramsMap.get("invoiceNo");
        if(FplxEnum.QDZZP.getFplxDm().equals(invoiceType) || FplxEnum.QDPP.getFplxDm().equals(invoiceType)){
            pramsMap.put("invoiceType", invoiceType);
        }else if("99".equals(invoiceType) && invoiceNo.length() == 20){
            invoiceType = "31";
            pramsMap.put("invoiceType", invoiceType);
        }else {
            InvoiceUtil iu = new InvoiceUtil(pramsMap.get("invoiceCode"));
            pramsMap.put("invoiceType", iu.getFplxdm());
            if (pramsMap.get("invoiceCode").isEmpty()) {

                return ResponseEntity.ok(R.error("发票代码不能为空！"));
            }
        }

        if (pramsMap.get("invoiceNo").isEmpty()) {
            return ResponseEntity.ok(R.error("发票号码不能为空！"));
        }

        if (pramsMap.get("invoiceDate").isEmpty()) {

            return ResponseEntity.ok(R.error("开票日期不能为空！"));
        }

        if (FplxEnum.ZP.getFplxDm().equals(pramsMap.get("invoiceType"))
            || FplxEnum.DZZP.getFplxDm().equals(pramsMap.get("invoiceType"))
            || FplxEnum.JDC.getFplxDm().equals(pramsMap.get("invoiceType"))
            || FplxEnum.QDZZP.getFplxDm().equals(pramsMap.get("invoiceType"))
            || FplxEnum.QDPP.getFplxDm().equals(pramsMap.get("invoiceType"))) {
            if (pramsMap.get("invoiceAmount").isEmpty()) {
                return ResponseEntity.ok(R.error("金额不能为空！"));
            }
        } else if (FplxEnum.ESC.getFplxDm().equals(pramsMap.get("invoiceType"))) {
            if (pramsMap.get("invoiceAmount").isEmpty()) {
                return ResponseEntity.ok(R.error("车价合计不能为空！"));
            }
        } else {
            if (pramsMap.get("checkCode").isEmpty()) {
                return ResponseEntity.ok(R.error("校验码不能为空！"));
            }
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", signFpqsService.smqsQsfp(pramsMap, getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 删除签收失败的签收记录
     *
     * @param smid
     *            发票类型
     *
     * @return 返回结果
     *
     */
    @PostMapping("/smqs/del")
    @ResponseBody
    @SysLog("删除签收失败发票")
    public ResponseEntity<?> delQsjl(@RequestBody String smid) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("userId", getUserName());
        pramsMap.put("dbName", getUserInfo().getDbName());

        if (!StringHelper.isBlank(smid)) {
            pramsMap.put("uuid", smid);
        } else {
            return ResponseEntity.ok(R.error("记录唯一标识不能为空！"));
        }
        pramsMap.put("del", "1");
        try {
            String msg = signFpqsService.requestValidate(pramsMap);
            if (msg == null) {
                signFpqsService.delQsjl(pramsMap);
                return ResponseEntity.ok(R.ok().put("data", CommonConstants.MSG_SUCC_DEFAULT));
            } else {
                return ResponseEntity.ok(R.error(msg));
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 扫描仪签收签收发票
     *
     * @param pramsMap
     *            扫描识别批次号
     *
     * @return 返回结果
     *
     */
    @PostMapping("/smyqs/fpqs")
    @ResponseBody
    @SysLog("扫描仪签收")
    public ResponseEntity<?> smyqsQsfp(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());

        if (StringUtils.isNoneBlank(pramsMap.get("businessCode")) && !"99".equals(pramsMap.get("businessCode"))) {
            List<Business> businessList = getUserInfo().getBusiness();
            for (Business business : businessList) {
                if (business.getBusinessCode().equals(pramsMap.get("businessCode"))) {
                    pramsMap.put("businessName", business.getBusinessName());
                }
            }
        }
        if (StringHelper.isBlank(pramsMap.get("scanPch"))) {

            return ResponseEntity.ok(R.error("扫描识别批次号不能为空！"));
        } else {
            pramsMap.put("pch", pramsMap.get("scanPch"));
        }

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

    /**
     * 修改扫描记录
     *
     * @param pramsMap
     *            扫描表对应唯一键
     * @return 返回结果
     *
     */
    @PostMapping("/smxx/update")
    @ResponseBody
    @SysLog("扫描仪签收--修改")
    public ResponseEntity<?> updateQsjl(@RequestBody Map<String, String> pramsMap) {

        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());

        if (!pramsMap.get("smid").isEmpty()) {
            pramsMap.put("scanId", pramsMap.get("smid"));
        } else {
            return ResponseEntity.ok(R.error("记录唯一标识不能为空！"));
        }
        if(!(FplxEnum.QDZZP.getFplxDm().equals(pramsMap.get("invoiceType")) || FplxEnum.QDPP.getFplxDm().equals(pramsMap.get("invoiceType")))){
            if (pramsMap.get("invoiceCode").isEmpty()) {

                return ResponseEntity.ok(R.error("发票代码不能为空！"));
            }
            if (pramsMap.get("xfsh") != null && pramsMap.get("xfsh").isEmpty()) {

                return ResponseEntity.ok(R.error("销方税号不能为空！"));
            }
            if (pramsMap.get("invoiceAmount") != null && pramsMap.get("invoiceAmount").isEmpty()) {

                return ResponseEntity.ok(R.error("金额不能为空！"));
            }
        }else {
            if (pramsMap.get("totalAmount") != null && pramsMap.get("totalAmount").isEmpty()) {

                return ResponseEntity.ok(R.error("价税合计不能为空！"));
            }
        }


        if (pramsMap.get("invoiceNo").isEmpty()) {

            return ResponseEntity.ok(R.error("发票号码不能为空！"));
        }

        if (pramsMap.get("invoiceDate").isEmpty()) {

            return ResponseEntity.ok(R.error("开票日期不能为空！"));
        }

        if (pramsMap.get("gfsh") != null && pramsMap.get("gfsh").isEmpty()) {

            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }





        if (StringUtils.isNoneBlank(pramsMap.get("businessCode")) && !"99".equals(pramsMap.get("businessCode"))) {
            List<Business> businessList = getUserInfo().getBusiness();
            for (Business business : businessList) {
                if (business.getBusinessCode().equals(pramsMap.get("businessCode"))) {
                    pramsMap.put("businessName", business.getBusinessName());
                }
            }
        }

        try {

            return ResponseEntity.ok(R.ok().put("data", signFpqsService.updateQsjl(pramsMap, getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 签收excel导入的发票
     *
     * @param postInvoiceDetail
     *            请求参数
     * @return 返回结果
     *
     */
    @PostMapping("/excelqs/fpqs")
    @ResponseBody
    @SysLog("导入签收")
    public ResponseEntity<?> excelQsfp(@RequestBody String postInvoiceDetail) {
        try {

            JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);
            if (!invoiceDetail.containsKey("zsl") || StringHelper.isBlank(invoiceDetail.getString("zsl"))) {
                return ResponseEntity.ok(R.error("签收发票总数不能为空！"));
            }
            JSONArray detailArray;
            if (!invoiceDetail.containsKey("invoices")) {
                return ResponseEntity.ok(R.error("签收发票信息不能为空！"));
            } else {
                detailArray = (JSONArray)invoiceDetail.get("invoices");
                if (detailArray == null || detailArray.size() != invoiceDetail.getLongValue("zsl")) {
                    return ResponseEntity.ok(R.error("发票明细数量和总数量不一致！"));
                }
            }

            // 入参处理
            Map<String, String> pramsMap = new HashMap<>(16);
            String businessCode = null;
            if (invoiceDetail.containsKey("businessCode")) {
                businessCode = invoiceDetail.getString("businessCode");
            }

            if (StringUtils.isNoneBlank(businessCode) && !"99".equals(businessCode)) {
                List<Business> businessList = getUserInfo().getBusiness();
                for (Business business : businessList) {
                    if (business.getBusinessCode().equals(businessCode)) {
                        pramsMap.put("businessName", business.getBusinessName());
                    }
                }
            } else {
                businessCode = "";
            }
            pramsMap.put("businessCode", businessCode);

            String msg = signFpqsService.checkExcelFp(detailArray);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                pramsMap.put("userId", getUserId());
                pramsMap.put("userName", getUserName());
                pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
                pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
                pramsMap.put("dbName", getUserInfo().getDbName());
                pramsMap.put("company", invoiceDetail.getString("company"));
                pramsMap.put("menuId", invoiceDetail.getString("menuId"));

                return ResponseEntity
                    .ok(R.ok().put("data", signFpqsService.qsExcelfp(detailArray, pramsMap, getUserInfo())));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 查询已签收信息
     *
     * @param pramsMap
     *            购方税号，全部时传0
     * @return 返回结果
     *
     */
    @RequestMapping("/qsxx/list")
    @ResponseBody
    @SysLog("签收查询")
    public ResponseEntity<?> listQsfp(@RequestBody Map<String, Object> pramsMap) {
        // 入参统一在入口处理
        Map<String, Object> data = new HashMap<>(21);

        log.info("签收查询接收到参数: {}",pramsMap);
        data.put("userId", getUserId());
        data.put("dbName", getUserInfo().getDbName());
        data.put("gfsh", pramsMap.get("gfsh"));
        data.put("invoiceNo", pramsMap.get("invoiceNo"));
        //全电号码用后8位查询
        if(ObjectUtils.isNotEmpty(pramsMap.get("invoiceNo"))&&pramsMap.get("invoiceNo").toString().length()==20){
            data.put("invoiceNo", pramsMap.get("invoiceNo").toString().substring(12,20));
        }
        data.put("businessCode", pramsMap.get("businessCode"));
        data.put("notes", pramsMap.get("notes"));
        data.put("xfsh", pramsMap.get("xfsh"));
        data.put("settlementNo", pramsMap.get("settlementNo"));
        data.put("settlementStatus", pramsMap.get("settlementStatus"));
        data.put("gxStatus", pramsMap.get("gxStatus"));
        data.put("orgList", getUserInfo().getOrg());
        String company = (String)pramsMap.get("company");
        String menuId = (String)pramsMap.get("menuId");
        data.put("company", company);
        data.put("menuId", menuId);
        data.put("voucherNumber", pramsMap.get("voucherNumber"));
        List<Business> business = getUserInfo().getBusiness();
        if (business.size() > 0) {
            data.put("isBusiness", true);
        }

        String businessCode = (String)pramsMap.get("businessCode");

        if (!StringHelper.isBlank(businessCode) && !"99".equals(businessCode)) {
            data.put("business", businessCode);
        }
        if (pramsMap.get("qsrqq") != null && !"".equals(pramsMap.get("qsrqq"))) {
            data.put("qsrqq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsrqq").toString())));
        } else {
            return ResponseEntity.ok(R.error("签收日期不能为空！"));
        }
        if (pramsMap.get("qsrqz") != null && !"".equals(pramsMap.get("qsrqz"))) {
            data.put("qsrqz", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsrqz").toString())));
        } else {
            return ResponseEntity.ok(R.error("签收日期不能为空！"));
        }

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

        if (pramsMap.get("invoiceType") != null && !"".equals(pramsMap.get("invoiceType"))
            && !"99".equals(pramsMap.get("invoiceType"))) {
            data.put("invoiceType", pramsMap.get("invoiceType"));
        }
        if (pramsMap.get("qsStatus") != null && !"".equals(pramsMap.get("qsStatus"))
            && !"99".equals(pramsMap.get("qsStatus"))) {
            data.put("qsStatus", pramsMap.get("qsStatus"));
        }
        if (pramsMap.get("qsType") != null && !"".equals(pramsMap.get("qsType"))
            && !"99".equals(pramsMap.get("qsType"))) {
            data.put("qsType", pramsMap.get("qsType"));
        }
        if (pramsMap.get("qsNameFlag") != null && !"".equals(pramsMap.get("qsNameFlag"))) {
            // 判断是否需要根据签收人查询
            data.put("qsNameFlag", pramsMap.get("qsNameFlag"));
        }
        data.put("xfmc", pramsMap.get("xfmc"));
        data.put("invoiceSource", pramsMap.get("invoiceSource"));
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            log.info("签收列表查询参数 {}",data);
//            log.info("返回结果为 {}",signFpqsService.selectQsfpList(data,curr,size));
            return ResponseEntity.ok(R.ok().put("data", signFpqsService.selectQsfpList(data, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 发票二次签收接口
     *
     * @param postInvoiceDetail
     *            签收记录表对应记录的唯一键
     *
     * @return 返回结果
     *
     */
    @PostMapping("/qsxx/ecqs")
    @ResponseBody
    @SysLog("签收查询--二次签收")
    public ResponseEntity<?> ecQsfp(@RequestBody String postInvoiceDetail) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);

        JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);
        if (!invoiceDetail.containsKey("zsl") || StringHelper.isBlank(invoiceDetail.getString("zsl"))) {
            return ResponseEntity.ok(R.error("签收发票总数不能为空！"));
        }
        JSONArray detailArray;
        if (!invoiceDetail.containsKey("ids")) {
            return ResponseEntity.ok(R.error("签收发票信息不能为空！"));
        } else {
            detailArray = (JSONArray)invoiceDetail.get("ids");
            if (detailArray == null || detailArray.size() != invoiceDetail.getLongValue("zsl")) {
                return ResponseEntity.ok(R.error("发票数量和总数量不一致！"));
            }
        }

        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());
        pramsMap.put("ids", postInvoiceDetail);

        try {

            return ResponseEntity.ok(R.ok().put("data", signFpqsService.ecQsfp(pramsMap, getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 查询发票全票面信息
     *
     * @param pramsMap
     *            发票代码
     *
     *
     * @return 返回结果
     *
     */
    @RequestMapping("/sgqs/info")
    @ResponseBody
    @SysLog("明细票面信息查询")
    public ResponseEntity<?> pmxxInfo(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        Map<String, String> data = new HashMap<>(8);
        data.put("dbName", getUserInfo().getDbName());
        if (pramsMap.get("invoiceCode") != null && !"".equals(pramsMap.get("invoiceCode"))) {
            data.put("invoiceCode", pramsMap.get("invoiceCode"));
        } else {
            return ResponseEntity.ok(R.error("发票代码不能为空！").put("data", "发票代码不能为空！"));
        }
        if (pramsMap.get("invoiceNo") != null && !"".equals(pramsMap.get("invoiceNo"))) {
            data.put("invoiceNo", pramsMap.get("invoiceNo"));
        } else {
            return ResponseEntity.ok(R.error("发票号码不能为空！").put("data", "发票号码不能为空！"));
        }

        try {
            String msg = signFpqsService.getInvoiceInfo(data);
            if (msg == null) {
                return ResponseEntity.ok(R.error("未获取到发票信息！").put("data", "未获取到发票信息！"));
            } else {
                return ResponseEntity.ok(R.ok().put("data", msg));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 批量取消已签收记录接口
     *
     * @param postInfo
     *            前端传入的待取消信息
     *
     * @return 返回结果
     *
     */
    @PostMapping("/yqsxx/cancle")
    @ResponseBody
    @SysLog("取消签收")
    public ResponseEntity<?> yqsxxCancle(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("userName", getUserName());
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                JSONArray cancleArray = changeInfo.getJSONArray("uuids");
                if (cancleArray == null || cancleArray.size() == 0) {
                    return ResponseEntity.ok(R.error("需要取消签收的记录不能为空！"));
                } else {
                    pramsMap.put("changeInfo", postInfo);
                }
            } else {
                return ResponseEntity.ok(R.error("需要取消签收的记录不能为空！"));
            }

            String msg = signFpqsService.yqsxxCancle(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.ok().put("data", msg));
            } else {
                return ResponseEntity.ok(R.error(msg));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 批量取消已签收记录接口
     *
     * @param postInfo
     *            前端传入的待取消信息
     *
     * @return 返回结果
     *
     */
    @PostMapping("/getRzhYesNo")
    @ResponseBody
    @SysLog("获取认证状态")
    public ResponseEntity<?> getRzhYesNo(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONArray cancleArray = JSONArray.parseArray(postInfo);
                if (cancleArray == null || cancleArray.size() == 0) {
                    return ResponseEntity.ok(R.error("需要取消签收的记录不能为空！"));
                } else {
                    pramsMap.put("uuids", postInfo);
                }
            } else {
                return ResponseEntity.ok(R.error("需要取消签收的记录不能为空！"));
            }

            return ResponseEntity.ok(R.ok().put("data", signFpqsService.getRzhYesNo(pramsMap)));

        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 批量变更业务单元
     *
     * @param postInfo
     *            前端传入的待变更信息
     *
     * @return 返回结果
     *
     */
    @PostMapping("/yqsxx/change/business")
    @ResponseBody
    @SysLog("修改业务单元")
    public ResponseEntity<?> yqsxxChangeBusiness(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("userName", getUserName());
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                String businessCode = changeInfo.getString("businessCode");
                if (StringHelper.isBlank(businessCode)) {
                    return ResponseEntity.ok(R.error("变更的业务单元不能为空！"));
                } else {
                    if (!"99".equals(businessCode)) {
                        List<Business> businessList = getUserInfo().getBusiness();
                        for (Business business : businessList) {
                            if (business.getBusinessCode().equals(businessCode)) {
                                pramsMap.put("businessName", business.getBusinessName());
                            }
                        }
                    } else {
                        businessCode = "";
                    }
                    pramsMap.put("businessCode", businessCode);
                    if (changeInfo.containsKey("ids")) {
                        JSONArray changeArray = changeInfo.getJSONArray("ids");
                        if (changeArray == null || changeArray.size() == 0) {
                            return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                        } else {
                            pramsMap.put("changeInfo", postInfo);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                    }
                }
            } else {
                return ResponseEntity.ok(R.error("需要变更业务单元的记录不能为空！"));
            }

            String msg = signFpqsService.yqsxxChangeBusiness(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                return ResponseEntity.ok(R.ok().put("data", "修改成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error("修改失败"));
        }
    }

    @SysLog("签收查询-导出图片")
    @RequestMapping("/batchDownloadImg")
    public void exportImage(@RequestBody String postInvoice, HttpServletResponse response) {
        try {
            JSONObject invoiceDetail = JSONObject.parseObject(postInvoice);
            // 本地临时文件夹路径
            String timeString = DateUtil.formatDateTime(DateUtil.date());
            String localTempDir = fileService.getLocalPathDefault() + timeString + File.separator;
            // 创建临时文件夹
            File dir = new File(localTempDir);
            dir.mkdirs();

            JSONArray invoiceArray = invoiceDetail.getJSONArray("invoices");

            // 处理每个图片,下载到本地临时文件夹内
            DynamicContextHolder.push(getUserInfo().getDbName() + DbConstant.BUSINESS_READ);
            for (int i = 0; i < invoiceArray.size(); i++) {

                JSONObject object = invoiceArray.getJSONObject(i);
                // 获取图片实体
                TDxInvoiceImg invoiceImg = signInvoiceImgService.selectByUuid(object.getString("scanId"));

                if (null != invoiceImg && !StringUtils.isEmpty(invoiceImg.getImagePath())) {
                    String startFileName =
                        object.get("invoiceNo") + "_" + object.get("invoiceDate") + "_" + object.get("invoiceCode");
                    if (invoiceImg.getImagePath().contains(".zip")) {
                        File file = new File(invoiceImg.getImagePath());
                        String fileName = file.getName();
                        fileService.downFile(invoiceImg.getImagePath(), localTempDir + fileName);
                        File localZipFile = new File(localTempDir + fileName);
                        List<String> zip = FileUtils.unZip(localZipFile, localTempDir);
                        reName(zip.get(0), startFileName + ".jpg");
                        localZipFile.delete();
                    } else {
                        try {
                            fileService.downFile(invoiceImg.getImagePath(), localTempDir + startFileName + ".jpg");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }
            }

            // 打包
            dir = new File(localTempDir);
            File[] files = dir.listFiles();

            String zipName = localTempDir + "exportImgs_" + timeString + ".zip";
            if (files != null) {

                FileZip.zipFiles(files, new File(zipName));
            }

            // 下载
            File zipFile = new File(zipName);
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(zipFile.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename="
                + new String(zipFile.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            toClient.write(buffer);
            toClient.flush();
            toClient.close();

            // 删除临时文件夹
            FileUtil.deleteDirectory(localTempDir);
        } catch (Exception e) {
            log.error("下载图片出错:" + e);
            e.printStackTrace();
        }
    }

    /**
     * 文件重命名
     *
     * @param path
     *            路径
     * @param newname
     *            文件名
     * @return 返回结果
     */
    public boolean reName(String path, String newname) {// 文件重命名
        File file = new File(path);
        if (file.exists()) {
            // 创建新名字的抽象文件
            File newfile = new File(file.getParent() + "/" + newname);
            if (file.renameTo(newfile)) {
                log.info("重命名成功！");
                return true;
            } else {
                log.info("重命名失败！新文件名已存在");
                return false;
            }
        } else {
            log.info("重命名文件不存在！");
            return false;
        }

    }

    /**
     * 添加备注
     *
     * @param postInfo
     *            前端传入的待变更信息
     *
     * @return 返回结果
     *
     */
    @PostMapping("/yqsxx/addRemark")
    @ResponseBody
    @SysLog("签收查询--添加备注")
    public ResponseEntity<?> yqsxxAddRemark(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                String remark = changeInfo.getString("remark");
                if (StringHelper.isBlank(remark)) {
                    return ResponseEntity.ok(R.error("备注不能为空！"));
                } else {
                    pramsMap.put("remark", remark);
                    if (changeInfo.containsKey("ids")) {
                        JSONArray changeArray = changeInfo.getJSONArray("ids");
                        if (changeArray == null || changeArray.size() == 0) {
                            return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                        } else {
                            pramsMap.put("changeInfo", postInfo);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                    }
                }
            } else {
                return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
            }

            String msg = signFpqsService.yqsxxAddRemark(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                return ResponseEntity.ok(R.ok().put("data", "添加成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error("添加失败"));
        }
    }

    @PostMapping("/getRecord")
    @ResponseBody
    @SysLog("一键同步获取底账信息")
    public ResponseEntity<?> getRecord(@RequestBody String uuid) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            pramsMap.put("uuid", uuid);

            return ResponseEntity.ok(R.ok().put("data", signFpqsService.getRecord(pramsMap)));

        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error());
        }
    }

    /**
     * 批量删除
     *
     * @return 返回结果
     *
     */
    @PostMapping("/qsxx/batchDel")
    @ResponseBody
    @SysLog("签收查询--批量删除")
    public ResponseEntity<?> batchDel(@RequestBody String postInvoiceDetail) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);

        JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);

        if (!invoiceDetail.containsKey("ids")) {
            return ResponseEntity.ok(R.error("删除发票信息不能为空！"));
        }

        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());
        pramsMap.put("ids", postInvoiceDetail);

        try {

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

    /**
     * 删除列表查询
     *
     * @param pramsMap
     *            请求参数
     * @return 返回结果
     */
    @RequestMapping("/delInvoice/list")
    @ResponseBody
    @SysLog("扫描删除查询")
    public ResponseEntity<?> delList(@RequestBody Map<String, Object> pramsMap) {
        // 入参统一在入口处理
        Map<String, Object> data = new HashMap<>(18);

        data.put("userId", getUserId());
        data.put("dbName", getUserInfo().getDbName());
        data.put("gfsh", pramsMap.get("gfsh"));
        data.put("invoiceNo", pramsMap.get("invoiceNo"));
        data.put("xfsh", pramsMap.get("xfsh"));
        data.put("company", pramsMap.get("company"));
        data.put("menuId", pramsMap.get("menuId"));
        if (pramsMap.get("createDateBegin") != null && !"".equals(pramsMap.get("createDateBegin"))) {
            data.put("createDateBegin",
                DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("createDateBegin").toString())));
        }
        if (pramsMap.get("createDateEnd") != null && !"".equals(pramsMap.get("createDateEnd"))) {
            data.put("createDateEnd",
                DateUtils.getNight(DateUtils.strToDate(pramsMap.get("createDateEnd").toString())));
        }

        if (pramsMap.get("invoiceType") != null && !"".equals(pramsMap.get("invoiceType"))
            && !"99".equals(pramsMap.get("invoiceType"))) {
            data.put("invoiceType", pramsMap.get("invoiceType"));
        }

        if (pramsMap.get("qsType") != null && !"".equals(pramsMap.get("qsType"))
            && !"99".equals(pramsMap.get("qsType"))) {
            data.put("qsType", pramsMap.get("qsType"));
        }

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", signFpqsService.selectDelfpList(data, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 强制签收
     *
     * @param pramsMap
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/forceSign")
    @ResponseBody
    @SysLog("编辑--强制签收")
    public ResponseEntity<?> forceSign(@RequestBody Map<String, String> pramsMap) {

        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());

        if (!pramsMap.get("smid").isEmpty()) {
            pramsMap.put("scanId", pramsMap.get("smid"));
        } else {
            return ResponseEntity.ok(R.error("记录唯一标识不能为空！"));
        }

        if (pramsMap.get("invoiceCode").isEmpty()) {

            return ResponseEntity.ok(R.error("发票代码不能为空！"));
        }

        if (pramsMap.get("invoiceNo").isEmpty()) {

            return ResponseEntity.ok(R.error("发票号码不能为空！"));
        }

        if (pramsMap.get("invoiceDate").isEmpty()) {

            return ResponseEntity.ok(R.error("开票日期不能为空！"));
        }

        if (pramsMap.get("gfsh") != null && pramsMap.get("gfsh").isEmpty()) {

            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }

        if (pramsMap.get("xfsh") != null && pramsMap.get("xfsh").isEmpty()) {

            return ResponseEntity.ok(R.error("销方税号不能为空！"));
        }

        if (pramsMap.get("xfmc") != null && pramsMap.get("xfmc").isEmpty()) {

            return ResponseEntity.ok(R.error("销方名称不能为空！"));
        }

        InvoiceUtil iu = new InvoiceUtil(pramsMap.get("invoiceCode"));
        String invoiceType = iu.getFplxdm();

        if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.JDC.getFplxDm().equals(invoiceType)
            || FplxEnum.ESC.getFplxDm().equals(invoiceType)) {
            if (pramsMap.get("invoiceAmount") != null && pramsMap.get("invoiceAmount").isEmpty()) {
                return ResponseEntity.ok(R.error("金额不能为空！"));
            }
        } else {
            if (pramsMap.get("checkCode") != null && pramsMap.get("checkCode").isEmpty()) {
                return ResponseEntity.ok(R.error("校验码不能为空！"));
            }
        }

        try {

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

    @RequestMapping("/qsxx/listMRYX")
    @ResponseBody
    @SysLog("每日优鲜发票扫描明细")
    public ResponseEntity<?> listQsfpMryx(@RequestBody Map<String, Object> pramsMap) {
        // 入参统一在入口处理
        Map<String, Object> data = new HashMap<>(16);

        data.put("userName", pramsMap.get("userName"));
        data.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        data.put("gfsh", gfshList);
        data.put("invoiceNo", pramsMap.get("invoiceNo"));
        data.put("invoiceCode", pramsMap.get("invoiceCode"));

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

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

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

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

        if (pramsMap.get("invoiceType") != null && !"".equals(pramsMap.get("invoiceType"))
            && !"99".equals(pramsMap.get("invoiceType"))) {
            data.put("invoiceType", pramsMap.get("invoiceType"));
        }
        if (pramsMap.get("invoiceSource") != null && !"".equals(pramsMap.get("invoiceSource"))
                && !"99".equals(pramsMap.get("invoiceSource"))) {
            data.put("invoiceSource", pramsMap.get("invoiceSource"));
        }
        if (pramsMap.get("qsStatus") != null && !"".equals(pramsMap.get("qsStatus"))
            && !"99".equals(pramsMap.get("qsStatus"))) {
            data.put("qsStatus", pramsMap.get("qsStatus"));
        }

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        data.put("company", pramsMap.get("company"));
        data.put("menuId", pramsMap.get("menuId"));
        try {
            return ResponseEntity.ok(R.ok().put("data", signFpqsService.selectQsfpListMryx(data, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 根据前台传过来的抵账表id的集合进行取消关联
     *
     * @param postInfo
     *            请求参数
     * @return 返回结果
     */
    @SysLog("取消关联")
    @RequestMapping(value = "/cancelRelate", method = RequestMethod.POST)
    public ResponseEntity<?> cancelSign(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                if (changeInfo.containsKey("uuids")) {
                    JSONArray changeArray = changeInfo.getJSONArray("uuids");
                    if (changeArray == null || changeArray.size() == 0) {
                        return ResponseEntity.ok(R.error("需要取消的记录不能为空！"));
                    } else {
                        pramsMap.put("changeInfo", postInfo);
                    }
                } else {
                    return ResponseEntity.ok(R.error("需要取消的记录不能为空！"));
                }
            } else {
                return ResponseEntity.ok(R.error("需要取消更的记录不能为空！"));
            }

            String msg = signFpqsService.cancelRelate(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                return ResponseEntity.ok(R.ok().put("data", "取消成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error("取消失败"));
        }
    }

    /**
     * 修改结算单号
     */
    @SysLog("修改结算单号")
    @RequestMapping("/saveSettle")
    public ResponseEntity<?> saveSettle(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, Object> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                String settlementNo = changeInfo.getString("settlementNo");
                String settleSource = changeInfo.getString("settleSource");
                if (StringHelper.isBlank(settlementNo)) {
                    return ResponseEntity.ok(R.error("结算单号不能为空！"));
                } else if (StringHelper.isBlank(settleSource)) {
                    return ResponseEntity.ok(R.error("结算单来源不能为空！"));
                } else {
                    pramsMap.put("settlementNo", settlementNo);
                    pramsMap.put("settleSource", settleSource);
                    if (changeInfo.containsKey("uuids")) {
                        JSONArray changeArray = changeInfo.getJSONArray("uuids");
                        if (changeArray == null || changeArray.size() == 0) {
                            return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                        } else {
                            pramsMap.put("changeInfo", postInfo);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                    }
                }
            } else {
                return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
            }

            String msg = signFpqsService.saveSettle(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                return ResponseEntity.ok(R.ok().put("data", "修改成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error("修改失败"));
        }
    }

    /**
     * 转移签收人
     */
    @SysLog("转移签收人")
    @RequestMapping("/updateUser")
    public ResponseEntity<?> transfer(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, Object> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                String changeUserId = changeInfo.getString("userId");
                String changeUserName = changeInfo.getString("userName");
                if (StringHelper.isBlank(changeUserId)) {
                    return ResponseEntity.ok(R.error("转移人不能为空！"));
                } else {
                    pramsMap.put("userId", changeUserId);
                    pramsMap.put("userName", changeUserName);
                    if (changeInfo.containsKey("uuids")) {
                        JSONArray changeArray = changeInfo.getJSONArray("uuids");
                        if (changeArray == null || changeArray.size() == 0) {
                            return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                        } else {
                            pramsMap.put("changeInfo", postInfo);
                        }
                    } else {
                        return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                    }
                }
            } else {
                return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
            }

            String msg = signFpqsService.transfer(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                return ResponseEntity.ok(R.ok().put("data", "转移成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error("转移失败"));
        }
    }

    @PostMapping("/getUser")
    @ResponseBody
    @SysLog("获取本机构用户信息")
    public ResponseEntity<?> getUser() {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());
        try {

            return ResponseEntity.ok(R.ok().put("data", signFpqsService.getUser(pramsMap)));

        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error());
        }
    }
}
