package com.dxhy.extend.service.customs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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.CustomsDao;
import com.dxhy.extend.entity.CustomsRecordDO;
import com.dxhy.extend.entity.JkszhcxRespVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CustomsServiceImpl extends MpBaseServiceImpl<CustomsDao, CustomsRecordDO> implements CustomsService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    /**
     * 海关缴款书票池查询接口-改造自海关缴款书综合查询陈刚扬
     *
     * @param pramsMap
     * @param curr
     * @param size
     * @return java.lang.String
     * @author 陈刚扬
     * @date 2020/8/10
     */
    @Override
    public String selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 表头
        String[] titlelist = {"序号", "缴款书号码", "管理状态", "填发日期", "缴款单位名称", "缴款单位税号", "税款金额", "抵扣税款金额", "稽核结果", "是否申请核对",
            "核对结果", "认证状态", "认证类型", "认证日期", "认证方式", "认证人", "采集状态", "底账库采集时间", "采集来源", "签收方式", "签收状态", "签收日期", "签收人",
            "入账月份", "入账状态", "凭证号", "业务单元", "入池时间"};
        String[] titleprop = {"xh", "customsCode", "glzt", "fillInDate", "gfName", "gfTaxNo", "taxAmount",
            "yxTaxAmount", "auditStatus", "applyCheck", "checkStatus", "rzhYesorno", "bdkStatus", "rzhDate", "rzhType",
            "confirmUser", "cjzt", "createDate", "dataSources", "qsType", "qsStatus", "qsDate", "qsUser",
            "inAccountDate", "inAccountStatus", "voucherNumber", "businessName", "createDate"};
        // 需要设置返回列表标题时调用
        JSONObject json = super.setTitleList(titlelist, titleprop);
        try {

            if (CollectionUtils.isEmpty((List<String>)pramsMap.get("gfsh"))) {
                json.put("total", "0");
                json.put("datalist", new ArrayList<>());
                return super.responseResult(json);
            }
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjse = "0";
                Map<String, Object> hjxx = super.baseMapper.selectHjxx(pramsMap);
                if (hjxx != null) {
                    hjsl = (long)hjxx.get("hjsl") + "";
                    if (!"0".equals(hjsl)) {
                        hjsl = AmountFormatUtil.fmtMicrometer(hjsl);
                        hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get("hjse") + ""));
                    }
                }
                json.put("hjsl", hjsl);
                json.put("hjse", hjse);
            }
            PageHelper.startPage(curr, size);
            List<JkszhcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            PageInfo<JkszhcxRespVO> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                int start = new Long(pageInfo.getStartRow()).intValue();

                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    pageList.get(i).setYxTaxAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getYxTaxAmount())));
                    // 字典值转换
                    String auditStatus = pageList.get(i).getAuditStatus();
                    pageList.get(i)
                        .setAuditStatus(dictdetaServcice.queryDictName(DictConstant.AUDIT_STATUS, auditStatus));
                    String applyCheck = pageList.get(i).getApplyCheck();
                    pageList.get(i).setApplyCheck(dictdetaServcice.queryDictName(DictConstant.APPLY_CHECK, applyCheck));
                    String checkStatus = pageList.get(i).getCheckStatus();
                    pageList.get(i)
                        .setCheckStatus(dictdetaServcice.queryDictName(DictConstant.CHECK_STATUS, checkStatus));
                    if ("0".equals(pageList.get(i).getBdkStatus())) {
                        pageList.get(i).setBdkStatus("");
                    } else {
                        String bdkStatus = pageList.get(i).getBdkStatus();
                        pageList.get(i)
                            .setBdkStatus(dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, bdkStatus));
                    }
                    String glzt = pageList.get(i).getGlzt();
                    pageList.get(i).setGlzt(dictdetaServcice.queryDictName(DictConstant.GLZT, glzt));
                    String qsStatus = pageList.get(i).getQsStatus();
                    pageList.get(i).setQsStatus(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qsStatus));
                    String authStatus = pageList.get(i).getAuthStatus();
                    pageList.get(i)
                        .setAuthStatus(dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, authStatus));
                    String qsType = pageList.get(i).getQsType();
                    pageList.get(i).setQsType(dictdetaServcice.queryDictName(DictConstant.QS_TYPE, qsType));
                    String rzhYesorno = pageList.get(i).getRzhYesorno();
                    pageList.get(i).setRzhYesorno(dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, rzhYesorno));
                    // 入账状态字段
                    pageList.get(i).setInAccountStatus(dictdetaServcice.queryDictName(DictConstant.INACCOUNT_STATUS,
                        pageList.get(i).getInAccountStatus()));
                    // 采集状态
                    pageList.get(i).setCjzt(pageList.get(i).getCreateDate() == null ? "未采集" : "已采集");
                    //
                    pageList.get(i).setInAccountDate(pageList.get(i).getInAccountDate() == null ? ""
                        : pageList.get(i).getInAccountDate().replaceAll("-", "").substring(0, 6));
                    pageList.get(i).setCreateDate(pageList.get(i).getCreateDate() == null ? ""
                        : pageList.get(i).getCreateDate().trim().substring(0, 11));
                    // 认证类型
                    pageList.get(i)
                        .setRzlx(dictdetaServcice.queryDictName(DictConstant.INVOICE_RZLX, pageList.get(i).getRzlx()));
                    // 认证方式
                    pageList.get(i).setRzhType(
                        dictdetaServcice.queryDictName(DictConstant.DEDU_TYPE, pageList.get(i).getRzhType()));
                    // 采集来源
                    pageList.get(i).setDataSources(
                        dictdetaServcice.queryDictName(DictConstant.JKSCJFS, pageList.get(i).getDataSources()));
                }
            }
            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            List<JkszhcxRespVO> resultList = pageInfo.getList();
            json.put("datalist", CollectionUtils.isEmpty(resultList) ? new ArrayList<>() : resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }
        return super.responseResult(json);
    }

}
