package com.css.txw.gxzx.service.lsjr.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.ggzc.framework.cache.utils.CacheUtils;
import com.css.ggzc.framework.cache.vo.DmbDTO;
import com.css.ggzc.framework.common.pojo.PageResult;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.util.json.JsonUtils;
import com.css.txw.gxzx.mapper.lsjr.GxzxDkbxsqxxMapper;
import com.css.txw.gxzx.pojo.domain.lsjr.GxzxDkbxsqxx;
import com.css.txw.gxzx.pojo.domain.lsjr.GxzxLsjrcpxx;
import com.css.txw.gxzx.pojo.lsjr.GxzxLsjrcpxxDto;
import com.css.txw.gxzx.pojo.lsjr.XdbxcpsqRequestVO;
import com.css.txw.gxzx.pojo.vo.lsjr.ProductApplyVO;
import com.css.txw.gxzx.service.lsjr.GxzxDkbxsqxxService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class GxzxDkbxsqxxServiceImpl extends ServiceImpl<GxzxDkbxsqxxMapper, GxzxDkbxsqxx> implements GxzxDkbxsqxxService {
	
	@Resource
	private GxzxDkbxsqxxMapper dkbxsqxxMapper;
	@Override
	public PageResult<ProductApplyVO> queryXdbxcpsqInfo(XdbxcpsqRequestVO requestVO) {
		PageResult<ProductApplyVO> pageResult = new PageResult<>();
	    pageResult.setTotal(0L);
	    Integer pageNum = requestVO.getPageNum();
        Integer pageSize = requestVO.getPageSize();
        Page<GxzxDkbxsqxx> page = new Page<>(pageNum, pageSize);
        Page<ProductApplyVO> sqxxPage= dkbxsqxxMapper.queryXdbxcpsqInfo(page,requestVO);
//		if (GyUtils.isNotNull(sqxxPage)) {
//			pageResult.setTotal(sqxxPage.getTotal());
//		    pageResult.setList(sqxxPage.getRecords());
//		}
		if (GyUtils.isNotNull(sqxxPage) && GyUtils.isNotNull(sqxxPage.getRecords())) {
            List<ProductApplyVO> records = sqxxPage.getRecords();
            List<DmbDTO> xdlxList = CacheUtils.getDmbData("LSJR_XDYWLX");
            List<DmbDTO> bxlxList = CacheUtils.getDmbData("LSJR_BXYWLX");
            Map<String, String> xdlxMap = xdlxList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));
            Map<String, String> bxlxMap = bxlxList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));
            List<DmbDTO> carbonTypeList = CacheUtils.getDmbData("CARBON_TYPE");
            Map<String, String> carbonTypeMap = carbonTypeList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));
            for (ProductApplyVO dto : records) {
                String ywlxDm = dto.getYwlxDm();
                String sshyDm = dto.getSshyDm();
                String ywlxmc = null;
                if (GyUtils.isEquals(sshyDm, "xd")) {
                	ywlxmc = xdlxMap.get(ywlxDm);
				}
                else {
                	ywlxmc = bxlxMap.get(ywlxDm);
                }
                dto.setYwlxmc(ywlxmc);
                Integer carbonType = dto.getCarbonType();
                String carbonTypeDesc = carbonTypeMap.get(carbonType + "");
                dto.setCarbonTypeMc(carbonTypeDesc);
                // 设置logo
                String jgid = dto.getFxjgid();
                String tableName = "xd".equals(sshyDm) ? "LSJRYH" : "LSJRBXJG";
                String cplxmc = "xd".equals(sshyDm) ? "信贷" : "保险";
                dto.setCplxmc(cplxmc);
                Map<String, Object> tableData = CacheUtils.getTableData(tableName, jgid);
                if (GyUtils.isNotNull(tableData)) {
                    dto.setYxlogo(GyUtils.isNotNull(tableData.get("bz")) ? tableData.get("bz").toString() : null);
                }
            }
            pageResult.setTotal(sqxxPage.getTotal());
		    pageResult.setList(sqxxPage.getRecords());
        }
	    return pageResult;
	}

}