package com.dxhy.extend.service.companypool;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.dxhy.extend.model.AccountInfoVO;
import com.dxhy.extend.util.InvoiceTypeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.extend.dao.ExtCompanyIndexDao;
import com.dxhy.extend.model.CompanyPoolIndexVO;
import com.dxhy.extend.model.PieDataVO;
import com.dxhy.extend.model.YearStatVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author futianhao
 * @date 2020-08-28 企业票池首页
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service
@Slf4j
public class CompanyIndexServiceImpl extends MpBaseServiceImpl<ExtCompanyIndexDao, CompanyPoolIndexVO>
    implements CompanyIndexService {
    @Resource
    private ExtCompanyIndexDao extCompanyIndexDao;

    public static List<PieDataVO> returnInvoiceList(List<PieDataVO> list) {
        List<PieDataVO> pageList = new ArrayList<>();
        for (PieDataVO vo : list) {
            if ("01".equals(vo.getName())) {
                vo.setName("增值税专用发票");
            }
            if ("04".equals(vo.getName())) {
                vo.setName("增值税普通发票");
            }
            if ("10".equals(vo.getName())) {
                vo.setName("增值税普通发票（电子）");
            }
            if ("11".equals(vo.getName())) {
                vo.setName("增值税普通发票（卷式）");
            }
            if ("03".equals(vo.getName())) {
                vo.setName("机动车销售统一发票");
            }
            if ("02".equals(vo.getName())) {
                vo.setName("货运运输业增值税专用发票");
            }
            if ("91".equals(vo.getName())) {
                vo.setName("出租车票");
            }
            if ("92".equals(vo.getName())) {
                vo.setName("火车票");
            }
            if ("93".equals(vo.getName())) {
                vo.setName("飞机票");
            }
            if ("94".equals(vo.getName())) {
                vo.setName("汽车票");
            }
            if ("95".equals(vo.getName())) {
                vo.setName("定额发票");
            }
            if ("96".equals(vo.getName())) {
                vo.setName("长途汽车票");
            }
            if ("00".equals(vo.getName())) {
                vo.setName("其他");
            }
            if ("97".equals(vo.getName())) {
                vo.setName("通用机打发票");
            }
            if ("98".equals(vo.getName())) {
                vo.setName("政府非税收收入一般缴款书");
            }
            if ("14".equals(vo.getName())) {
                vo.setName("增值税电子普通发票（通行费）");
            }
            if ("90".equals(vo.getName())) {
                vo.setName("航空运输客票");
            }
            if ("89".equals(vo.getName())) {
                vo.setName("公路运输客票");
            }
            if ("88".equals(vo.getName())) {
                vo.setName("水路运输客票");
            }
            if ("87".equals(vo.getName())) {
                vo.setName("其他运输客票");
            }

            pageList.add(vo);
        }
        return pageList;
    }

    @Override
    public JSONObject getTotal(Map<String, Object> paramsMap) throws ParseException {
        String currMonth = paramsMap.get("currMonth") + "";
        String taxnoParam = paramsMap.get("taxno") + "";
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        // 记录发票份数 进项 销项
        BigDecimal invoiceJxSize = new BigDecimal(0);
        BigDecimal invoiceXxSize = new BigDecimal(0);
        // 记录发票金额 进项 销项
        BigDecimal invoiceJxAmount = new BigDecimal(0);
        BigDecimal invoiceXxAmount = new BigDecimal(0);
        JSONObject resJson = new JSONObject();
        if ("99".equals(taxnoParam)) {
            String taxnos = paramsMap.get("gfshAll") + "";
            if (StringUtils.isNotEmpty(taxnos)) {
                String[] arr = taxnos.split(",");
                for (int i = 1; i < arr.length; i++) {
                    String taxno = arr[i];
                    // 通过税号和当前月份进行汇总 进项
                    CompanyPoolIndexVO jxmodel = extCompanyIndexDao.getJxTotalByTaxno(taxno, currMonth);
                    CompanyPoolIndexVO xxmodel = extCompanyIndexDao.getXxTotalByTaxno(taxno, currMonth);
                    if (jxmodel != null) {
                        invoiceJxSize = invoiceJxSize.add(new BigDecimal(jxmodel.getInvoiceJxSize()));
                        invoiceJxAmount = invoiceJxAmount.add(jxmodel.getInvoiceJxAmount());
                    }
                    if (xxmodel != null) {
                        invoiceXxSize = invoiceXxSize.add(new BigDecimal(xxmodel.getInvoiceXxSize()));
                        invoiceXxAmount = invoiceXxAmount.add(xxmodel.getInvoiceXxAmount());
                    }

                }
                resJson.put("jxSize", invoiceJxSize.toString());
                resJson.put("xxSize", invoiceXxSize.toString());
                resJson.put("jxAmount", invoiceJxAmount.toString());
                resJson.put("xxAmount", invoiceXxAmount.toString());
                // 开始时间-结束时间，当前月
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
                Calendar c = Calendar.getInstance();
                c.setTime(simpleDateFormat.parse(currMonth));
                c.add(Calendar.MONTH, -11);
                Date y = c.getTime();
                String startTime = simpleDateFormat.format(y);
                // 年度统计
                JSONArray statJson = new JSONArray();
                JSONObject jxStatObj = new JSONObject();
                List<YearStatVO> jxList = extCompanyIndexDao.getJxYearStat(arr, startTime, currMonth);
                JSONArray jxArray = JSONArray.parseArray(JSON.toJSONString(jxList));
                jxStatObj.put("type", "1");
                jxStatObj.put("data", jxArray);
                statJson.add(jxStatObj);
                // 年度统计
                JSONObject xxStatObj = new JSONObject();
                List<YearStatVO> xxList = extCompanyIndexDao.getXxYearStat(arr, startTime, currMonth);
                JSONArray xxArray = JSONArray.parseArray(JSON.toJSONString(xxList));
                xxStatObj.put("type", "2");
                xxStatObj.put("data", xxArray);
                statJson.add(xxStatObj);
                resJson.put("statResult", statJson);

                // 发票风险概览
                // JSONObject riskJson = new JSONObject();
                // JSONObject riskJxJson = extCompanyIndexDao.getJxRiskCollect(arr,currMonth);
                // resJson.put("riskJxJson",riskJxJson);
                // JSONObject riskXxJson = extCompanyIndexDao.getXxRiskCollect(arr,currMonth);
                // resJson.put("riskXxJson",riskXxJson);

                // 进项发票与税局信息对账情况
                // List<AccountInfoVO> jxAccount = extCompanyIndexDao.getJxAccount(arr,currMonth);
                // 销项发票与税局信息对账情况
                // List<AccountInfoVO> xxAccount = extCompanyIndexDao.getXxAccount(arr,currMonth);
                // resJson.put("jxAccount",jxAccount);
                // resJson.put("xxAccount",xxAccount);

            }

        } else {
            String[] arr = new String[1];
            arr[0] = taxnoParam;
            CompanyPoolIndexVO jxmodel = extCompanyIndexDao.getJxTotalByTaxno(taxnoParam, currMonth);
            CompanyPoolIndexVO xxmodel = extCompanyIndexDao.getXxTotalByTaxno(taxnoParam, currMonth);
            if (jxmodel != null) {
                invoiceJxSize = invoiceJxSize.add(new BigDecimal(jxmodel.getInvoiceJxSize()));
                invoiceJxAmount = invoiceJxAmount.add(jxmodel.getInvoiceJxAmount());
            }
            if (xxmodel != null) {
                invoiceXxSize = invoiceXxSize.add(new BigDecimal(xxmodel.getInvoiceXxSize()));
                invoiceXxAmount = invoiceXxAmount.add(xxmodel.getInvoiceXxAmount());
            }
            resJson.put("jxSize", invoiceJxSize.toString());
            resJson.put("xxSize", invoiceXxSize.toString());
            resJson.put("jxAmount", invoiceJxAmount.toString());
            resJson.put("xxAmount", invoiceXxAmount.toString());
            // 开始时间-结束时间，当前月
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            Calendar c = Calendar.getInstance();
            c.setTime(simpleDateFormat.parse(currMonth));
            c.add(Calendar.MONTH, -11);
            Date y = c.getTime();
            String startTime = simpleDateFormat.format(y);
            // 年度统计
            JSONArray statJson = new JSONArray();
            JSONObject jxStatObj = new JSONObject();
            List<YearStatVO> jxList = extCompanyIndexDao.getJxYearStat(arr, startTime, currMonth);
            JSONArray jxArray = JSONArray.parseArray(JSON.toJSONString(jxList));
            jxStatObj.put("type", "1");
            jxStatObj.put("data", jxArray);
            statJson.add(jxStatObj);
            // 年度统计
            JSONObject xxStatObj = new JSONObject();
            List<YearStatVO> xxList = extCompanyIndexDao.getXxYearStat(arr, startTime, currMonth);
            JSONArray xxArray = JSONArray.parseArray(JSON.toJSONString(xxList));
            xxStatObj.put("type", "2");
            xxStatObj.put("data", xxArray);
            statJson.add(xxStatObj);
            resJson.put("statResult", statJson);

            // 发票风险概览
            // JSONObject riskJson = new JSONObject();
            // JSONObject riskJxJson = extCompanyIndexDao.getJxRiskCollect(arr,currMonth);
            // resJson.put("riskJxJson",riskJxJson);
            // JSONObject riskXxJson = extCompanyIndexDao.getXxRiskCollect(arr,currMonth);
            // resJson.put("riskXxJson",riskXxJson);

            // 进项发票与税局信息对账情况
            // List<AccountInfoVO> jxAccount = extCompanyIndexDao.getJxAccount(arr,currMonth);
            // 销项发票与税局信息对账情况
            // List<AccountInfoVO> xxAccount = extCompanyIndexDao.getXxAccount(arr,currMonth);
            // resJson.put("jxAccount",jxAccount);
            // resJson.put("xxAccount",xxAccount);
        }
        return resJson;
    }

    @Override
    public JSONObject queryRiskList(Map<String, Object> paramsMap) {
        String currMonth = paramsMap.get("currMonth") + "";
        String taxnoParam = paramsMap.get("taxno") + "";
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject resJson = new JSONObject();
        if ("99".equals(taxnoParam)) {
            String taxnos = paramsMap.get("gfshAll") + "";
            if (StringUtils.isNotEmpty(taxnos)) {
                String[] arr = taxnos.split(",");
                // 发票风险概览
                JSONObject riskJxJson = extCompanyIndexDao.getJxRiskCollect(arr, currMonth);
                resJson.put("riskJxJson", riskJxJson);
                // resJson.put("riskXxJson",riskXxJson);
            }
        } else {
            String[] arr = new String[1];
            arr[0] = taxnoParam;
            // 发票风险概览
            JSONObject riskJxJson = extCompanyIndexDao.getJxRiskCollect(arr, currMonth);
            resJson.put("riskJxJson", riskJxJson);
        }
        return resJson;
    }

    @Override
    public JSONObject queryAccountList(Map<String, Object> paramsMap) {
        String currMonth = paramsMap.get("currMonth") + "";
        String taxnoParam = paramsMap.get("taxno") + "";
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        JSONObject resJson = new JSONObject();
        if ("99".equals(taxnoParam)) {
            String taxnos = paramsMap.get("gfshAll") + "";
            if (StringUtils.isNotEmpty(taxnos)) {
                String[] arr = taxnos.split(",");
                // 进项发票与税局信息对账情况
                List<AccountInfoVO> jxAccount = extCompanyIndexDao.getJxAccount(arr, currMonth);
                // 销项发票与税局信息对账情况
                List<AccountInfoVO> xxAccount = extCompanyIndexDao.getXxAccount(arr, currMonth);
                resJson.put("jxAccount", jxAccount);
                resJson.put("xxAccount", xxAccount);
            }
        } else {
            String[] arr = new String[1];
            arr[0] = taxnoParam;
            // 进项发票与税局信息对账情况
            List<AccountInfoVO> jxAccount = extCompanyIndexDao.getJxAccount(arr, currMonth);
            // 销项发票与税局信息对账情况
            List<AccountInfoVO> xxAccount = extCompanyIndexDao.getXxAccount(arr, currMonth);
            resJson.put("jxAccount", jxAccount);
            resJson.put("xxAccount", xxAccount);
        }
        return resJson;
    }
}
