/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.xrtygis.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.xrtygis.entity.Dmaicf;
import com.tofly.xrtygis.entity.Dmapietype;
import com.tofly.xrtygis.mapper.DmaicfMapper;
import com.tofly.xrtygis.service.DmaicfService;
import com.tofly.xrtygis.service.DmapietypeService;
import com.tofly.xrtygis.service.DmaregionService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 分区计量对应的基础设施的状况因子表
 *
 * @author admin
 * @date Tue May 18 00:00:00 CST 2021
 */
@Service("dmaicfService")
@AllArgsConstructor
public class DmaicfServiceImpl extends ServiceImpl<DmaicfMapper, Dmaicf> implements DmaicfService {

        private final DmapietypeService dmapietypeService;

    @Override
    public String getIcfByYear(Long mid, String bsign, String esign, int diffYear) {
        return baseMapper.getIcfByYear(mid , bsign , esign , diffYear);
    }

    @Override
    public IPage<Map> selectICF(Page<Dmaicf> page, Dmaicf dmaicf) {
        IPage iPage = this.page(page , Wrappers.query(dmaicf));

        //转map
        List<Map> map = JSONObject.parseArray(JSONObject.toJSONString(iPage.getRecords()) , Map.class);
        List<Dmapietype> typeList = dmapietypeService.list(Wrappers.<Dmapietype>lambdaQuery().select(Dmapietype::getMid , Dmapietype::getMname));
        //list转MAP
        Map<Long , String> typeMap = typeList.stream().collect(Collectors.toMap(Dmapietype::getMid , Dmapietype::getMname));
        map.forEach(k->{
            String mName= typeMap.get(Long.parseLong(k.get("mid")+""));
            k.put("mName" , mName);
        });
        iPage.setRecords(map);

        return iPage;
    }


}
