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

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.common.core.util.StringUtil;
import com.tofly.xrtygis.custom.entity.gs.GdQualityStatis;
import com.tofly.xrtygis.custom.entity.gs.GdStatisReslut;
import com.tofly.xrtygis.custom.entity.gs.RecordStatis;
import com.tofly.xrtygis.custom.entity.gs.StatisEntity;
import com.tofly.xrtygis.entity.Gdreturn;
import com.tofly.xrtygis.entity.Pfrecord;
import com.tofly.xrtygis.mapper.PfrecordMapper;
import com.tofly.xrtygis.service.PfrecordService;
import org.springframework.stereotype.Service;

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

/**
 * 评分记录表
 *
 * @author wangjieliu
 * @date Thu Jun 17 00:00:00 CST 2021
 */
@Service("pfrecordService")
public class PfrecordServiceImpl extends ServiceImpl<PfrecordMapper, Pfrecord> implements PfrecordService {


    @Override
    public List<GdQualityStatis> getQualityStatis() {
        //获取工单应质检数
        List<GdQualityStatis> reslutList = baseMapper.getGdQualityNum();
        //获取质检满意、不满意等质检数目
        List<GdQualityStatis> qualityList = baseMapper.getQualityNum();
        if (qualityList.size() > 0) {
            Map<Long, GdQualityStatis> lxAndQualityMap = qualityList.stream().collect(Collectors.toMap(GdQualityStatis::getQualityGdlx, v -> v));
            reslutList.forEach(k -> {
                GdQualityStatis reslut = lxAndQualityMap.get(k.getGdlx());
                k.setRealityQualityNum(reslut.getRealityQualityNum());
                k.setVeryMyNum(reslut.getVeryMyNum());
                k.setMyNum(reslut.getMyNum());
                k.setCommonNum(reslut.getCommonNum());
                k.setNoMyNum(reslut.getNoMyNum());
            });
        }
        return reslutList;
    }

    @Override
    public List<RecordStatis> getRecordStatis() {
        return baseMapper.getRecordNum();
    }

    @Override
    public IPage<Pfrecord> getGdQuality(Page page, StatisEntity entity) {
        Long gdlx = entity.getGdlx();
        Date startTime = entity.getStartTime();
        Date endTime = entity.getEndTime();

        return this.page(page, Wrappers.<Pfrecord>lambdaQuery().eq(Pfrecord::getPfType, "1")
                .eq(StringUtil.isNotNull(gdlx), Pfrecord::getGdType, gdlx)
                .le(StringUtil.isNotNull(endTime), Pfrecord::getPfsj, endTime)
                .ge(StringUtil.isNotNull(startTime), Pfrecord::getPfsj, startTime)
                .select(Pfrecord::getPfGdbh , Pfrecord::getGdType , Pfrecord::getPfry , Pfrecord::getPfsj ,
                        Pfrecord::getPfMs , Pfrecord::getPfFz , Pfrecord::getQualityReslut , Pfrecord::getIsreslut));
    }

    @Override
    public IPage<Pfrecord> getLyQuality(Page page, StatisEntity entity) {
        String callType = entity.getCallType();
        Date startTime = entity.getStartTime();
        Date endTime = entity.getEndTime();

        return this.page(page , Wrappers.<Pfrecord>lambdaQuery().eq(Pfrecord::getPfType , "2")
                .eq(StringUtil.isNotEmpty(callType) , Pfrecord::getCallType , callType)
                .le(StringUtil.isNotNull(endTime), Pfrecord::getPfsj, endTime)
                .ge(StringUtil.isNotNull(startTime), Pfrecord::getPfsj, startTime));
    }

    @Override
    public boolean updateGdQualityState(String pfGdbh) {
        return baseMapper.updateGdQualityState(pfGdbh);
    }
}
