package com.dxhy.config.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dxhy.api.model.rz.rzInvoice.RzInvoicePageReq;
import com.dxhy.api.model.rz.rzInvoice.drrq.RzReqItem;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.jxpt.baseService.rzInvoice.service.RzInvoiceService;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @Description 发票入账后台
* @Return
* @Author wangruwei
* @Date 2023/9/21 11:35
**/
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/rzInvoice")
@Slf4j
public class RzInvoiceController extends AbstractController {

    @Autowired
    private RzInvoiceService rzInvoiceService;

    /**
    * @Description 待入账发票获取
    * @Author wangruwei
    * @Date 2023/9/21 16:10
    **/
    @RequestMapping("/preRzInvoiceList")
    @SysLog("待入账发票获取")
    public Object preRzInvoiceList(@RequestBody Map<String, Object> pramsMap) {
        log.info("待入账发票获取。。。");
        //pramsMap.put("business", getUserInfo().getBusiness());
        //pramsMap.put("dbName", getUserInfo().getDbName());
        //pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", "business");

        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);

        //红字锁定状态 lqHzsdbs 未锁定  已锁定
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqHzsdbs"))&&"99".equals(pramsMap.get("lqHzsdbs").toString())){
            pramsMap.put("lqHzsdbs", "");
        }

        //入账管理状态 lqRzglzt 只要有值，此sql就查不出数据   条件 1=2
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzglzt"))&&"99".equals(pramsMap.get("lqRzglzt").toString())){
            pramsMap.put("lqRzglzt", "");
        }

        //入账状态 lqRzzt 未入账，已入账
        /*if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzzt"))&&"99".equals(pramsMap.get("lqRzzt").toString())){
            pramsMap.put("lqRzzt", "");
        }*/

        //入账用途 lqRzyt 只要有值，此sql就查不出数据   条件 1=2
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzyt"))&&"99".equals(pramsMap.get("lqRzyt").toString())){
            pramsMap.put("lqRzyt", "");
        }

        //入账属期 lqRzsq  只要有值，此sql就查不出数据   条件 1=2
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzsq"))&&"99".equals(pramsMap.get("lqRzsq").toString())){
            pramsMap.put("lqRzsq", "");
        }

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

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

        if (pramsMap.get("skssq") != null && !"".equals(pramsMap.get("skssq")) && !"1".equals(pramsMap.get("rzzt"))
                && !"2".equals(pramsMap.get("rzzt")) && !"3".equals(pramsMap.get("rzzt"))) {
            pramsMap.put("skssq", "");
        }
        List<String> l = Arrays.asList("1", "2", "3");
        if (pramsMap.get("rzksrq") != null && !"".equals(pramsMap.get("rzksrq"))
                && l.contains(pramsMap.get("rzzt").toString())) {
            pramsMap.put("rzksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("rzksrq").toString())));
        } else {
            pramsMap.put("rzksrq", "");
        }
        if (pramsMap.get("rzjsrq") != null && !"".equals(pramsMap.get("rzjsrq"))
                && l.contains(pramsMap.get("rzzt").toString())) {
            pramsMap.put("rzjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("rzjsrq").toString())));
        } else {
            pramsMap.put("rzjsrq", "");
        }

        if (pramsMap.get("qsksrq") != null && !"".equals(pramsMap.get("qsksrq")) && "1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsksrq").toString())));
        } else {
            pramsMap.put("qsksrq", "");
        }
        if (pramsMap.get("qsjsrq") != null && !"".equals(pramsMap.get("qsjsrq")) && "1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsjsrq").toString())));
        } else {
            pramsMap.put("qsjsrq", "");
        }
        if (!"1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("businessCode", null);
        } else {
            if ("99".equals(pramsMap.get("qszt"))) {
                pramsMap.put("businessCode", null);
            }
        }

        String invoiceNo = pramsMap.get("invoiceNo").toString();
        if (!StringHelper.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                pramsMap.put("invoiceNo", invoiceNo);
            }else {
                pramsMap.put("eleInvoiceNo", invoiceNo);
                pramsMap.put("invoiceNo", "");
            }
        }

        int curr = (int)pramsMap.get("curr");

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

    /**
    * @Description 发票入账/撤销 申请
    * @Author wangruwei
    * @Date 2023/9/22 10:16
    **/
    @RequestMapping("/rzInvoiceReq")
    @SysLog("发票入账/撤销 申请")
    public Object rzInvoiceReq(@RequestBody RzInvoicePageReq req) {
        if(CollectionUtils.isEmpty(req.getInvoiceList())){
            return ResponseEntity.ok(R.error("请选择发票"));
        }
        if(StringUtil.isNotEmpty(req.getRzsq())){
            req.setRzsq(req.getRzsq().replace("-", ""));
        }
        //0入账  1撤销入账
        String rzlx = req.getRzlx().equals("06") ? "1" : "0";
        boolean result = rzInvoiceService.rzInvoiceReq(req,getUserName());
        if(result){
            if("0".equals(rzlx)){
                return ResponseEntity.ok(R.ok().put("msg", "入账成功"));
            }else{
                return ResponseEntity.ok(R.ok().put("msg", "入账撤销成功"));
            }
        }else{
            if("0".equals(rzlx)){
                return ResponseEntity.ok(R.ok().put("msg", "入账失败").put("code", 500));
            }else{
                return ResponseEntity.ok(R.ok().put("msg", "入账撤销失败").put("code",500 ));
            }
        }

    }

    /**
    * @Description 入账结果查询
    * @Author wangruwei
    * @Date 2023/9/21 16:48
    **/
    @RequestMapping("/rzInvoiceResultList")
    @SysLog("入账结果查询")
    public Object rzInvoiceResultList(@RequestBody Map<String, Object> pramsMap) {
        log.info("入账结果查询");
        //pramsMap.put("business", getUserInfo().getBusiness());
        //pramsMap.put("dbName", getUserInfo().getDbName());
        //pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", "business");

        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);

        //红字锁定状态 lqHzsdbs 未锁定  已锁定
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqHzsdbs"))&&"99".equals(pramsMap.get("lqHzsdbs").toString())){
            pramsMap.put("lqHzsdbs", "");
        }

        //入账管理状态 lqRzglzt
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzglzt"))&&"99".equals(pramsMap.get("lqRzglzt").toString())){
            pramsMap.put("lqRzglzt", "");
        }

        //入账状态 lqRzzt 未入账，已入账
        /*if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzzt"))&&"99".equals(pramsMap.get("lqRzzt").toString())){
            pramsMap.put("lqRzzt", "");
        }*/

        //入账用途 lqRzyt
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzyt"))&&"99".equals(pramsMap.get("lqRzyt").toString())){
            pramsMap.put("lqRzyt", "");
        }

        //入账属期 lqRzsq
        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzsq"))&&"99".equals(pramsMap.get("lqRzsq").toString())){
            pramsMap.put("lqRzsq", "");
        }


        if(ObjectUtil.isNotEmpty(pramsMap.get("lqRzsq"))){
            pramsMap.put("lqRzsq", pramsMap.get("lqRzsq").toString().replace("-", ""));
        }

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

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

        if (pramsMap.get("skssq") != null && !"".equals(pramsMap.get("skssq")) && !"1".equals(pramsMap.get("rzzt"))
                && !"2".equals(pramsMap.get("rzzt")) && !"3".equals(pramsMap.get("rzzt"))) {
            pramsMap.put("skssq", "");
        }
        List<String> l = Arrays.asList("1", "2", "3");
        if (pramsMap.get("rzksrq") != null && !"".equals(pramsMap.get("rzksrq"))
                && l.contains(pramsMap.get("rzzt").toString())) {
            pramsMap.put("rzksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("rzksrq").toString())));
        } else {
            pramsMap.put("rzksrq", "");
        }
        if (pramsMap.get("rzjsrq") != null && !"".equals(pramsMap.get("rzjsrq"))
                && l.contains(pramsMap.get("rzzt").toString())) {
            pramsMap.put("rzjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("rzjsrq").toString())));
        } else {
            pramsMap.put("rzjsrq", "");
        }

        if (pramsMap.get("qsksrq") != null && !"".equals(pramsMap.get("qsksrq")) && "1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsksrq").toString())));
        } else {
            pramsMap.put("qsksrq", "");
        }
        if (pramsMap.get("qsjsrq") != null && !"".equals(pramsMap.get("qsjsrq")) && "1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsjsrq").toString())));
        } else {
            pramsMap.put("qsjsrq", "");
        }
        if (!"1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("businessCode", null);
        } else {
            if ("99".equals(pramsMap.get("qszt"))) {
                pramsMap.put("businessCode", null);
            }
        }

        String invoiceNo = pramsMap.get("invoiceNo").toString();
        if (!StringHelper.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                pramsMap.put("invoiceNo", invoiceNo);
            }else {
                pramsMap.put("eleInvoiceNo", invoiceNo);
                pramsMap.put("invoiceNo", "");
            }
        }

        int curr = (int)pramsMap.get("curr");

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

    /**
    * @Description 导入入账
    * @param reqList
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/6/4 19:53
    **/
    @RequestMapping("/importRzInvoice")
    @SysLog("导入入账")
    public ResponseEntity<R> importRzInvoice(@RequestBody List<RzReqItem> reqList) {
        if(reqList.size()==0){
            return ResponseEntity.ok(R.error("未检测到数据"));
        }
        List<String> gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        try {
            return rzInvoiceService.importRzInvoice(reqList,gfshList,getUserName());
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error("业务处理异常"));
        }
    }
}
