package com.dxhy.config.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dxhy.api.model.cpy.pageImport.CpyImportInvioceReq;
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.R;
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("/cpyAuthInvoice")
@Slf4j
public class CpyAuthInvoiceController extends AbstractController {

    @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", cpyAuthInvoiceService.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", cpyAuthInvoiceService.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("/selInvoiceCount")
    @SysLog("成品油状态-选中发票提示勾选总吨数")
    public Object selInvoiceCount(@RequestBody Map<String, String> pramsMap) {
        String uuids = pramsMap.get("uuids");
        List<String> uuidList = Arrays.asList(uuids.split(","));
        try {
            return ResponseEntity.ok(R.ok().put("data", cpyAuthInvoiceService.selInvoiceCount(uuidList)));
        } 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("/cpyGxByUuids")
    @SysLog("成品油批量勾选入库接口")
    public Object cpyGxByUuids(@RequestBody Map<String, String> pramsMap) {
        String uuids = pramsMap.get("uuids");
        List<String> uuidList = Arrays.asList(uuids.split(","));
        try {
            return ResponseEntity.ok(R.ok().put("data", cpyAuthInvoiceService.cpyGxByUuids(uuidList,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 = cpyAuthInvoiceService.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", cpyAuthInvoiceService.cpyGxBySelCondition(pramsMap,getUserInfo())));
        } catch (Exception e) {
            log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
    * @Description 成品油导入勾选-发票
    * @param cpyImportInvioceReqList
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2024/7/17 10:43
    **/
    @RequestMapping("/cpyImportInvoice")
    @SysLog("成品油导入勾选发票")
    public ResponseEntity<R> cpyImportInvoice(@RequestBody List<CpyImportInvioceReq> cpyImportInvioceReqList) {
        if(cpyImportInvioceReqList.size()==0){
            return ResponseEntity.ok(R.error("未检测到数据"));
        }
        try {
            return cpyAuthInvoiceService.cpyImportInvoice(cpyImportInvioceReqList,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);
        //发票号码
        String fphm = pramsMap.get("fphm").toString();
        if (!StringHelper.isBlank(fphm)) {
            if(fphm.length()==8){
                pramsMap.put("fphm", fphm);
            }else {
                pramsMap.put("fphm", "");
                pramsMap.put("eleInvoiceNo", fphm);
            }
        }
        //开票日期起 kpksrq
        if(ObjectUtil.isNotEmpty(pramsMap.get("kpksrq"))){
            pramsMap.put("kpksrq", pramsMap.get("kpksrq").toString()+" 00:00:00");
        }
        //开票日期止 kpjsrq
        if(ObjectUtil.isNotEmpty(pramsMap.get("kpjsrq"))){
            pramsMap.put("kpjsrq", pramsMap.get("kpjsrq").toString()+" 23:59:59");
        }
        //发票类型 invoiceTypes
        if(ObjectUtil.isNotEmpty(pramsMap.get("invoiceTypes"))){
            pramsMap.put("invoiceTypes", pramsMap.get("invoiceTypes").toString().split(","));
        }else{
            pramsMap.put("invoiceTypes", Arrays.asList("01","08","31","185"));
        }
        //发票状态 fpzts
        if(ObjectUtil.isNotEmpty(pramsMap.get("fpzts"))){
            pramsMap.put("fpzts", pramsMap.get("fpzts").toString().split(","));
        }
        //发票风险等级 glzts
        if(ObjectUtil.isNotEmpty(pramsMap.get("glzts"))){
            pramsMap.put("glzts", pramsMap.get("glzts").toString().split(","));
        }
        //红字锁定 hzsdbs
        if(ObjectUtil.isNotEmpty(pramsMap.get("hzsdbs"))&&!"99".equals(pramsMap.get("hzsdbs"))){
            pramsMap.put("hzsdbs", pramsMap.get("hzsdbs").toString());
        }else{
            pramsMap.put("hzsdbs", "");
        }
        //成品油异常标识 cpyycbs
        if(ObjectUtil.isNotEmpty(pramsMap.get("cpyycbs"))&&!"99".equals(pramsMap.get("cpyycbs"))){
            pramsMap.put("cpyycbs", pramsMap.get("cpyycbs").toString());
        }else{
            pramsMap.put("cpyycbs", "");
        }

        return pramsMap;
    }




}
