package com.dxhy.config.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.api.model.rz.rzCustom.RzCustomPageReq;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.jxpt.baseService.config.service.SysEnterpriseConfigService;
import com.dxhy.jxpt.baseService.rzInvoice.service.RzCustomService;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @Description 海关缴款书入账后台
* @Return
* @Author wangruwei
* @Date 2023/9/25 11:35
**/
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/rzCustom")
@Slf4j
public class RzCustomController extends AbstractController {

    @Autowired
    private RzCustomService rzCustomService;

    @Autowired
    private SysEnterpriseConfigService sysEnterpriseConfigService;


    /**
    * @Description 待入账海关缴款书获取
    * @Author wangruwei
    * @Date 2023/9/21 16:10
    **/
    @RequestMapping("/preRzCustomList")
    @SysLog("待入账海关缴款书获取")
    public Object preRzCustomList(@RequestBody Map<String, Object> pramsMap) {
        log.info("待入账海关缴款书获取");
        pramsMap.put("business", getUserInfo().getBusiness());
        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());
        }
        List<SysEnterpriseConfigEntity> qyList = sysEnterpriseConfigService.selectEnterpriseList(gfshList,"1");
        gfshList = qyList.stream().filter(e -> "1".equals(e.getJrfs())).map(SysEnterpriseConfigEntity::getNsrsbh).collect(Collectors.toList());
        log.info("筛选出乐企的数据进行查询{}",gfshList);
        pramsMap.put("gfsh", gfshList);

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

        //入账管理状态 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 ("1".equals(pramsMap.get("qsStatus"))) {
            if (pramsMap.get("qsDateStartDate") != null && !"".equals(pramsMap.get("qsDateStartDate"))) {
                pramsMap.put("qsDateStartDate",
                        DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsDateStartDate").toString())));
            }
            if (pramsMap.get("qsDateEndDate") != null && !"".equals(pramsMap.get("qsDateEndDate"))) {
                pramsMap.put("qsDateEndDate",
                        DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsDateEndDate").toString())));
            }
            if ("99".equals(pramsMap.get("businessCode"))) {
                pramsMap.put("businessCode", null);
            }
        } else {
            pramsMap.put("qsDateStartDate", null);
            pramsMap.put("qsDateEndDate", null);
            pramsMap.put("businessCode", null);
        }
        if ("1".equals(pramsMap.get("rzhYesorno")) || "2".equals(pramsMap.get("rzhYesorno"))
                || "3".equals(pramsMap.get("rzhYesorno"))) {
            if (pramsMap.get("rzhStartDate") != null && !"".equals(pramsMap.get("rzhStartDate"))) {
                pramsMap.put("rzhStartDate",
                        DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("rzhStartDate").toString())));
            }
            if (pramsMap.get("rzhEndDate") != null && !"".equals(pramsMap.get("rzhEndDate"))) {
                pramsMap.put("rzhEndDate",
                        DateUtils.getNight(DateUtils.strToDate(pramsMap.get("rzhEndDate").toString())));
            }
        } else {
            pramsMap.put("rzhStartDate", null);
            pramsMap.put("rzhEndDate", null);
            pramsMap.put("rzhBelongDate", null);
        }

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", rzCustomService.preRzCustomList(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("/rzCustomReq")
    @SysLog("缴款书入账/撤销 申请")
    public Object rzCustomReq(@RequestBody RzCustomPageReq req) {
        if(CollectionUtils.isEmpty(req.getCustomList())){
            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 = rzCustomService.rzCustomReq(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("/rzCustomResultList")
    @SysLog("缴款书入账结果查询")
    public Object rzCustomResultList(@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", 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());
        }
        List<SysEnterpriseConfigEntity> qyList = sysEnterpriseConfigService.selectEnterpriseList(gfshList,"1");
        gfshList = qyList.stream().filter(e -> "1".equals(e.getJrfs())).map(SysEnterpriseConfigEntity::getNsrsbh).collect(Collectors.toList());
        log.info("筛选出乐企的数据进行查询{}",gfshList);
        pramsMap.put("gfsh", gfshList);

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

        //入账管理状态 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 ("1".equals(pramsMap.get("qsStatus"))) {
            if (pramsMap.get("qsDateStartDate") != null && !"".equals(pramsMap.get("qsDateStartDate"))) {
                pramsMap.put("qsDateStartDate",
                        DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsDateStartDate").toString())));
            }
            if (pramsMap.get("qsDateEndDate") != null && !"".equals(pramsMap.get("qsDateEndDate"))) {
                pramsMap.put("qsDateEndDate",
                        DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsDateEndDate").toString())));
            }
            if ("99".equals(pramsMap.get("businessCode"))) {
                pramsMap.put("businessCode", null);
            }
        } else {
            pramsMap.put("qsDateStartDate", null);
            pramsMap.put("qsDateEndDate", null);
            pramsMap.put("businessCode", null);
        }
        if ("1".equals(pramsMap.get("rzhYesorno")) || "2".equals(pramsMap.get("rzhYesorno"))
                || "3".equals(pramsMap.get("rzhYesorno"))) {
            if (pramsMap.get("rzhStartDate") != null && !"".equals(pramsMap.get("rzhStartDate"))) {
                pramsMap.put("rzhStartDate",
                        DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("rzhStartDate").toString())));
            }
            if (pramsMap.get("rzhEndDate") != null && !"".equals(pramsMap.get("rzhEndDate"))) {
                pramsMap.put("rzhEndDate",
                        DateUtils.getNight(DateUtils.strToDate(pramsMap.get("rzhEndDate").toString())));
            }
        } else {
            pramsMap.put("rzhStartDate", null);
            pramsMap.put("rzhEndDate", null);
            pramsMap.put("rzhBelongDate", null);
        }

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


}
