package com.dxhy.extend.service.companypool;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FpqsjgEnum;
import com.dxhy.common.enums.FpztEnum;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.extend.dao.ExtSaleCountDao;
import com.dxhy.extend.entity.*;
import com.dxhy.extend.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author futianhao
 * @date 2021-04-14 增值税申报数据
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service
@Slf4j
public class SaleCountServiceImpl extends MpBaseServiceImpl<ExtSaleCountDao, ExtSaleCountModel>
    implements SaleCountService {
    @Resource
    private ExtSaleCountDao saleCountDao;

    @Override
    public Object queryRateList(Map<String, Object> paramsMap) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"正数有效发票金额", "正数有效发票税额", "正数有效价税合计", "负数有效发票金额", "负数有效发票税额", "负数有效价税合计", "正数作废发票金额",
                "正数作废发票税额", "正数作废价税合计", "负数作废发票金额", "负数作废发票税额", "负数作废价税合计"};

            String[] titleprop = {"invoiceAmountPt", "taxAmountPt", "totalAmountPt", "invoiceAmountNt", "taxAmountNt",
                "totalAmountNt", "invoiceAmountPtVoid", "taxAmountPtVoid", "totalAmountPtVoid", "invoiceAmountNtVoid",
                "taxAmountNtVoid", "totalAmountNtVoid"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);

            List<ExtSaleCountModel> pageList = super.baseMapper.querySaleCountRate(paramsMap);
            PageInfo<ExtSaleCountModel> pageInfo = new PageInfo<>(pageList);
            ExtSaleCountModel countModel = initAllCount();
            String[] str = new String[pageInfo.getSize() + 1];
            // 用于页面返回list对象
            if (pageList.size() > 0) {

                str[0] = "全部";
                for (int i = 0; i < pageList.size(); i++) {
                    if (!"免税".equals(pageList.get(i).getTaxRate()) && !"不征税".equals(pageList.get(i).getTaxRate())) {
                        str[i + 1] = new BigDecimal(pageList.get(i).getTaxRate()).multiply(new BigDecimal(100))
                            .toString().replace(".00", "") + "%";
                        pageList.get(i).setTaxRate(str[i + 1]);
                    } else {
                        str[i + 1] = pageList.get(i).getTaxRate();
                    }
                    // 汇总金额
                    countModel.setTaxRate("全部");
                    countModel.setInvoiceAmountPt(new BigDecimal(countModel.getInvoiceAmountPt())
                        .add(new BigDecimal(pageList.get(i).getInvoiceAmountPt())).toString());
                    countModel.setTaxAmountPt(new BigDecimal(countModel.getTaxAmountPt())
                        .add(new BigDecimal(pageList.get(i).getTaxAmountPt())).toString());
                    countModel.setTotalAmountPt(new BigDecimal(countModel.getTotalAmountPt())
                        .add(new BigDecimal(pageList.get(i).getTotalAmountPt())).toString());
                    countModel.setInvoiceAmountNt(new BigDecimal(countModel.getInvoiceAmountNt())
                        .add(new BigDecimal(pageList.get(i).getInvoiceAmountNt())).toString());
                    countModel.setTaxAmountNt(new BigDecimal(countModel.getTaxAmountNt())
                        .add(new BigDecimal(pageList.get(i).getTaxAmountNt())).toString());
                    countModel.setTotalAmountNt(new BigDecimal(countModel.getTotalAmountNt())
                        .add(new BigDecimal(pageList.get(i).getTotalAmountNt())).toString());
                    countModel.setInvoiceAmountPtVoid(new BigDecimal(countModel.getInvoiceAmountPtVoid())
                        .add(new BigDecimal(pageList.get(i).getInvoiceAmountPtVoid())).toString());
                    countModel.setTaxAmountPtVoid(new BigDecimal(countModel.getTaxAmountPtVoid())
                        .add(new BigDecimal(pageList.get(i).getTaxAmountPtVoid())).toString());
                    countModel.setTotalAmountPtVoid(new BigDecimal(countModel.getTotalAmountPtVoid())
                        .add(new BigDecimal(pageList.get(i).getTotalAmountPtVoid())).toString());
                    countModel.setInvoiceAmountNtVoid(new BigDecimal(countModel.getInvoiceAmountNtVoid())
                        .add(new BigDecimal(pageList.get(i).getInvoiceAmountNtVoid())).toString());
                    countModel.setTaxAmountNtVoid(new BigDecimal(countModel.getTaxAmountNtVoid())
                        .add(new BigDecimal(pageList.get(i).getTaxAmountNtVoid())).toString());
                    countModel.setTotalAmountNtVoid(new BigDecimal(countModel.getTotalAmountNtVoid())
                        .add(new BigDecimal(pageList.get(i).getTotalAmountNtVoid())).toString());

                    // 金额转换
                    pageList.get(i).setInvoiceAmountPt(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmountPt())));
                    pageList.get(i).setTaxAmountPt(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmountPt())));
                    pageList.get(i).setTotalAmountPt(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmountPt())));
                    pageList.get(i).setInvoiceAmountNt(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmountNt())));
                    pageList.get(i).setTaxAmountNt(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmountNt())));
                    pageList.get(i).setTotalAmountNt(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmountNt())));
                    pageList.get(i).setInvoiceAmountPtVoid(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmountPtVoid())));
                    pageList.get(i).setTaxAmountPtVoid(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmountPtVoid())));
                    pageList.get(i).setTotalAmountPtVoid(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmountPtVoid())));
                    pageList.get(i).setInvoiceAmountNtVoid(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmountNtVoid())));
                    pageList.get(i).setTaxAmountNtVoid(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmountNtVoid())));
                    pageList.get(i).setTotalAmountNtVoid(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmountNtVoid())));

                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtSaleCountModel> resultList = pageInfo.getList();
            if (resultList.size() > 0) {
                json.put("column", str);
                countModel.setInvoiceAmountPt(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getInvoiceAmountPt())));
                countModel.setTaxAmountPt(AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTaxAmountPt())));
                countModel
                    .setTotalAmountPt(AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTotalAmountPt())));
                countModel.setInvoiceAmountNt(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getInvoiceAmountNt())));
                countModel.setTaxAmountNt(AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTaxAmountNt())));
                countModel
                    .setTotalAmountNt(AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTotalAmountNt())));
                countModel.setInvoiceAmountPtVoid(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getInvoiceAmountPtVoid())));
                countModel.setTaxAmountPtVoid(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTaxAmountPtVoid())));
                countModel.setTotalAmountPtVoid(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTotalAmountPtVoid())));
                countModel.setInvoiceAmountNtVoid(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getInvoiceAmountNtVoid())));
                countModel.setTaxAmountNtVoid(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTaxAmountNtVoid())));
                countModel.setTotalAmountNtVoid(
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(countModel.getTotalAmountNtVoid())));

                resultList.add(0, countModel);
            }
            json.put("datalist", resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    private ExtSaleCountModel initAllCount() {
        ExtSaleCountModel countModel = new ExtSaleCountModel();
        countModel.setInvoiceAmountPt("0");
        countModel.setTaxAmountPt("0");
        countModel.setTotalAmountPt("0");
        countModel.setInvoiceAmountNt("0");
        countModel.setTaxAmountNt("0");
        countModel.setTotalAmountNt("0");
        countModel.setInvoiceAmountPtVoid("0");
        countModel.setTaxAmountPtVoid("0");
        countModel.setTotalAmountPtVoid("0");
        countModel.setInvoiceAmountNtVoid("0");
        countModel.setTaxAmountNtVoid("0");
        countModel.setTotalAmountNtVoid("0");
        countModel.setTaxRate("全部");
        return countModel;
    }

    @Override
    public Object queryItemList(Map<String, Object> paramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"项目编码", "销方名称", "项目名称", "发票份数", "发票金额", "发票税额", "价税合计"};

            String[] titleprop =
                {"itemCode", "xfName", "itemName", "invoiceNum", "invoiceAmount", "taxAmount", "totalAmount"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            List<ExtSaleItemCountModel> pageList = super.baseMapper.querySaleItem(paramsMap);
            PageInfo<ExtSaleItemCountModel> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {

                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    String xfName = super.baseMapper.querySaleName(pageList.get(i).getTaxNumber());
                    pageList.get(i).setXfName(xfName);
                    pageList.get(i).setInvoiceNum(pageList.get(i).getInvoiceNum().replace(".0000", ""));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    pageList.get(i).setTotalAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmount())));

                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtSaleItemCountModel> resultList = pageInfo.getList();
            json.put("datalist", resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    @Override
    public Object queryInvoiceList(Map<String, Object> paramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(paramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "发票类型", "发票代码", "发票号码", "发票状态", "开票日期", "购方名称", "购方税号", "金额", "税额", "价税合计"};

            String[] titleprop = {"xh", "invoiceType", "invoiceCode", "invoiceNo", "invoiceStatus", "invoiceDate",
                "gfName", "gfTaxNo", "invoiceAmount", "taxAmount", "totalAmount"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);

            // 获取项目名称对应的查询条件
            ExtSaleItemConditionModel model = super.baseMapper.querySaleNameCondition(paramsMap);
            paramsMap.put("yhzc", model.getYhzc());
            if ("专票".equals(model.getInvoiceType())) {
                String invoiceType[] = new String[2];
                invoiceType[0] = "01";
                invoiceType[1] = "08";
                paramsMap.put("invoiceType", invoiceType);
            } else {
                String invoiceType[] = new String[3];
                invoiceType[0] = "10";
                invoiceType[1] = "04";
                invoiceType[2] = "11";
                paramsMap.put("invoiceType", invoiceType);
            }
            if (model.getTaxRate().contains("征收率")) {
                // 3 4 5 6
                String taxRate = "0.0" + model.getTaxRate().split("%")[0];
                String taxRates[] = new String[1];
                taxRates[0] = taxRate;
                paramsMap.put("taxRate", taxRates);
            } else if (model.getTaxRate().contains("或")) {
                String[] taxRate = model.getTaxRate().replace("%", "").split("或");
                for (int i = 0; i < taxRate.length; i++) {
                    if (taxRate[i].length() == 1) {
                        taxRate[i] = "0.0" + taxRate[i];
                    } else {
                        taxRate[i] = "0." + taxRate[i];
                    }
                }
                paramsMap.put("taxRate", taxRate);
            } else {

                String taxRate = model.getTaxRate().replace("%", "");
                if (!"免税".equals(taxRate) && !"不征税".equals(taxRate)) {
                    if (taxRate.length() == 1) {
                        taxRate = "0.0" + taxRate;
                    } else {
                        taxRate = "0." + taxRate;
                    }
                }
                String taxRates[] = new String[1];
                taxRates[0] = taxRate;
                paramsMap.put("taxRate", taxRates);
            }
            if ("无需判断商品编码".equals(model.getGoodsCode())) {
                String gs[] = new String[0];
                paramsMap.put("goodsCode", gs);
            } else {
                if (model.getGoodsCode() != null) {
                    if (model.getGoodsCode().contains("或")) {
                        paramsMap.put("goodsCode", model.getGoodsCode().split(" 或 "));
                    } else if (model.getGoodsCode().contains(",")) {
                        paramsMap.put("goodsCode", model.getGoodsCode().split(","));
                    } else {
                        String gs[] = new String[1];
                        gs[0] = model.getGoodsCode();
                        paramsMap.put("goodsCode", gs);
                    }
                }

            }
            PageHelper.startPage(curr, size);
            List<ExtSaleItemInvoiceModel> pageList = super.baseMapper.querySaleItemInvoices(paramsMap);
            PageInfo<ExtSaleItemInvoiceModel> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {

                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    pageList.get(i).setTotalAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmount())));
                    for (FpztEnum e : FpztEnum.values()) {
                        if (e.getFpztDm().equals(pageList.get(i).getInvoiceStatus())) {
                            pageList.get(i).setInvoiceStatus(e.getFpztMc(e.getIndex()));
                        }
                    }
                    pageList.get(i).setInvoiceType(InvoiceTypeUtils.invoiceTypeName(pageList.get(i).getInvoiceType()));
                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtSaleItemInvoiceModel> resultList = pageInfo.getList();
            json.put("datalist", resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }
}
