package com.dxhy.extend.service.companypool;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.enums.FpqsjgEnum;
import com.dxhy.common.enums.FpztEnum;
import com.dxhy.common.enums.InvoiceTypeEnum;
import com.dxhy.common.enums.QsfsEnum;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.extend.dao.ExtSaleSignDao;
import com.dxhy.extend.dao.ExtTicketDao;
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.util.List;
import java.util.Map;

/**
 * @author futianhao
 * @date 2020-08-28 企业票池首页
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service
@Slf4j
public class SaleSignServiceImpl extends MpBaseServiceImpl<ExtSaleSignDao, ExtSaleSignModel>
    implements SaleSignService {
    @Resource
    private ExtSaleSignDao saleSignDao;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String queryList(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", "qsDate", "systemName", "invoiceNum", "invoiceAmount", "taxAmount"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            List<ExtSaleSignModel> pageList = super.baseMapper.querySaleSign(paramsMap);
            PageInfo<ExtSaleSignModel> 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())));
                    for (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQsStatus())) {
                            pageList.get(i).setQsStatus(e.getQsjgMc(e.getIndex()));
                        }
                    }
                    if (pageList.get(i).getQsDate() != null) {
                        if (pageList.get(i).getQsDate().length() > 0) {
                            pageList.get(i).setQsDate(pageList.get(i).getQsDate().substring(0, 10));
                        }
                    }
                }
            }

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

    @Override
    public String queryDetailList(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", "qsStatus", "invoiceType", "invoiceStatus", "invoiceCode", "invoiceNo",
                "invoiceDate", "gfName", "xfName", "invoiceAmount", "taxAmount", "totalAmount", "incentiveFlag"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            List<ExtSaleSignDetailModel> pageList = super.baseMapper.querySaleSignDetail(paramsMap);
            PageInfo<ExtSaleSignDetailModel> 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())));
                    for (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQsStatus())) {
                            pageList.get(i).setQsStatus(e.getQsjgMc(e.getIndex()));
                        }
                    }
                    pageList.get(i).setInvoiceStatus(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS,pageList.get(i).getInvoiceStatus()));
                    for (FpztEnum e : FpztEnum.values()) {
                        if (e.getFpztDm().equals(pageList.get(i).getInvoiceStatus())) {
                            pageList.get(i).setInvoiceStatus(e.getFpztMc(e.getIndex()));
                        }
                    }
                    if (pageList.get(i).getInvoiceDate() != null) {
                        if (pageList.get(i).getInvoiceDate().length() > 0) {
                            pageList.get(i).setInvoiceDate(pageList.get(i).getInvoiceDate().substring(0, 10));
                        }
                    }
                    pageList.get(i).setTotalAmount(
                            AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTotalAmount())));
                    pageList.get(i).setInvoiceType(InvoiceTypeUtils.invoiceTypeName(pageList.get(i).getInvoiceType()));
                }
            }
            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtSaleSignDetailModel> resultList = pageInfo.getList();
            json.put("datalist", resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.responseResult(json);
    }

    @Override
    public String querySignInvoice(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", "xfName", "xfTaxno", "invoiceAmount", "taxAmount", "totalAmount", "listStatus",
                "remark", "incentiveFlag", "qsStatus", "qsDate", "qsType", "systemName"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                List<ExtAddTaxInvoice> hjList = super.baseMapper.selectAddTaxHj(paramsMap);
                if (hjList.size() > 0) {
                    ExtAddTaxInvoice hjxx = hjList.get(0);
                    hjsl = hjxx.getId();
                    if (!"0".equals(hjsl)) {
                        hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount()));
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount()));
                    }
                }

                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }
            PageHelper.startPage(curr, size);
            List<ExtSaleSignInvoiceModel> pageList = super.baseMapper.querySaleSignInvoice(paramsMap);
            PageInfo<ExtSaleSignInvoiceModel> 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 (FpqsjgEnum e : FpqsjgEnum.values()) {
                        if (e.getQsjgDm().equals(pageList.get(i).getQsStatus())) {
                            pageList.get(i).setQsStatus(e.getQsjgMc(e.getIndex()));
                            pageList.get(i).setQszt(pageList.get(i).getQsStatus());
                        }
                    }
                    for (FpztEnum e : FpztEnum.values()) {
                        if (e.getFpztDm().equals(pageList.get(i).getInvoiceStatus())) {
                            pageList.get(i).setInvoiceStatus(e.getFpztMc(e.getIndex()));
                        }
                    }
                    for (QsfsEnum e : QsfsEnum.values()) {
                        if (e.getQsfsDm().equals(pageList.get(i).getQsType())) {
                            pageList.get(i).setQsType(e.getQsfsMc(e.getIndex()));
                        }
                    }
                    if ("0".equals(pageList.get(i).getListStatus())) {
                        pageList.get(i).setListStatus("否");
                    }
                    if ("1".equals(pageList.get(i).getListStatus())) {
                        pageList.get(i).setListStatus("是");
                    }
                    if (pageList.get(i).getQsDate() != null) {
                        if (pageList.get(i).getQsDate().length() > 0) {
                            pageList.get(i).setQsDate(pageList.get(i).getQsDate().substring(0, 10));
                        }
                    }
                    if (pageList.get(i).getQsDate() != null) {
                        if (pageList.get(i).getQsDate().length() > 0) {
                            pageList.get(i).setQsDate(pageList.get(i).getQsDate().substring(0, 10));
                            pageList.get(i).setQsrq(pageList.get(i).getQsDate());
                        }
                    }
                    if (pageList.get(i).getInvoiceDate() != null) {
                        if (pageList.get(i).getInvoiceDate().length() > 0) {
                            pageList.get(i).setInvoiceDate(pageList.get(i).getInvoiceDate().substring(0, 10));
                        }
                    }
                    pageList.get(i).setInvoiceType(InvoiceTypeUtils.invoiceTypeName(pageList.get(i).getInvoiceType()));
                }
            }
            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<ExtSaleSignInvoiceModel> resultList = pageInfo.getList();
            json.put("datalist", resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.responseResult(json);
    }
}
