package com.dxhy.dkdj.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.api.model.dkdj.DkdjAuthExcel;
import com.dxhy.api.model.dkdj.DkdjAuthReq;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.constant.ConfigureConstant;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;
import com.dxhy.dkdj.service.DkdjService;
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;

/**
 * @ClassName DkdjController
 * @Description 代开代缴页面功能
 * @Author wangruwei
 * @Date 2023/11/28 10:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/dkdj")
@Slf4j
public class DkdjController extends AbstractController {

    @Autowired
    private DkdjService dkdjService;
    /**
    * @Description 代扣代缴查询页面
    * @param pramsMap
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2023/11/28 11:38
    **/
    @RequestMapping("/dkdjAuthPage")
    @SysLog("代扣代缴查询列表")
    public Object dkdjAuthPage(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("kjywrsbh")) && pramsMap.get("kjywrsbh") != null && !"".equals(pramsMap.get("kjywrsbh"))) {
            gfshList.add(pramsMap.get("kjywrsbh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }

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

        //填发日期开始
        if(ObjectUtil.isNotEmpty(pramsMap.get("tfrqStart"))){
            pramsMap.put("tfrqStart", pramsMap.get("tfrqStart")+" 00:00:00");
        }
        //填发日期结束
        if(ObjectUtil.isNotEmpty(pramsMap.get("tfrqEnd"))){
            pramsMap.put("tfrqEnd", pramsMap.get("tfrqEnd")+" 23:59:59");
        }

        //查询方式  0待认证   1认证结果查询
        if(ObjectUtil.isNotEmpty(pramsMap.get("searchPageType"))&&("0".equals(pramsMap.get("searchPageType").toString())||"1".equals(pramsMap.get("searchPageType").toString()))){
            pramsMap.put("searchPageType", pramsMap.get("searchPageType"));
        }else{
            return ResponseEntity.ok(R.error("查询页类型未传"));
        }

        //认证类型，如果是99，rzlx字段不限制。  1认证抵扣(rzlx in 01,02)，2认证不抵扣(rzlx in 03,04)
        if(ObjectUtil.isNotEmpty(pramsMap.get("rzlx"))&&"99".equals(pramsMap.get("rzlx").toString())){
            pramsMap.put("rzlx", "");
        }

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

        //不抵扣类型
        if(ObjectUtil.isNotEmpty(pramsMap.get("bdklx"))&&"99".equals(pramsMap.get("bdklx").toString())){
            pramsMap.put("bdklx", "");
        }


        //认证状态
        if(ObjectUtil.isNotEmpty(pramsMap.get("rzzt"))&&"99".equals(pramsMap.get("rzzt").toString())){
            pramsMap.put("rzzt", "");
        }

        //认证状态
        if(ObjectUtil.isNotEmpty(pramsMap.get("rzssq"))){
            pramsMap.put("rzssq", pramsMap.get("rzssq").toString().replace("-", ""));
        }


        int curr = (int)pramsMap.get("curr");

        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", dkdjService.dkdjAuthPage(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 2023/11/28 11:38
     **/
    @RequestMapping("/dkdjAuth")
    @SysLog("代扣代缴认证/认证不抵扣")
    public Object dkdjAuth(@RequestBody DkdjAuthReq dkdjAuthReq) {

        if(!ConfigureConstant.STRING_1.equals(dkdjAuthReq.getRzType())&&!ConfigureConstant.STRING_2.equals(dkdjAuthReq.getRzType())&&!ConfigureConstant.STRING_3.equals(dkdjAuthReq.getRzType())){
            return ResponseEntity.ok(R.error("认证类型不正确"));
        }
        if(dkdjAuthReq.getDkdjwspzhs().size()==0){
            return ResponseEntity.ok(R.error("未选择数据"));
        }
        if(ConfigureConstant.STRING_2.equals(dkdjAuthReq.getRzType())){
            if(!Arrays.asList("1,2,3,4,5".split(",")).contains(dkdjAuthReq.getBdklx())){
                return ResponseEntity.ok(R.error("不抵扣类型不正确"));
            }
            if("5".equals(dkdjAuthReq.getBdklx())&& StringUtils.isEmpty(dkdjAuthReq.getBdkyy())){
                return ResponseEntity.ok(R.error("不抵扣原因是其他时，需填写原因"));
            }
        }

        Object jsonStr =dkdjService.dkdjAuth(dkdjAuthReq,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", jsonStr).put("msg", jsonObject.getString("msg")));
        }else{
            return ResponseEntity.ok(R.error().put("data", jsonStr).put("msg", jsonObject.getString("msg")));
        }
    }

    /**
    * @Description 代扣代缴导入，返回前端展示
    * @param dkdjAuthExcels
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2023/11/29 16:04
    **/
    @RequestMapping("/dkdjExcelShow")
    @SysLog("代扣代缴-excel导入")
    public Object dkdjExcelShow(@RequestBody List<DkdjAuthExcel> dkdjAuthExcels) {
        if(dkdjAuthExcels.size()==0){
            return ResponseEntity.ok(R.error("未检测到数据"));
        }

        Object jsonStr = dkdjService.dkdjExcelShow(dkdjAuthExcels,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")));
        }
    }

    /**
     * @Description 代扣代缴导入的保存，入库
     * @param dkdjAuthExcels
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2023/11/29 16:04
     **/
    @RequestMapping("/dkdjExcelSave")
    @SysLog("代扣代缴-excel导入，认证入库")
    public Object dkdjExcelSave(@RequestBody List<DkdjAuthExcel> dkdjAuthExcels) {
        if(dkdjAuthExcels.size()==0){
            return ResponseEntity.ok(R.error("未检测到数据"));
        }

        Object jsonStr = dkdjService.dkdjExcelSave(dkdjAuthExcels,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")));
        }
    }
}
