package com.dxhy.config.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.ncp.ncpInvoice.ncpDkInvoice.NcpDkInvoiceLrReq;
import com.dxhy.api.model.ncp.ncpInvoice.ncpDkInvoice.NcpDkInvoiceReqBatchUpdate;
import com.dxhy.api.model.ncp.ncpInvoice.ncpDkInvoice.NcpDkInvoiceReqExcel;
import com.dxhy.api.model.ncp.ncpInvoice.ncpXsInvoice.NcpXsQrInvoice;
import com.dxhy.api.model.ncp.ncpInvoice.ncpXsInvoice.NcpXsQrInvoiceReq;
import com.dxhy.api.model.ncp.ncpInvoice.ncpXsInvoice.NcpXsQrInvoiceReqExcel;
import com.dxhy.api.model.ncp.ncpInvoice.ncpXsInvoice.NcpXsQrInvoiceReqExcelSave;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.enums.NcpQrlxEnum;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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("/ncpInvoice")
@Slf4j
public class NcpInvoiceController extends AbstractController {

    @Autowired
    private NcpInvoiceService ncpInvoiceService;

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

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

        //查询界面，区分是销售普票 还是 3%专票  代开农产品发票
        //销售普票   0
        //3%专票     1
        //代开农产品发票     2
        if (pramsMap.get("searchPageType") != null && !"".equals(pramsMap.get("searchPageType"))) {
            pramsMap.put("searchPageType", pramsMap.get("searchPageType"));
        }else{
            return ResponseEntity.ok(R.error("查询页类型未传"));
        }


        //申请确认状态
        if(ObjectUtil.isNotEmpty(pramsMap.get("dqsqqrclzt"))&&"99".equals(pramsMap.get("dqsqqrclzt").toString())){
            pramsMap.put("dqsqqrclzt", "");
        }
        //申请确认类型
        if(ObjectUtil.isNotEmpty(pramsMap.get("ncpfpcllx"))&&"99".equals(pramsMap.get("ncpfpcllx").toString())){
            pramsMap.put("ncpfpcllx", "");
        }
        //发票确认状态
        if(ObjectUtil.isNotEmpty(pramsMap.get("jdqrzt"))&&"99".equals(pramsMap.get("jdqrzt").toString())){
            pramsMap.put("jdqrzt", "");
        }
        //发票确认类型
        if(ObjectUtil.isNotEmpty(pramsMap.get("jdqrlx"))&&"99".equals(pramsMap.get("jdqrlx").toString())){
            pramsMap.put("jdqrlx", "");
        }

