package com.dxhy.config.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dxhy.api.model.cpy.pageImport.CpyImportCustomReq;
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.cpyAuthInvoice.service.CpyAuthCustomService;
import com.dxhy.jxpt.baseService.cpyAuthInvoice.service.CpyAuthInvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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 成品油缴款书勾选
* @Author wangruwei
* @Date 2024/7/12 10:08
**/
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/cpyAuthCustom")
@Slf4j
public class CpyAuthCustomController extends AbstractController {

    @Autowired
    private CpyAuthCustomService cpyAuthCustomService;

    @Autowired
    private CpyAuthInvoiceService cpyAuthInvoiceService;

    /**
    * @Description 成品油缴款书勾选-页面查询接口
    * @param pramsMap
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/7/12 10:42
    **/
    @RequestMapping("/selectAuthPageList")
    @SysLog("成品油缴款书勾选-页面查询接口")
    public Object selectAuthPageList(@RequestBody Map<String, Object> pramsMap) {
        //购方税号 gfsh
        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", ""));
            }
        }
        if(!Arrays.asList("0","1","2","3","4").contains(pramsMap.get("tabNum"))){
            return ResponseEntity.ok(R.error("没有该页面"));
        }
        pramsMap = selConditionDeal(pramsMap);

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

    /**
     * @Description 成品油状态-页面状态统计接口
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2024/7/12 10:42
     **/
    @RequestMapping("/selectCountStatusPage")
    @SysLog("成品油状态-页面状态统计接口")
    public Object selectCountStatusPage(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("gfshAll", UserInfoUtil.getGfshAll(getUserInfo().getOrg()));
        try {
            return ResponseEntity.ok(R.ok().put("data", cpyAuthCustomService.selectCountStatusPage(pramsMap)));
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
    * @Description 成品油状态-选中缴款书提示勾选总吨数
    * @param
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/7/16 17:09
    **/
    @RequestMapping("/selCustomCount")
    @SysLog("成品油状态-选中缴款书提示勾选总吨数")
    public Object selCustomCount(@RequestBody Map<String, String> pramsMap) {
        String customsCodes = pramsMap.get("customsCodes");
        List<String> customList = Arrays.asList(customsCodes.split(","));
        try {
            return ResponseEntity.ok(R.ok().put("data", cpyAuthCustomService.selCustomCount(customList)));
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
    * @Description 成品油批量勾选入库接口
    * @param pramsMap
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/7/16 14:25
    **/
    @RequestMapping("/cpyGxByCustoms")
    @SysLog("成品油批量勾选入库接口")
    public Object cpyGxByCustoms(@RequestBody Map<String, String> pramsMap) {
        String customsCodes = pramsMap.get("customsCodes");
        List<String> customsList = Arrays.asList(customsCodes.split(","));
        try {
            return ResponseEntity.ok(R.ok().put("data", cpyAuthCustomService.cpyGxByCustoms(customsList,getUserInfo())));
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


    /**
     * @Description 成品油全部勾选入库接口--根据页面查询条件提示勾选总吨数
     * @param
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2024/7/16 17:09
     **/
    @RequestMapping("/cpyGxBySelConditionCount")
    @SysLog("成品油全部勾选入库接口--根据页面查询条件提示勾选总吨数")
    public Object cpyGxBySelConditionCount(@RequestBody Map<String, Object> pramsMap) {
        //购方税号 gfsh
        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", ""));
            }
        }
        if(!Arrays.asList("0","1","2","3","4").contains(pramsMap.get("tabNum"))){
            return ResponseEntity.ok(R.error("没有该页面"));
        }
        pramsMap = selConditionDeal(pramsMap);

        try {
            Map<String, Object> resultMap = cpyAuthCustomService.cpyGxBySelConditionCount(pramsMap);
            if("0000".equals(resultMap.get("code"))){
                return ResponseEntity.ok(R.ok().put("data", resultMap.get("data")));
            }else{
                return ResponseEntity.ok(R.error().put("msg", resultMap.get("msg")));
            }
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


    /**
     * @Description 成品油全部勾选入库接口--根据页面查询条件全部勾选
     * @param pramsMap
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2024/7/16 14:25
     **/
    @RequestMapping("/cpyGxBySelCondition")
    @SysLog("成品油全部勾选入库接口--根据页面查询条件全部勾选")
    public Object cpyGxBySelCondition(@RequestBody Map<String, Object> pramsMap) {
        //购方税号 gfsh
        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", ""));
            }
        }
        if(!Arrays.asList("0","1","2","3","4").contains(pramsMap.get("tabNum"))){
            return ResponseEntity.ok(R.error("没有该页面"));
        }
        pramsMap = selConditionDeal(pramsMap);

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

    /**
    * @Description 成品油导入勾选-缴款书
    * @param cpyImportCustomsReqList
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/7/17 10:43
    **/
    @RequestMapping("/cpyImportCustom")
    @SysLog("成品油导入勾选缴款书")
    public ResponseEntity<R> cpyImportCustom(@RequestBody List<CpyImportCustomReq> cpyImportCustomsReqList) {
        if(cpyImportCustomsReqList.size()==0){
            return ResponseEntity.ok(R.error("未检测到数据"));
        }
        try {
            return cpyAuthCustomService.cpyImportCustom(cpyImportCustomsReqList,getUserInfo(),UserInfoUtil.getGfshAll(getUserInfo().getOrg()) );
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error("业务处理异常"));
        }
    }

    /**
    * @Description 处理勾选条件
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    * @Author wangruwei
    * @Date 2024/7/16 19:23
    **/
    private Map<String, Object> selConditionDeal(Map<String, Object> pramsMap) {
        //tab选择 tabNum 未勾选0  勾选中1  已勾选2  勾选失败3  全部4
        if(ObjectUtil.isNotEmpty(pramsMap.get("tabNum"))){
            pramsMap.put("tabNum", pramsMap.get("tabNum").toString());
        }

        //购方税号 gfsh
        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) {

            }
        }
        pramsMap.put("gfsh", gfshList);
        //缴款书号码 customsCode
        //填发日期起 fillInStartDate
        if (pramsMap.get("fillInStartDate") != null && !"".equals(pramsMap.get("fillInStartDate"))) {
            pramsMap.put("fillInStartDate",
                    DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("fillInStartDate").toString())));
        }
        //填发日期止 fillInEndDate
        if (pramsMap.get("fillInEndDate") != null && !"".equals(pramsMap.get("fillInEndDate"))) {
            pramsMap.put("fillInEndDate",
                    DateUtils.getNight(DateUtils.strToDate(pramsMap.get("fillInEndDate").toString())));
        }

        //重号锁定 sfchsd
        if(ObjectUtil.isNotEmpty(pramsMap.get("sfchsd"))&&!"99".equals(pramsMap.get("sfchsd"))){
            pramsMap.put("sfchsd", pramsMap.get("sfchsd").toString());
        }else{
            pramsMap.put("sfchsd", "");
        }

        return pramsMap;
    }




}
