package com.dxhy.baseInterface.bigBTab.controller;

import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.bigB.tabInterface.RzTjTabReq;
import com.dxhy.api.model.bigB.tabInterface.RzTjTabResp;
import com.dxhy.api.model.bigB.tabInterface.YcFptjTabResp;
import com.dxhy.baseInterface.bigBTab.service.BigBTabService;
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;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @ClassName BigBTabController
 * @Description 为大B首页提供的接口
 * @Author wangruwei
 * @Date 2023/7/10 10:09
 * @Version 1.0
 */
@RestController
@RequestMapping("bigBTab")
@Slf4j
public class BigBTabController {
    @Autowired
    private BigBTabService bigBTabService;

    /**
    * @Description 大B认证统计，进项税额转出 统计
    * @param rzTjTabReq
    * @Return java.lang.Object
    * @Author wangruwei
    * @Date 2023/7/10 11:16
    **/
    @RequestMapping(value = "/queryRzTabInfo", method = RequestMethod.POST)
    public RzTjTabResp queryRzTabInfo(@RequestBody RzTjTabReq rzTjTabReq) throws Exception {
        if(!"2".equals(rzTjTabReq.getSystemType())){
            return getFailRz("系统标识必须是进项");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isBlank(rzTjTabReq.getStartDate())) {
            return getFailRz("开始时间必传");
        }else{
            try {
                format.parse(rzTjTabReq.getStartDate());
            } catch (ParseException e) {
               log.info("业务处理异常：{}", e);
                return getFailRz("开始时间格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (StringUtils.isBlank(rzTjTabReq.getEndDate())) {
            return getFailRz("结束时间必传");
        }else{
            try {
                format.parse(rzTjTabReq.getEndDate());
            } catch (ParseException e) {
               log.info("业务处理异常：{}", e);
                return getFailRz("结束时间格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (rzTjTabReq.getTaxplayercodeDeptList().size()==0) {
            return getFailRz("纳税人识别号必传");
        }
        RzTjTabResp returnResult = null;
        log.info("=========大B认证统计，进项税额转出请求参数进来了===========" , JSON.toJSONString(rzTjTabReq));
        returnResult = bigBTabService.queryRzTabInfo(rzTjTabReq);
        return returnResult;
    }


    /**
     * 异常发票数量接口
     *
     * @author 陈刚扬
     * @date 2020/10/30
     * @return java.lang.String
     */
    @RequestMapping(value = "/queryYCTabInfo", method = RequestMethod.POST)
    public YcFptjTabResp queryycTabInfo(@RequestBody RzTjTabReq rzTjTabReq) throws Exception {
        if(!"2".equals(rzTjTabReq.getSystemType())){
            return getFailYcFp("系统标识必须是进项");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isBlank(rzTjTabReq.getStartDate())) {
            return getFailYcFp("开始时间必传");
        }else{
            try {
                format.parse(rzTjTabReq.getStartDate());
            } catch (ParseException e) {
               log.info("业务处理异常：{}", e);
                return getFailYcFp("开始时间格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (StringUtils.isBlank(rzTjTabReq.getEndDate())) {
            return getFailYcFp("结束时间必传");
        }else{
            try {
                format.parse(rzTjTabReq.getEndDate());
            } catch (ParseException e) {
               log.info("业务处理异常：{}", e);
                return getFailYcFp("结束时间格式不正确，需为yyyy-MM-dd HH:mm:ss");
            }
        }
        if (rzTjTabReq.getTaxplayercodeDeptList().size()==0) {
            return getFailYcFp("纳税人识别号必传");
        }
        YcFptjTabResp returnResult = null;
        log.info("=========大B异常发票统计-请求参数进来了===========" , JSON.toJSONString(rzTjTabReq));
        returnResult = bigBTabService.queryycTabInfo(rzTjTabReq);
        return returnResult;
    }


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

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

}
