/*
 * 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.Date8Util;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.xrtygis.custom.entity.gs.GdStatisReslut;
import com.tofly.xrtygis.custom.entity.gs.StatisEntity;
import com.tofly.xrtygis.entity.Gdreturn;
import com.tofly.xrtygis.entity.Pfrecord;
import com.tofly.xrtygis.service.GdreturnService;
import org.springframework.stereotype.Service;
import com.tofly.xrtygis.mapper.GdreturnMapper;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 工单回访记录表
 *
 * @author wangjieliu
 * @date Mon Jun 28 00:00:00 CST 2021
 */
@Service("gdreturnService")
public class GdreturnServiceImpl extends ServiceImpl<GdreturnMapper, Gdreturn> implements GdreturnService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean gdReturn(Gdreturn gdreturn) {
        String gdbh = gdreturn.getGdbh();
        if (StringUtil.isEmpty(gdbh)){
            throw new ToflyDeniedException("请传入工单编号");
        }
        //回访人员
        Long returnUser = SecurityUtils.getUserId();
        gdreturn.setReturnUser(returnUser);
        //回访时间
        gdreturn.setReturnTime(Date8Util.getCurrentDate());
        //修改工单回访次数
        baseMapper.updateGdReturnNum(gdbh);
        this.save(gdreturn);
        return true;
    }

    @Override
    public List<GdStatisReslut> getReturnStatis() {
        //获取工单数、应回访数、回访数
        List<GdStatisReslut> reslutList = baseMapper.getGdNum();
        //获取回访满意、不满意等评价数目
        List<GdStatisReslut> returnList = baseMapper.getReturnNum();
        //由于回访业务是热线的工单只有归档了才可以回访，所以只用判断回访就行了
        if (returnList.size() > 0){
            Map<Long , GdStatisReslut> lxAndReturnMap = returnList.stream().collect(Collectors.toMap(GdStatisReslut::getReturnGdlx , v->v));
            reslutList.forEach( k ->{
                GdStatisReslut reslut = lxAndReturnMap.get(k.getGdlx());
                k.setVeryMyNum(reslut.getVeryMyNum());
                k.setMyNum(reslut.getMyNum());
                k.setCommonNum(reslut.getCommonNum());
                k.setNoMyNum(reslut.getNoMyNum());
            });
        }
        return reslutList;
    }

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

        return this.page(page ,Wrappers.<Gdreturn>lambdaQuery()
                .eq(StringUtil.isNotNull(gdlx) , Gdreturn::getGdType , gdlx)
                .le(StringUtil.isNotNull(endTime) , Gdreturn::getReturnTime , endTime)
                .ge(StringUtil.isNotNull(startTime) , Gdreturn::getReturnTime , startTime));

    }

}
