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.ncpJjkcInvoice.NcpJjkcSqReq;
import com.dxhy.api.model.ncp.ncpJjkcInvoice.NcpJjkcSqReqItem;
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.ncpInvoice.service.NcpInvoiceService;
import com.dxhy.jxpt.baseService.ncpInvoice.service.NcpJjkcInvoiceService;
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 农产品加计扣除管理类
* @Return
* @Author wangruwei
* @Date 2023/9/25 11:35
**/
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/ncpJjkcInvoice")
@Slf4j
public class NcpJjkcInvoiceController extends AbstractController {

    @Autowired
    private NcpJjkcInvoiceService ncpJjkcInvoiceService;

    @Autowired
    private NcpInvoiceService ncpInvoiceService;

    /**
     * @Description 农产品加计扣除发票列表
     * @Author wangruwei
     * @Date 2023年10月17日16:57:06
     **/
    @RequestMapping("/ncpJjkcInvoice")
    @SysLog("农产品加计扣除发票列表")
    public Object ncpJjkcInvoice(@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());
            gfshList = ncpInvoiceService.getGfshNcpJjkc(gfshList);
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }

        }
        pramsMap.put("gfsh", gfshList);

        //发票 uuids
        if(ObjectUtil.isNotEmpty(pramsMap.get("uuids"))){
            pramsMap.put("uuids", Arrays.asList(pramsMap.get("uuids").toString().split(",")));
        }
        //开票日期
        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())));
        }

        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", ncpJjkcInvoiceService.ncpJjkcInvoice(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("/ncpJjkcSq")
    @SysLog("农产品加计扣除申请")
    public Object ncpJjkcSq(@RequestBody NcpJjkcSqReq 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.getInvoiceList())||req.getInvoiceList().size()==0){
            return ResponseEntity.ok(R.error("请选择发票"));
        }

        String jsonStr = ncpJjkcInvoiceService.ncpJjkcSq(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("/ncpJjkcResultPage")
    @SysLog("农产品加计扣除结果查询")
    public Object ncpJjkcResultPage(@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());
            gfshList = ncpInvoiceService.getGfshNcpJjkc(gfshList);
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }

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


        //发票 uuids
        if(ObjectUtil.isNotEmpty(pramsMap.get("uuids"))){
            pramsMap.put("uuids", Arrays.asList(pramsMap.get("uuids").toString().split(",")));
        }
        //开票 时间
        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())));
        }

        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", ncpJjkcInvoiceService.ncpJjkcResultPage(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("/ncpJjkcCx")
    @SysLog("农产品撤销加计")
    public Object ncpJjkcCx(@RequestBody List<NcpJjkcSqReqItem> invoiceList) {
        log.info("农产品撤销加计");
        if(ObjectUtil.isEmpty(invoiceList)||invoiceList.size()==0){
            return ResponseEntity.ok(R.error("请选择发票"));
        }

        String jsonStr = ncpJjkcInvoiceService.ncpJjkcCx(invoiceList,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")));
        }
    }
}
