package com.dxhy.baseInterface.bigBTab.controller;

import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.bigB.enterpriseInterface.EnterpriseChangeResp;
import com.dxhy.api.model.bigB.enterpriseInterface.EnterpriseDqyChangeReq;
import com.dxhy.api.model.bigB.enterpriseInterface.EnterpriseLqSyChangeReq;
import com.dxhy.api.model.bigB.enterpriseInterface.EnterpriseLqZlChangeReq;
import com.dxhy.baseInterface.bigBTab.service.BigBEnterpriseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 为大B提供的企业信息管理接口
 * @Author wangruwei
 * @Date 2023/7/10 10:09
 * @Version 1.0
 */
@RestController
@RequestMapping("bigBEnterprise")
@Slf4j
public class BigBEnterpriseController {
    @Autowired
    private BigBEnterpriseService bigBEnterpriseService;

    /**
    * @Description 企业信息—RPA企业使用-企业信息变化(增删改)--大企业
    * @param enterpriseChangeReq
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2023/7/10 11:16
    **/
    @RequestMapping(value = "/enterpriseDqyChange", method = RequestMethod.POST)
    public EnterpriseChangeResp enterpriseDqyChange(@RequestBody EnterpriseDqyChangeReq enterpriseChangeReq) {
        if (StringUtils.isEmpty(enterpriseChangeReq.getId())) {
            return getFailRz("id必须传");
        }
        if (StringUtils.isEmpty(enterpriseChangeReq.getNsrsbh())) {
            return getFailRz("纳税人识别号必须传");
        }
        if (StringUtils.isEmpty(enterpriseChangeReq.getNsrmc())) {
            return getFailRz("纳税人名称必须传");
        }
        if (StringUtils.isEmpty(enterpriseChangeReq.getJrfs())) {
            return getFailRz("接入方式必须传");
        }
        if (!"1".equals(enterpriseChangeReq.getJrfs())) {
            return getFailRz("接入方式必须为1(用票)");
        }
        if (
               !("1".equals(enterpriseChangeReq.getType())||
                "2".equals(enterpriseChangeReq.getType())||
                "3".equals(enterpriseChangeReq.getType()))
        ) {
            return getFailRz("变更类型只能为1、2、3其一");
        }

        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (ObjectUtils.isEmpty(enterpriseChangeReq.getLrrq())) {
            //return getFailRz("开始时间必传");
        }else{
            try {
                format.parse(enterpriseChangeReq.getLrrq());
            } catch (ParseException e) {
               log.info("业务处理异常：{}", e);
                return getFailRz("录入格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (StringUtils.isBlank(enterpriseChangeReq.getGxsj())) {
            //return getFailRz("更新时间必传");
        }else{
            try {
                format.parse(enterpriseChangeReq.getGxsj());
            } catch (ParseException e) {
               log.info("业务处理异常：{}", e);
                return getFailRz("更新时间格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }*/
        EnterpriseChangeResp returnResult = null;
        log.info("=========企业信息变更，请求参数进来了===========" , JSON.toJSONString(enterpriseChangeReq));
        returnResult = bigBEnterpriseService.enterpriseDqyChange(enterpriseChangeReq);
        return returnResult;
    }



    /**
     * @Description 直连单位-乐企（增删改）
     * @param enterpriseLqZlChangeReq
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2023/7/10 11:16
     **/
    @RequestMapping(value = "/enterpriseLqZlChange", method = RequestMethod.POST)
    public EnterpriseChangeResp enterpriseLqZlChange(@RequestBody EnterpriseLqZlChangeReq enterpriseLqZlChangeReq) {
        if (StringUtils.isEmpty(enterpriseLqZlChangeReq.getId())) {
            return getFailRz("id必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqZlChangeReq.getTaxpayerCode())) {
            return getFailRz("纳税人识别号必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqZlChangeReq.getCompanyName())) {
            return getFailRz("纳税人名称必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqZlChangeReq.getLqId())) {
            return getFailRz("乐企id必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqZlChangeReq.getApiKey())) {
            return getFailRz("乐企apikey必须传");
        }
        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isBlank(enterpriseLqZlChangeReq.getCreateTime())) {
            //return getFailRz("开始时间必传");
        }else{
            try {
                format.parse(enterpriseLqZlChangeReq.getCreateTime());
            } catch (ParseException e) {
                log.info("业务处理异常：{}", e);
                return getFailRz("创建日期不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (StringUtils.isBlank(enterpriseLqZlChangeReq.getUpdateTime())) {
            //return getFailRz("更新时间必传");
        }else{
            try {
                format.parse(enterpriseLqZlChangeReq.getUpdateTime());
            } catch (ParseException e) {
                log.info("业务处理异常：{}", e);
                return getFailRz("修改日期格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }*/
        if (
                !("1".equals(enterpriseLqZlChangeReq.getType())||
                        "2".equals(enterpriseLqZlChangeReq.getType())||
                        "3".equals(enterpriseLqZlChangeReq.getType()))
        ) {
            return getFailRz("变更类型只能为1、2、3其一");
        }
        return bigBEnterpriseService.enterpriseLqZlChange(enterpriseLqZlChangeReq);
    }

    /**
     * @Description 使用单位-乐企（增删改）
     * @param enterpriseLqSyChangeReq
     * @Return java.lang.Object
     * @Author wangruwei
     * @Date 2023/7/10 11:16
     **/
    @RequestMapping(value = "/enterpriseLqSyChange", method = RequestMethod.POST)
    public EnterpriseChangeResp enterpriseLqSyChange(@RequestBody EnterpriseLqSyChangeReq enterpriseLqSyChangeReq) {
        if (StringUtils.isEmpty(enterpriseLqSyChangeReq.getId())) {
            return getFailRz("id必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqSyChangeReq.getParentId())) {
            return getFailRz("直连单位id必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqSyChangeReq.getTaxpayerCode())) {
            return getFailRz("纳税人识别号必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqSyChangeReq.getCompanyName())) {
            return getFailRz("纳税人名称必须传");
        }
        if (StringUtils.isEmpty(enterpriseLqSyChangeReq.getLqId())) {
            return getFailRz("乐企id必须传");
        }
        /*SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isBlank(enterpriseLqSyChangeReq.getCreateTime())) {
            //return getFailRz("开始时间必传");
        }else{
            try {
                format.parse(enterpriseLqSyChangeReq.getCreateTime());
            } catch (ParseException e) {
                log.info("业务处理异常：{}", e);
                return getFailRz("创建日期不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (StringUtils.isBlank(enterpriseLqSyChangeReq.getUpdateTime())) {
            //return getFailRz("更新时间必传");
        }else{
            try {
                format.parse(enterpriseLqSyChangeReq.getUpdateTime());
            } catch (ParseException e) {
                log.info("业务处理异常：{}", e);
                return getFailRz("修改日期格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }*/
        if (
                !("1".equals(enterpriseLqSyChangeReq.getType())||
                        "2".equals(enterpriseLqSyChangeReq.getType())||
                        "3".equals(enterpriseLqSyChangeReq.getType()))
        ) {
            return getFailRz("变更类型只能为1、2、3其一");
        }
        return bigBEnterpriseService.enterpriseLqSyChange(enterpriseLqSyChangeReq);
    }



    public EnterpriseChangeResp getFailRz(String msg){
        EnterpriseChangeResp resp = new EnterpriseChangeResp();
        resp.setCode("9999");
        resp.setMessage(msg);
        return resp;
    }

}
