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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.api.model.config.sys.SysGroupConfigEntity;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.dao.CoreSysEnterpriseConfigCoreDao;
import com.dxhy.core.job.entity.TDxViolationTaxno;
import com.dxhy.core.job.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

/**
 * @author Gy Chen
 * @date 2020/11/26
 */
@Service
public class ViolationMsgServiceImpl implements ViolationMsgService {
    @Resource
    ViolationLogService violationLogService;
    @Resource
    ViolationRecordService violationRecordService;
    @Resource
    ViolationTaxnoService violationTaxnoService;
    @Resource
    ViolationTDxRecordInvoice violationTDxRecordInvoice;

    @Resource
    private CoreSysEnterpriseConfigCoreDao coreSysEnterpriseConfigCoreDao;


    @Override
    public void selectViolationTaxno() {
        List<SysGroupConfigEntity> deptList = violationTaxnoService.selectCompanyByViolationStatus();
        if (deptList != null && deptList.size() > 0) {
            for (SysGroupConfigEntity dept : deptList) {
                List<String> orgList = violationTaxnoService.selectTaxnoByCompany(dept.getCompany());
                if (orgList != null && orgList.size() > 0) {
                    DynamicContextHolder.push(DbConstant.BASICS_READ);
                    //判断是否为大企业税号 乐企税号也走大企业通道
//                    BasisSysDeptCfg baseSysDeptCfg = basisSysDeptCfgDao.selectOne(new QueryWrapper<BasisSysDeptCfg>().);
//                    if (baseSysDeptCfg != null) {
//                        continue;
//                    }
                    LocalDate localDate = LocalDate.now();
                    int year = localDate.getYear() - 2;
                    List<TDxViolationTaxno> taxnoList = violationTaxnoService.selectViolationTaxno(dept.getCompany(),
                        year + "-01-01", orgList, dept.getDbName());
                    DynamicContextHolder.push(dept.getDbName() + DbConstant.BUSINESS_WRITE);
                    violationTaxnoService.insertTaxno(taxnoList, dept.getCompany());
                }
            }
        }
    }

    @Override
    public void getViolation() {
        DynamicContextHolder.push("business" + DbConstant.BUSINESS_READ);
        // List<TDxViolationTaxno> list =
        // violationTaxnoService.list(Wrappers.<TDxViolationTaxno>query().groupBy("xf_tax_no"));
        List<TDxViolationTaxno> list = violationTaxnoService.selectViolationTaxnoByXf();
        if (list != null && list.size() > 0) {
            Map<String, List<String>> map = new HashMap<>();
            for (TDxViolationTaxno tax : list) {
                DynamicContextHolder.push(DbConstant.BASICS_READ);
                //判断是否为大企业税号
                SysEnterpriseConfigEntity sysEnterpriseConfigEntity = coreSysEnterpriseConfigCoreDao.selectOne(new QueryWrapper<SysEnterpriseConfigEntity>().eq("nsrsbh", tax.getGfTaxNo()));
                if (ObjectUtil.isNotEmpty(sysEnterpriseConfigEntity)&&"1".equals(sysEnterpriseConfigEntity.getJrfs())) {
                    continue;
                }
                List<String> vList = map.get(tax.getGfTaxNo());
                if (vList != null && vList.size() > 0) {
                    vList.add(tax.getXfTaxNo());
                    map.put(tax.getGfTaxNo(), vList);
                } else {
                    List<String> xfList = new ArrayList<>();
                    xfList.add(tax.getXfTaxNo());
                    map.put(tax.getGfTaxNo(), xfList);
                }
            }

            Iterator<Map.Entry<String, List<String>>> entries = map.entrySet().iterator();
            while (entries.hasNext()) {
                Map.Entry<String, List<String>> entry = entries.next();
                String gfTaxNo = entry.getKey();
                List<String> xfList = entry.getValue();
                if (xfList != null && xfList.size() > 0) {
                    List<String> taxList = new ArrayList<>();
                    for (String xfTaxNo : xfList) {
                        taxList.add(xfTaxNo);
                        if (taxList.size() == 99) {
                            violationRecordService.sendViolationRequest(gfTaxNo, taxList);
                            // diaojiekou
                            taxList.clear();
                        }
                    }
                    if (taxList != null && taxList.size() > 0) {
                        // 处理剩余数据
                        violationRecordService.sendViolationRequest(gfTaxNo, taxList);
                    }
                }
            }
        }
    }

    @Override
    public void getViolationResult() {
        violationRecordService.getViolationResult();
    }

    @Override
    public void updateTdxRecodeInvoce() {
        violationTDxRecordInvoice.updateTdxRecodeInvoce();
    }
}