        //发票 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())));
        }
        //确认时间
        if (pramsMap.get("ncpQrsjKs") != null && !"".equals(pramsMap.get("ncpQrsjKs"))) {
            pramsMap.put("ncpQrsjKs", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("ncpQrsjKs").toString())));
        }

        if (pramsMap.get("ncpQrsjJs") != null && !"".equals(pramsMap.get("ncpQrsjJs"))) {
            pramsMap.put("ncpQrsjJs", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("ncpQrsjJs").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", ncpInvoiceService.ncpXsInvoice(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月9日16:57:06
     **/
    @RequestMapping("/ncpXsInvoiceQr")
    @SysLog("农产品销售发票确认")
    public Object ncpXsInvoiceQr(@RequestBody NcpXsQrInvoiceReq req) {
        log.info("农产品销售发票确认");
        String type = req.getType();
        if(ObjectUtil.isEmpty(type)){
            return ResponseEntity.ok(R.error("请选择确认类型"));
        }
        if(NcpQrlxEnum.NCP_QR_TYPE_QBZC_13.getKey().equals(type)||NcpQrlxEnum.NCP_QR_TYPE_BSYZC_14.getKey().equals(type)
                ||NcpQrlxEnum.NCP_QR_TYPE_BFSY_15.getKey().equals(type)||NcpQrlxEnum.NCP_QR_TYPE_JE_KCL_16.getKey().equals(type)||NcpQrlxEnum.NCP_QR_TYPE_PMSE_17.getKey().equals(type)){
            List<NcpXsQrInvoice> invoiceList = req.getInvoiceList();
            if(invoiceList.size()>0){
                String jsonStr = ncpInvoiceService.ncpXsInvoiceQr(req,getUserInfo());

                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", "发票确认成功"));
                }

            }else{
                return ResponseEntity.ok(R.error("请选择发票"));
            }
        }else{
            return ResponseEntity.ok(R.error("没有此类型"));
        }

    }

    /**
    * @Description 农产品销售发票确认-excel导入-展示给前端数据
    * @Author wangruwei
    * @Date 2023/10/11 9:29
    **/
    @RequestMapping("/ncpXsInvoiceQrExcel")
    @SysLog("农产品销售发票确认-导入")
    public Object ncpXsInvoiceQrExcel(@RequestBody List<NcpXsQrInvoiceReqExcel> reqExcelList) {
        log.info("农产品销售发票确认-导入，数据:{}", JSON.toJSONString(reqExcelList));
        if(reqExcelList.size()==0){
            return ResponseEntity.ok(R.error("请填写发票信息"));
        }

        Object jsonStr = ncpInvoiceService.ncpXsInvoiceQrExcel(reqExcelList,UserInfoUtil.getGfshAll(getUserInfo().getOrg()));
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr.toString()));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }

    /**
     * @Description 农产品销售发票确认-excel导入-入库
     * @Author wangruwei
     * @Date 2023/10/11 9:29
     **/
    @RequestMapping("/ncpXsInvoiceQrExcelSave")
    @SysLog("农产品销售发票确认-入库")
    public Object ncpXsInvoiceQrExcelSave(@RequestBody List<NcpXsQrInvoiceReqExcelSave> reqExcelList) {
        log.info("农产品销售发票确认-入库,数据：{}",JSON.toJSONString(reqExcelList));
        if(reqExcelList.size()==0){
            return ResponseEntity.ok(R.error("请选择发票信息"));
        }
        Object jsonStr = ncpInvoiceService.ncpXsInvoiceQrExcelSave(reqExcelList,UserInfoUtil.getGfshAll(getUserInfo().getOrg()),getUserInfo());
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr.toString()));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("msg", jsonObject.getString("msg")));
        }
    }




    //----------------------------------------农产品3%专票导入excel------------------------------------------

    /**
     * @Description 农产品3%专票确认-excel导入-展示给前端数据
     * @Author wangruwei
     * @Date 2023/10/11 9:29
     **/
    @RequestMapping("/ncpZpInvoiceQrExcel")
    @SysLog("农产品3%专票确认-导入")
    public Object ncpZpInvoiceQrExcel(@RequestBody List<NcpXsQrInvoiceReqExcel> reqExcelList) {
        log.info("农产品3%专票确认-导入，数据:{}", JSON.toJSONString(reqExcelList));
        if(reqExcelList.size()==0){
            return ResponseEntity.ok(R.error("请填写发票信息"));
        }

        Object jsonStr = ncpInvoiceService.ncpZpInvoiceQrExcel(reqExcelList,UserInfoUtil.getGfshAll(getUserInfo().getOrg()));
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr.toString()));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }


    //--------------------------------------------代开农产品部分-----------------------------------------------
    /**
     * @Description 通过代码号码找到底账表数据
     * @Author wangruwei
     * @Date 2023/10/13 14:55
     **/
    @RequestMapping("/selRecordInvoiceByDmHm")
    public Object selRecordInvoiceByDmHm(@RequestBody Map<String, String> pramsMap) {
        if(StringUtils.isEmpty(pramsMap.get("fphm"))){
            return ResponseEntity.ok(R.error("发票号码必须传"));
        }
        JSONObject jsonObject = ncpInvoiceService.selRecordInvoiceByDmHm(pramsMap);
        if("0000".equals(jsonObject.getString("code"))){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }

    /**
    * @Description 代开农产品录入保存
    * @Author wangruwei
    * @Date 2023/10/13 15:44
    **/
    @RequestMapping("/dkNcpInvoiceLr")
    @SysLog("代开农产品录入")
    public Object dkNcpInvoiceLr(@RequestBody List<NcpDkInvoiceLrReq> list) {
        if(list.size()==0){
            return ResponseEntity.ok(R.error("录入信息必传"));
        }
        JSONObject jsonObject = ncpInvoiceService.dkNcpInvoiceLr(list,UserInfoUtil.getGfshAll(getUserInfo().getOrg()),getUserInfo());
        if("0000".equals(jsonObject.getString("code"))){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }

    /**
     * @Description 代开农产品确认-excel导入-展示给前端数据
     * @Author wangruwei
     * @Date 2023/10/11 9:29
     **/
    @RequestMapping("/ncpDkInvoiceExcel")
    @SysLog("代开农产品确认-excel导入")
    public Object ncpDkInvoiceExcel(@RequestBody List<NcpDkInvoiceReqExcel> reqExcelList) {
        log.info("代开农产品确认-excel导入，数据:{}", JSON.toJSONString(reqExcelList));
        if(reqExcelList.size()==0){
            return ResponseEntity.ok(R.error("请填写发票信息"));
        }

        Object jsonStr = ncpInvoiceService.ncpDkInvoiceExcel(reqExcelList,UserInfoUtil.getGfshAll(getUserInfo().getOrg()));
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr.toString()));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }

    /**
    * @Description 代开农产品批量修改
    * @Author wangruwei
    * @Date 2023/10/16 11:29
    **/
    @RequestMapping("/ncpDkInvoiceBatchUpdate")
    @SysLog("代开农产品批量修改")
    public Object ncpDkInvoiceBatchUpdate(@RequestBody List<NcpDkInvoiceReqExcel> reqExcelList) {
        log.info("代开农产品批量修改，数据:{}", JSON.toJSONString(reqExcelList));
        if(reqExcelList.size()==0){
            return ResponseEntity.ok(R.error("请填写发票信息"));
        }

        Object jsonStr = ncpInvoiceService.ncpDkInvoiceBatchUpdate(reqExcelList,UserInfoUtil.getGfshAll(getUserInfo().getOrg()),getUserInfo());
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr.toString()));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }


    @RequestMapping("/ncpDkInvoiceBatchDel")
    @SysLog("代开农产品批量删除")
    public Object ncpDkInvoiceBatchDel(@RequestBody List<NcpDkInvoiceReqExcel> reqExcelList) {
        log.info("代开农产品批量删除，数据:{}", JSON.toJSONString(reqExcelList));
        if(reqExcelList.size()==0){
            return ResponseEntity.ok(R.error("请填写发票信息"));
        }

        Object jsonStr = ncpInvoiceService.ncpDkInvoiceBatchDel(reqExcelList,UserInfoUtil.getGfshAll(getUserInfo().getOrg()));
        JSONObject jsonObject = JSONObject.parseObject(Base64.decodeStr(jsonStr.toString()));
        String code = jsonObject.getString("code");
        if("0000".equals(code)){
            return ResponseEntity.ok(R.ok().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonObject).put("msg", jsonObject.getString("msg")));
        }
    }

}
