/*
package com.dxhy.core.job.service.impl;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.dxhy.common.enums.FplxEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.job.bxvo.*;
import com.dxhy.core.job.dao.EnterpriseControlRulesDao;
import com.dxhy.core.job.entity.TAcOrg;
import com.dxhy.core.job.pojo.*;
import com.dxhy.core.job.service.EnterpriseControlRulesService;
import com.dxhy.core.util.SignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class EnterpriseControlRulesServiceImpl extends ServiceImpl<EnterpriseControlRulesDao, EnterpriseInfoEntity>
        implements EnterpriseControlRulesService {
    @Value("${enterpriseControlRulesTask.url}")
    private String controlRulesUrl;
    @Value("${enterpriseControlRulesTask.ruleCheckCode}")
    private String ruleCheckCode;
    @Value("${enterpriseControlRulesTask.version}")
    private String version;
    @Value("${enterpriseControlRulesTask.appId}")
    private String appId;
    @Value("${enterpriseControlRulesTask.secretId}")
    private String secretId;
    @Value("${enterpriseControlRulesTask.secretKey}")
    private String secretKey;
    @Value("${enterpriseControlRulesTask.vatInvoiceType}")
    private String vatInvoiceType;

    @Override
    public List<TAcOrg> getTaxNo() {
        return super.baseMapper.getTaxNo();
    }

    @Override
    public void vatInvoiceRules(TAcOrg tAcOrg) {
        //根据税号获取需求对接的发票信息数量
        DynamicContextHolder.push(tAcOrg.getDbName() + DbConstant.BUSINESS_READ);
        //获取规则引擎配置文件
        EnterpriseInfoConfigEntity enterpriseInfoConfigEntity = super.baseMapper.selectConfig(tAcOrg.getTaxno(),"1");
        if (null == enterpriseInfoConfigEntity) {
            log.info("税号taxNo:{}没有设置信息", tAcOrg.getTaxno());
            return;
        }
        if (StringUtils.isEmpty(enterpriseInfoConfigEntity.getVatInvoiceType())) {
            log.info("税号taxNo:{}没有需要对接规则引擎的增值税发票类型", tAcOrg.getTaxno());
            return;
        }
        if (0 == enterpriseInfoConfigEntity.getInvoiceSum()) {
            log.info("税号taxNo:{}没有设置对接规则引擎的发票单次推送数量", tAcOrg.getTaxno());
            return;
        }
        String[] invoiceTypes = enterpriseInfoConfigEntity.getVatInvoiceType().split(",");
        //分批次查询所要推送的发票信息,组合需要传输的参数
        Map<String, Object> pramsMap = new HashMap<>(10);
        pramsMap.put("taxNo", tAcOrg.getTaxno());
        pramsMap.put("vatInvoiceType", invoiceTypes);
        //筛选字段
        if (!StringUtils.isEmpty(enterpriseInfoConfigEntity.getInvoiceDate())) {
            pramsMap.put("invoiceDate", enterpriseInfoConfigEntity.getInvoiceDate());
        }
        if (!StringUtils.isEmpty(enterpriseInfoConfigEntity.getCreateDate())) {
            pramsMap.put("createDate", enterpriseInfoConfigEntity.getCreateDate());
        }
        //筛选区间
        if (enterpriseInfoConfigEntity.getSelectMonth()!=0) {
            String lastMonth = getLastMonth(enterpriseInfoConfigEntity.getSelectMonth());
            pramsMap.put("selectMonth", lastMonth);
        }
        int vatInvoiceSum = super.baseMapper.vatInvoiceSum(pramsMap);
        if (vatInvoiceSum == 0) {
            log.info("税号taxNo:{}没有需要对接规则引擎的发票信息", tAcOrg.getTaxno());
            return;
        }

        int invoiceSum = enterpriseInfoConfigEntity.getInvoiceSum();
        //计算需要推送几次规则引擎
        int pushSum = 1;
        if (invoiceSum < vatInvoiceSum) {
            pushSum = vatInvoiceSum % invoiceSum == 0 ? vatInvoiceSum / invoiceSum : (vatInvoiceSum / invoiceSum + 1);
        }
        log.info("税号taxNo:{}需要对接规则引擎的发票信息次数pushSum:{}", tAcOrg.getTaxno(), pushSum);
        int row = 0;
        for (int i = 0; i < pushSum; i++) {
            pramsMap.put("row", row);
            pramsMap.put("number", invoiceSum);
            List<ControlRulesInvoiceRequest> controlRulesInvoiceEntities = super.baseMapper.vatInvoice(pramsMap);
            ControlRulesResponse response = sendRequest(controlRulesInvoiceEntities);
            //发票规则引擎信息入库
            if (response != null) {
                infoStorage(response);
            }
            row = row + invoiceSum;
        }
    }

    @Override
    public void notInvoiceRules(TAcOrg tAcOrg) {
        DynamicContextHolder.push(tAcOrg.getDbName() + DbConstant.BUSINESS_READ);
        EnterpriseInfoConfigEntity enterpriseInfoConfigEntity = super.baseMapper.selectConfig(tAcOrg.getTaxno(),"2");
        if (null == enterpriseInfoConfigEntity) {
            log.info("税号taxNo:{}没有设置信息", tAcOrg.getTaxno());
            return;
        }
        if (StringUtils.isEmpty(enterpriseInfoConfigEntity.getNotInvoiceType())) {
            log.info("税号taxNo:{}没有需要对接规则引擎的发票类型", tAcOrg.getTaxno());
            return;
        }
        if (0 == enterpriseInfoConfigEntity.getInvoiceSum()) {
            log.info("税号taxNo:{}没有设置对接规则引擎的发票单次推送数量", tAcOrg.getTaxno());
            return;
        }
        String[] invoiceTypes = enterpriseInfoConfigEntity.getNotInvoiceType().split(",");
        Map<String, Object> pramsMap = new HashMap<>(10);
        pramsMap.put("taxNo", tAcOrg.getTaxno());
        pramsMap.put("notInvoiceType", invoiceTypes);
        if (!StringUtils.isEmpty(enterpriseInfoConfigEntity.getInvoiceDate())) {
            pramsMap.put("invoiceDate", enterpriseInfoConfigEntity.getInvoiceDate());
        }
        if (!StringUtils.isEmpty(enterpriseInfoConfigEntity.getCreateDate())) {
            pramsMap.put("createDate", enterpriseInfoConfigEntity.getCreateDate());
        }
        if (enterpriseInfoConfigEntity.getSelectMonth()!=0) {
            String lastMonth = getLastMonth(enterpriseInfoConfigEntity.getSelectMonth());
            pramsMap.put("selectMonth", lastMonth);
        }
        int notInvoiceSum = super.baseMapper.notInvoiceSum(pramsMap);
        if (0 == notInvoiceSum) {
            log.info("税号taxNo:{}没有需要对接规则引擎的发票信息", tAcOrg.getTaxno());
            return;
        }

        int invoiceSum = enterpriseInfoConfigEntity.getInvoiceSum();
        int pushSum = 1;
        if (invoiceSum < notInvoiceSum) {
            pushSum = notInvoiceSum % invoiceSum == 0 ? notInvoiceSum / invoiceSum : (notInvoiceSum / invoiceSum + 1);
        }
        log.info("税号taxNo:{}需要对接规则引擎的发票信息次数pushSum:{}", tAcOrg.getTaxno(), pushSum);
        int row = 0;
        for (int i = 0; i < pushSum; i++) {
            pramsMap.put("row", row);
            pramsMap.put("number", invoiceSum);
            List<ControlRulesInvoiceRequest> controlRulesInvoiceEntities = super.baseMapper.notInvoice(pramsMap);
            controlRulesInvoiceEntities.stream().forEach(controlRulesInvoiceRequest -> {
                if ("90".equals(controlRulesInvoiceRequest.getInvoiceTypeCode())) {
                    controlRulesInvoiceRequest.setFlightNumber(controlRulesInvoiceRequest.getTrainNumber());
                }
            });
            ControlRulesResponse response = sendRequest(controlRulesInvoiceEntities);
            if (response != null) {
                infoStorage(response);
            }
            row = row + invoiceSum;
        }

    }

    //发票对接规则引擎信息入库整理
    public void infoStorage(ControlRulesResponse response) {
        List<ControlRulesInvoiceResponse> ruleInvoiceDataList = response.getRuleInvoiceDataList();
        ruleInvoiceDataList.stream().forEach(controlRulesInvoiceResponse -> {
            //获取发票类型
            String invoiceTypeCode = controlRulesInvoiceResponse.getInvoiceTypeCode();
            String uuid = "";
            if (vatInvoiceType.contains(invoiceTypeCode)) {
                uuid = controlRulesInvoiceResponse.getInvoiceCode() + controlRulesInvoiceResponse.getInvoiceNo();
            }else {
                StringBuffer sb = new StringBuffer();
                sb.append(invoiceTypeCode);
                if (StringUtils.isNotBlank(controlRulesInvoiceResponse.getTicketNumber())) {
                    sb.append(controlRulesInvoiceResponse.getTicketNumber());
                }
                if (StringUtils.isNotBlank(controlRulesInvoiceResponse.getInvoiceCode())) {
                    sb.append(controlRulesInvoiceResponse.getInvoiceCode());
                }
                if (StringUtils.isNotBlank(controlRulesInvoiceResponse.getInvoiceNo())) {
                    sb.append(controlRulesInvoiceResponse.getInvoiceNo());
                }
                uuid = sb.toString();
            }
            if (!CollectionUtils.isEmpty(controlRulesInvoiceResponse.getRuleCheckApiList())) {
                List<EnterpriseInfoEntity> ruleCheckApiList = controlRulesInvoiceResponse.getRuleCheckApiList();
                //删除原发票信息
                Map<String, Object> columnMap = new HashMap<>();
                columnMap.put("uuid", uuid);
                super.baseMapper.deleteByMap(columnMap);
                for (EnterpriseInfoEntity enterpriseInfoEntity : ruleCheckApiList) {
                    enterpriseInfoEntity.setUuid(uuid);
                    enterpriseInfoEntity.setInvoiceType(invoiceTypeCode);
                    enterpriseInfoEntity.setInvoiceCode(controlRulesInvoiceResponse.getInvoiceCode());
                    enterpriseInfoEntity.setInvoiceNo(controlRulesInvoiceResponse.getInvoiceNo());
                    enterpriseInfoEntity.setCreateTime(new Date());
                    super.baseMapper.insert(enterpriseInfoEntity);
                }
                if (vatInvoiceType.contains(invoiceTypeCode)) {
                    super.baseMapper.updateVatInvoice(uuid);
                } else {
                    super.baseMapper.updateNotInvoice(uuid);
                }
            }
        });
    }

    public ControlRulesResponse sendRequest(List jsonObject) {

        // 请求通用头
        final GlobalInfo globalInfo = new GlobalInfo();
        // 请求通用
        final Authorize authorize = new Authorize();

        final BxRequestData requestData = new BxRequestData();
        ControlRulesResponse controlRulesResponse = null;
        try {
            // 封装参数

            globalInfo.setAppId(appId);
            globalInfo.setVersion(version);
            globalInfo.setInterfaceCode(ruleCheckCode);
            globalInfo.setEnterpriseCode(null);
            requestData.setGlobalInfo(globalInfo);
            requestData.setData(jsonObject);

            authorize.setAppSecId(secretId);
            requestData.setAuthorize(authorize);

            String appSec = SignUtil.sign(controlRulesUrl, requestData, secretKey);

            authorize.setAppSec(appSec);
            requestData.setAuthorize(authorize);
            String result = JSON.toJSONString(requestData);
            log.info("url:{}", controlRulesUrl);
            log.info("请求参数：{}", result);
            log.info("------------------发票对接规则引擎发送请求-----------------");
            final String responseJson = HttpUtil.post(controlRulesUrl, result, 600000);
            log.info("返回报文:{}", responseJson);
            final BxResponseData baseResJson = JSONObject.parseObject(responseJson, BxResponseData.class);
            if ("0000".equals(baseResJson.getCode())) {
                controlRulesResponse = baseResJson.getDataObj();
            }
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("发票对接规则引擎异常,e:{}", e.getMessage());
        }
        return controlRulesResponse;
    }

    public String getLastMonth(int month) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - month);
        date = calendar.getTime();
        String accDate = format.format(date);
        log.info("发票对接规则引擎accDate:{}", accDate);
        return accDate;
    }
}
*/
