package com.dxhy.extend.service.tjbb.impl;

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.QsfsEnum;
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.common.util.StringHelper;
import com.dxhy.extend.dao.ExtQstjbDao;
import com.dxhy.extend.model.ExtQstjbRespVO;
import com.dxhy.extend.model.ExtTjbbHjxxVO;
import com.dxhy.extend.service.tjbb.IExtQstjbService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 * 签收统计表业务实现类
 *
 * @author 张宏 @date 2019年5月14日
 *
 */

@Service("extendqstjbService")
@Slf4j
public class ExtQstjbServiceImpl extends MpBaseServiceImpl<ExtQstjbDao, ExtQstjbRespVO> implements IExtQstjbService {
    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;
    /**
     * 签收状态码统一放入map中
     */
    public static final Map<String, String> QSZT_MAP = new HashMap<String, String>(16) {
        private static final long serialVersionUID = 3195016454748903072L;

        {
            for (FpqsjgEnum en : FpqsjgEnum.values()) {
                this.put(en.getQsjgDm(), en.getQsjgMcSmb(en.getIndex()));
            }
        }
    };
    /**
     * 签收方式统一放入map中
     */
    public static final Map<String, String> QSFS_MAP = new HashMap<String, String>(16) {
        private static final long serialVersionUID = 5137143201446095218L;

        {
            for (QsfsEnum en : QsfsEnum.values()) {
                this.put(en.getQsfsDm(), en.getQsfsMc(en.getIndex()));
            }
        }
    };

    @Override
    public String selectQstjbByCxtj(Map<String, Object> pramsMap, int curr, int size) {

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        String company = (String)pramsMap.get("company");
        String menuId = (String)pramsMap.get("menuId");
        JSONObject json = tabulateRedisService.getTabulateMenu(company, menuId);
        String hjje = "0.00";
        String hjse = "0.00";

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<ExtQstjbRespVO> pageList = super.baseMapper.selectQstjbByCxtj(pramsMap);
        PageInfo<ExtQstjbRespVO> pageInfo = new PageInfo<>(pageList);
        List<ExtQstjbRespVO> resultList = pageInfo.getList();

        // 设置记录总数
        json.put("total", pageInfo.getTotal());

        if (resultList != null && resultList.size() > 0) {
            int start = (int)pageInfo.getStartRow();
            for (int i = 0; i < resultList.size(); i++) {
                if (!StringHelper.isBlank(resultList.get(i).getJe())) {
                    resultList.get(i).setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getJe())));
                }
                if (!StringHelper.isBlank(resultList.get(i).getSe())) {
                    resultList.get(i).setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getSe())));
                }
                //字典转换
                String qszt = resultList.get(i).getQszt();
                resultList.get(i).setQszt(dictdetaServcice.queryDictName(DictConstant.QS_RESULTS,qszt));
                String qsfs = resultList.get(i).getQsfs();
                resultList.get(i).setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,qsfs));

                resultList.get(i).setXh(String.valueOf(i + start));
            }
        }

        if (1 == curr) {
            List<ExtTjbbHjxxVO> hjxx = super.baseMapper.qstjbHjxxByCxtj(pramsMap);
            if (!StringHelper.isBlank(hjxx.get(0).getHjje())) {
                hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjje()));
            }
            if (!StringHelper.isBlank(hjxx.get(0).getHjse())) {
                hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjse()));
            }
            json.put("hjje", hjje);
            json.put("hjse", hjse);
        }
        json.put("datalist", resultList);

        return super.responseResult(json);
    }

    @Override
    public String selectYqswrzByCxtj(Map<String, Object> pramsMap, int curr, int size) {

        String company = (String)pramsMap.get("company");
        String menuId = (String)pramsMap.get("menuId");
        JSONObject json = tabulateRedisService.getTabulateMenu(company, menuId);

        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        String hjje = "0.00";
        String hjse = "0.00";

        // 分页获取明细列表
        PageHelper.startPage(curr, size);
        List<ExtQstjbRespVO> pageList = super.baseMapper.selectYqswrzByCxtj(pramsMap);
        PageInfo<ExtQstjbRespVO> pageInfo = new PageInfo<>(pageList);
        List<ExtQstjbRespVO> resultList = pageInfo.getList();

        // 设置记录总数
        json.put("total", pageInfo.getTotal());

        if (resultList != null && resultList.size() > 0) {
            int start = (int)pageInfo.getStartRow();
            for (int i = 0; i < resultList.size(); i++) {

                if (!StringHelper.isBlank(resultList.get(i).getJe())) {
                    resultList.get(i).setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getJe())));
                }
                if (!StringHelper.isBlank(resultList.get(i).getSe())) {
                    resultList.get(i).setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getSe())));
                }
                //字典值转换
                String qszt = resultList.get(i).getQszt();
                resultList.get(i).setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS,qszt));
                String qsfs = resultList.get(i).getQsfs();
                resultList.get(i).setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,qsfs));

                resultList.get(i).setXh(String.valueOf(i + start));
            }
        }
        if (1 == curr) {
            List<ExtTjbbHjxxVO> hjxx = super.baseMapper.yqswrzHjxxByCxtj(pramsMap);
            if (!StringHelper.isBlank(hjxx.get(0).getHjje())) {
                hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjje()));
            }
            if (!StringHelper.isBlank(hjxx.get(0).getHjse())) {
                hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.get(0).getHjse()));
            }
            json.put("hjje", hjje);
            json.put("hjse", hjse);
        }
        json.put("datalist", resultList);

        return super.responseResult(json);
    }

}
