package com.dxhy.config.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.ncp.ncpCustoms.NcpJjkcCustomsSqReq;
import com.dxhy.api.model.ncp.ncpCustoms.NcpJjkcCustomsSqReqItem;
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.ncpInvoice.service.NcpJjkcCustomsService;
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.List;
import java.util.Map;
import java.util.Objects;

/**
* @Description 农产品-海关缴款书-加计扣除管理类
* @Return
* @Author wangruwei
* @Date 2023/9/25 11:35
**/
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/ncpJjkcCustom")
@Slf4j
public class NcpJjkcCustomController extends AbstractController {

    @Autowired
    private NcpJjkcCustomsService ncpJjkcCustomsService;

    /**
    * @Description 农产品海关缴款书加计扣除发票列表
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2023/10/20 17:55
    **/
    @RequestMapping("/ncpJjkcCustom")
    @SysLog("农产品海关缴款书加计扣除发票列表")
    public Object ncpJjkcCustom(@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());
        }
        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())));
        }


        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", ncpJjkcCustomsService.selectByPramsMap(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年10月17日16:57:06
     **/
    @RequestMapping("/ncpJjkcCustomSq")
    @SysLog("农产品-缴款书加计扣除申请")
    public Object ncpJjkcCustomSq(@RequestBody NcpJjkcCustomsSqReq req) {
        log.info("农产品-缴款书加计扣除申请");
        if(!"0".equals(req.getSqType())&&!"1".equals(req.getSqType())&&!"2".equals(req.getSqType())){
            return ResponseEntity.ok(R.error("没有此类型"));
        }
        if(ObjectUtil.isEmpty(req.getCustomsList())||req.getCustomsList().size()==0){
            return ResponseEntity.ok(R.error("请选择发票"));
        }

        String jsonStr = ncpJjkcCustomsService.ncpJjkcCustomSq(req,getUserName());

        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("msg", "发票确认成功"));
        }else{
            return ResponseEntity.ok(R.error().put("msg", jsonObject.getString("msg")).put("data", jsonObject.get("data")));
        }
    }

    /**
     * @Description 农产品-缴款书加计扣除结果查询
     * @Author wangruwei
     * @Date 2023年10月19日16:57:06
     **/
    @RequestMapping("/ncpJjkcCustomResultPage")
    @SysLog("农产品-缴款书加计扣除结果查询")
    public Object ncpJjkcCustomResultPage(@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());
        }
        pramsMap.put("gfsh", gfshList);

        //加计处理状态
        if(ObjectUtil.isNotEmpty(pramsMap.get("clzt"))&&"99".equals(pramsMap.get("clzt").toString())){
            pramsMap.put("clzt", "");
        }

        //申请类型
        if(ObjectUtil.isNotEmpty(pramsMap.get("sqlx"))&&"99".equals(pramsMap.get("sqlx").toString())){
            pramsMap.put("sqlx", "");
        }

        //加计所属期
        if(ObjectUtil.isEmpty(pramsMap.get("jjssq"))){
            pramsMap.put("jjssq", "");
        }else{
            pramsMap.put("jjssq", pramsMap.get("jjssq").toString().replace("-", ""));
        }


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


        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", ncpJjkcCustomsService.ncpJjkcCustomResultPage(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年10月19日16:57:06
     **/
    @RequestMapping("/ncpJjkcCustomCx")
    @SysLog("农产品--缴款书撤销加计")
    public Object ncpJjkcCustomCx(@RequestBody List<NcpJjkcCustomsSqReqItem> customList) {
        log.info("农产品--缴款书撤销加计");
        if(ObjectUtil.isEmpty(customList)||customList.size()==0){
            return ResponseEntity.ok(R.error("请选择缴款书"));
        }

        String jsonStr = ncpJjkcCustomsService.ncpJjkcCustomCx(customList,getUserName());

        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("msg",jsonObject.get("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("msg", jsonObject.getString("msg")).put("data", jsonObject.get("data")));
        }
    }


}
