package com.dxhy.baseInterface.bigBTab.service.impl;

import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.bigB.tabInterface.*;
import com.dxhy.baseInterface.bigBTab.service.BigBTabService;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.util.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
* @Description 对大B接口业务层
* @Return
* @Author wangruwei
* @Date 2023/7/10 11:03
**/
@Service("bigBtabService")
@Slf4j
public class BigBTabServiceImpl implements BigBTabService {

    @Autowired(required = false)
    private com.dxhy.baseInterface.bigBTab.dao.BigBTabDao bigBTabDao;

    @Resource
    private DictdetaServcice dictdetaServcice;

    /**
    * @Description 认证统计，进项税转出统计
    * @param rzTjTabReq
    * @Return com.dxhy.api.model.bigB.tabInterface.RzTjTabResp
    * @Author wangruwei
    * @Date 2023/7/10 11:26
    **/
    @Override
    public RzTjTabResp queryRzTabInfo(RzTjTabReq rzTjTabReq) {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        log.info("已认证抵扣税额");
        rzTjTabReq.setRzhYesorno("1");
        List<Map<String,Object>> byyrzdkse = bigBTabDao.countRzSe(rzTjTabReq);
        log.info("已认证不抵扣税额");
        rzTjTabReq.setRzhYesorno("2");
        List<Map<String,Object>> byyrzbdkse = bigBTabDao.countRzSe(rzTjTabReq);
        log.info("进项税额转出金额税额");
        List<Map<String,Object>> jxszcMap = bigBTabDao.countJxszc(rzTjTabReq);
        //异常发票数量     和异常发票预警条件一样 list/listAnomalyInvoice
        log.info("异常发票数量");
        List<Map<String,Object>> byycfpsl = bigBTabDao.countYcfpsl(rzTjTabReq);

        RzTjTabResp rzTjTabResp = new RzTjTabResp();
        rzTjTabResp.setCode("0000");

        List<RzTjTabRespData> listTj = new LinkedList();
        for (int i = 1; i < 13; i++) {
            RzTjTabRespData rzTjTabRespData = new RzTjTabRespData();
            rzTjTabRespData.setMonthStr(changeMonthStr(rzTjTabReq.getStartDate(),i));
            rzTjTabRespData.setByyrzdkse(getResult(byyrzdkse,i+""));
            rzTjTabRespData.setByyrzdksl(getResultNum(byyrzdkse,i+""));
            rzTjTabRespData.setByyrzbdkse(getResult(byyrzbdkse,i+""));
            rzTjTabRespData.setByjxsezce(getResult(jxszcMap,i+""));
            rzTjTabRespData.setByycfpsl(getResult(byycfpsl,i+""));

            listTj.add(rzTjTabRespData);
        }
        rzTjTabResp.setListTj(listTj);
        /*RzTjTabRespData rzTjTabRespData = new RzTjTabRespData();
        rzTjTabRespData.setStartDate(rzTjTabReq.getStartDate());
        rzTjTabRespData.setEndDate(rzTjTabReq.getEndDate());
        rzTjTabRespData.setSystemType(rzTjTabReq.getSystemType());
        rzTjTabRespData.setByyrzdkse(byyrzdkse);
        rzTjTabRespData.setByyrzbdkse(byyrzbdkse);
        rzTjTabRespData.setByjxsezce(jxszcMap);
        rzTjTabRespData.setByycfpsl(byycfpsl);

        rzTjTabResp.setData(rzTjTabRespData);*/
        rzTjTabResp.setMessage("获取成功");
        log.info("大Btab接口认证统计，进项税转出统计：{}", JSON.toJSONString(rzTjTabResp));
        return rzTjTabResp;
    }

    /**
     * 获取数量
     **/
    private String getResultNum(List<Map<String, Object>> byyrzdkse, String i) {
        String result = "";
        List<Map<String, Object>> monthList = byyrzdkse.stream().filter(e -> i.equals(e.get("monthStr").toString())).collect(Collectors.toList());
        if(monthList.size()>0){
            result = monthList.get(0).get("num").toString();
        }else{
            result = "0";
        }
        return result;
    }

    private String getResult(List<Map<String, Object>> byyrzdkse, String i) {
        String result = "";
        List<Map<String, Object>> monthList = byyrzdkse.stream().filter(e -> i.equals(e.get("monthStr").toString())).collect(Collectors.toList());
        if(monthList.size()>0){
            result = monthList.get(0).get("total").toString();
        }else{
            result = "0";
        }
        return result;
    }

    public static void main(String[] args) {
        List<Map<String, Object>> byyrzdkse = new LinkedList<>();

    }

    /**
    * @Description 格式化年月
    * @Author wangruwei
    * @Date 2023/9/22 17:22
    **/
    private String changeMonthStr(String startDate, int i) {
        String result = "";
        String year = startDate.substring(0, 4);
        if(i>0&&i<10){
            result = year+"-0"+i;
        }else{
            result = year+"-"+i;
        }
        return result;
    }


    /**
    * @Description 大B异常发票统计
    * @param rzTjTabReq
    * @Return com.dxhy.api.model.bigB.tabInterface.YcFptjTabResp
    * @Author wangruwei
    * @Date 2023/7/10 15:18
    **/
    @Override
    public YcFptjTabResp queryycTabInfo(RzTjTabReq rzTjTabReq) {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        //异常发票信息，以票种分组
        //去重 deptlist
        List<String> deptList = rzTjTabReq.getDeptList();
        deptList = deptList.stream().distinct().filter(e-> StringUtils.isNotBlank(e)).collect(Collectors.toList());
        rzTjTabReq.setDeptList(deptList);

        List<String> taxplayercodeDeptList = rzTjTabReq.getTaxplayercodeDeptList();
        taxplayercodeDeptList = taxplayercodeDeptList.stream().distinct().filter(e-> StringUtils.isNotBlank(e)).collect(Collectors.toList());
        rzTjTabReq.setTaxplayercodeDeptList(taxplayercodeDeptList);

        List<String> enterpriseNumbersList = rzTjTabReq.getEnterpriseNumbersList();
        enterpriseNumbersList = enterpriseNumbersList.stream().distinct().filter(e-> StringUtils.isNotBlank(e)).collect(Collectors.toList());
        rzTjTabReq.setEnterpriseNumbersList(enterpriseNumbersList);

        List<YcFptjTabRespData> ycfpDataList = bigBTabDao.countYcfpinfo(rzTjTabReq);
        AtomicInteger allSl = new AtomicInteger();
        ycfpDataList.stream().forEach(o -> allSl.addAndGet(Integer.parseInt(o.getFpsl())));
        log.info("本次查询异常发票总数量是：{}",allSl);
        for (YcFptjTabRespData item : ycfpDataList) {
            item.setFpzlmc(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,item.getFpzldm()));
            item.setBfb(MathUtil.divide(item.getFpsl(),allSl.toString(),2));
        }

        YcFptjTabResp ycFptjTabResp = new YcFptjTabResp();
        ycFptjTabResp.setCode("0000");
        ycFptjTabResp.setData(ycfpDataList);
        ycFptjTabResp.setMessage("获取成功");
        log.info("大Btab接口异常发票统计返回数据：{}", JSON.toJSONString(ycFptjTabResp));
        return ycFptjTabResp;
    }
}
