package com.dxhy.domestic.service.count.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.TabulateRedisService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.domestic.dao.DomesticExportInvoiceDao;
import com.dxhy.domestic.entity.TdxExportInvoice;
import com.dxhy.domestic.model.dto.ResultData;
import com.dxhy.domestic.model.dto.TdxExportInvoiceModelDTO;
import com.dxhy.domestic.service.count.DomesticInvoiceExportCountService;

/**
 * @ProjectName: dev
 * @Package: com.dxhy.domestic.service.count.impl
 * @ClassName: InvoiceExportCountServiceImpl
 * @author : ddx : ${description}
 * @Date: 2020-07-06 14:24
 * @Version: 1.0
 */
@Service
@Slf4j
public class DomesticInvoiceExportCountServiceImpl extends MpBaseServiceImpl<DomesticExportInvoiceDao, TdxExportInvoice>
    implements DomesticInvoiceExportCountService {

    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public String queryExportInvoiceList(Page page, TdxExportInvoiceModelDTO dto) throws Exception {
        DynamicContextHolder
            .push(StringUtils.defaultString(dto.getUserInfo().getDbName(), "business") + DbConstant.BUSINESS_READ);
        dto.setParam(page, dto.getUserInfo());

        ResultData<Object> resultData = new ResultData<>();

        JSONObject tabulateMenu = tabulateRedisService.getTabulateMenu(dto.getCompany(), dto.getMenuId());

        resultData.putTabulateMenu(tabulateMenu);
        if (org.apache.commons.lang.StringUtils.isBlank(dto.getGfsh()) && dto.getGfTaxNo().size() <= 0) {
            resultData.setDataList(new ArrayList<>());
            return resultData.toJsonBase();
        }
        log.info("开始分页查询queryPrams值为 {}",dto);
        Page<Map> result = this.baseMapper.queryExportInvoiceList(page, dto);
        if (CollectionUtils.isNotEmpty(result.getRecords())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // List<DictdetaModel> cxrzModels = dictdetaServcice.selectDictdeta("CXRZ_STATUS");
            double exportAmountTotal = 0.0;
            double exportTaxAmountTotal = 0.0;
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map map = result.getRecords().get(i);
                map.put("xh",
                    String.valueOf((((page.getCurrent() == 0 ? 1 : page.getCurrent()) - 1) * page.getSize()) + i + 1));
                if (map.get("invoiceDate") != null) {
                    map.put("invoiceDate",map.get("invoiceDate"));
                }
                if (map.get("rzhDate") != null) {
                    map.put("rzhDate", map.get("rzhDate"));
                }
                // 字典转换
                String invoiceStatus = (String)map.get("invoiceStatus");
                map.put("invoiceStatus", dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, invoiceStatus));
                String rzhYesorno = (String)map.get("rzhYesorno");
                map.put("rzhYesorno", dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, rzhYesorno));
                String authStatus = (String)map.get("authStatus");
                map.put("authStatus", dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, authStatus));
                String bdkStatus = (String)map.get("bdkStatus");
                map.put("bdkStatus", dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, bdkStatus));
                String cxbdkStatus = (String)map.get("cxbdkStatus");
                map.put("cxbdkStatus", dictdetaServcice.queryDictName(DictConstant.CXRZ_STATUS, cxbdkStatus));

                // if (cxrzModels!=null){
                // for (DictdetaModel model:cxrzModels){
                // if (model.getDictcode().equals(map.get("cxbdkStatus"))){
                // map.put("cxbdkStatus",model.getDictname());
                // }
                // }
                // }
                map.put("select", true);
                exportAmountTotal += ((BigDecimal)map.get("exportAmount")).doubleValue();
                exportTaxAmountTotal += ((BigDecimal)map.get("exportTaxAmount")).doubleValue();

                map.put("exportAmount",
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(map.get("exportAmount").toString())));
                map.put("exportTaxAmount",
                    AmountFormatUtil.fmtMicrometer(MathUtil.round(map.get("exportTaxAmount").toString())));
                map.put("exportYxse", AmountFormatUtil.fmtMicrometer(MathUtil.round(map.get("exportYxse").toString())));
            }

            Map<String, Object> mapTj = null;
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (dto.getCurr() == 1) {
                mapTj = this.baseMapper.queryExportInvoiceSum(dto);
            }
            resultData.getTjData(mapTj);

        }
        return resultData.getPageData(result).toJsonBase();
    }
}
