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

import cn.hutool.core.util.ObjectUtil;
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.DomesticExportCustomsDao;
import com.dxhy.api.model.custom.TDxExportCustoms;
import com.dxhy.common.utils.ResultData;
import com.dxhy.domestic.model.dto.TdxExportCustomsModelDTO;
import com.dxhy.domestic.service.count.DomesticCustomsExportCountService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @ProjectName: dev
 * @author : ddx
 */
@Service
public class DomesticCustomsExportCountServiceImpl extends MpBaseServiceImpl<DomesticExportCustomsDao, TDxExportCustoms>
    implements DomesticCustomsExportCountService {

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

    @Override
    public String queryExportCustomsList(Page page, TdxExportCustomsModelDTO 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.getUserInfo().getCompany(), dto.getMenuId());

        resultData.putTabulateMenu(tabulateMenu);

        if (StringUtils.isBlank(dto.getGfsh()) && dto.getGfTaxNo().size() <= 0) {
            resultData.setDataList(new ArrayList<>());
            return resultData.toJsonBase();
        }

        Page<Map<String, Object>> result = this.baseMapper.queryExportCustomsList(page, dto);
        if (CollectionUtils.isNotEmpty(result.getRecords())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            double exportAmountTotal = 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("fillInDate") != null) {
                    map.put("fillInDate", map.get("fillInDate"));
                }
                if (map.get("rzhDate") != null) {
                    map.put("rzhDate", map.get("rzhDate"));
                }
                // 字典转换
                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 cxrzStatus = (String)map.get("cxrzStatus");
                map.put("cxrzStatus", dictdetaServcice.queryDictName(DictConstant.CXRZ_STATUS, cxrzStatus));
                String dataSources = (String)map.get("dataSources");
                map.put("dataSources", dictdetaServcice.queryDictName(DictConstant.JKSCJFS, dataSources));
                String glzt = (String)map.get("glzt");
                map.put("glzt", dictdetaServcice.queryDictName(DictConstant.GLZT, glzt));

                if (StringUtils.isNotBlank((String) map.get("exportTaxAmount"))) {
                    exportAmountTotal += Double.parseDouble((String)map.get("exportTaxAmount"));
                    map.put("exportTaxAmount",
                            AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("exportTaxAmount"))));
                }

                if(ObjectUtil.isNotEmpty(map.get("yxExportTaxAmount"))){
                    map.put("yxExportTaxAmount",
                            AmountFormatUtil.fmtMicrometer(MathUtil.round((String)map.get("yxExportTaxAmount"))));
                }

                map.put("select", true);
            }
            Map mapTj = null;
            if (dto.getCurr() == 1) {
                // 合计信息,仅第一页时需要统计，减少数据库压力
                mapTj = this.baseMapper.queryExportCustomsSum(dto);
            }
            resultData.getTjBdData(mapTj);
        }
        return resultData.getPageData(result).toJsonBase();
    }
}
