package com.dxhy.core.service.export.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.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.utils.CommonUtil;
import com.dxhy.core.dao.CoreQssbDao;
import com.dxhy.core.model.QssbRespVO;
import com.dxhy.core.service.export.IQssbService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 签收失败统计业务实现类
 *
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("coreqssbService")
@Slf4j

public class QssbServiceImpl extends MpBaseServiceImpl<CoreQssbDao, QssbRespVO> implements IQssbService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectQssbBySmb(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json = new JSONObject();

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

        PageHelper.startPage(curr, size);
        List<QssbRespVO> pageList = super.baseMapper.selectQssbBySmb(pramsMap);
        PageInfo<QssbRespVO> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());

        // 用于页面展示
        List<QssbRespVO> resultList = pageInfo.getList();
        if (resultList != null && resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setXh(String.valueOf((curr - 1) * size + i + 1));
                if(StringUtils.isNotEmpty(resultList.get(i).getJe())){
                    resultList.get(i).setJe(CommonUtil.jeclNullToHyphen(resultList.get(i).getJe()));
                }
                if(StringUtils.isNotEmpty(resultList.get(i).getSe())){
                    resultList.get(i).setSe(CommonUtil.jeclNullToHyphen(resultList.get(i).getSe()));
                }
                //字典值转换
                resultList.get(i).setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,resultList.get(i).getQsfs()));
                resultList.get(i).setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,resultList.get(i).getInvoiceType()));
            }
        }

        json.put("datalist", resultList);

        return json;
    }

}
