package com.dxhy.extend.service.paper;

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.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.ExtPaperDao;
import com.dxhy.extend.entity.ExtBuyInvoicePaper;
import com.dxhy.extend.entity.ExtSelInvoicePaper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author dxhy
 */
@Service
public class ExtPaperServiceImpl extends MpBaseServiceImpl<ExtPaperDao, ExtBuyInvoicePaper> implements ExtPaperService {

    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String listBuyInvoicePaper(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "采集时间", "采集来源", "数量合计", "金额合计", "税额合计"};
            String[] titleprop = {"xh", "collectDate", "sourceSystem", "allCount", "invoiceAmount", "taxAmount",};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            PageHelper.startPage(curr, size);
            List<ExtBuyInvoicePaper> pageList = super.baseMapper.listBuyInvoicePaper(pramsMap);
            PageInfo<ExtBuyInvoicePaper> 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));
                    if ("0".equals(pageList.get(i).getCollectFrom())) {
                        pageList.get(i).setSourceSystem("进项采集");
                        pageList.get(i).setSourceSystemId("0");
                    } else if ("1".equals(pageList.get(i).getCollectFrom())) {
                        pageList.get(i).setSourceSystem("票据采集");
                        pageList.get(i).setSourceSystemId("1");
                    } else if ("4".equals(pageList.get(i).getCollectFrom())) {
                        pageList.get(i).setSourceSystem("大企业归集");
                        pageList.get(i).setSourceSystemId("4");
                    }
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    if ("0".equals(pageList.get(i).getPoolStatus())) {
                        pageList.get(i).setPoolStatus("成功");
                    } else if ("1".equals(pageList.get(i).getPoolStatus())) {
                        pageList.get(i).setPoolStatus("失败");
                    }
                    if (StringUtils.isBlank(pageList.get(i).getInPoolReason())) {
                        pageList.get(i).setInPoolReason("--");
                    }
                }
            }
            json.put("total", pageInfo.getTotal());
            List<ExtBuyInvoicePaper> resultList = pageInfo.getList();
            json.put("datalist", resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

    @Override
    public String selectInvoicePaper(Map<String, Object> pramsMap) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String[] titlelist = {"序号", "入池结果", "发票类型", "发票代码", "发票号码", "数电号码","开票日期", "购方名称", "销方名称", "金额", "税额", "价税合计"};
            String[] titleprop = {"xh", "poolStatus", "invoiceType", "invoiceCode", "invoiceNo","eleInvoiceNo", "invoiceDate",
                "buyerName", "saleName", "invoiceAmount", "taxAmount", "totalAmount"};
            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);
            int curr = (Integer)pramsMap.get("curr");
            int size = (Integer)pramsMap.get("size");
            PageHelper.startPage(curr, size);
            List<ExtSelInvoicePaper> pageList = super.baseMapper.selectInvoicePaper(pramsMap);
            // 记录成功数量
            int succCount = super.baseMapper.getSuccCount(pramsMap);
            int failCount = super.baseMapper.getFailCount(pramsMap);
            // 记录失败数量
            PageInfo<ExtSelInvoicePaper> 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 + (curr - 1) * size + 1));
                    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()))));
                    pageList.get(i).setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL, pageList.get(i).getInvoiceType()));
                    if ("0".equals(pageList.get(i).getPoolStatus())) {
                        pageList.get(i).setPoolStatus("成功");
                    } else if ("1".equals(pageList.get(i).getPoolStatus())) {
                        pageList.get(i).setPoolStatus("失败:数据重复");
                    }
                }
            }
            json.put("total", pageInfo.getTotal());
            json.put("succSize", succCount);
            json.put("failSize", failCount);
            json.put("datalist", pageInfo.getList());
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

}
