package com.xhkjedu.sapi.service.appversion;

import com.xhkjedu.sapi.mapper.appversion.AppthirdMonitorMapper;
import com.xhkjedu.sapi.mapper.appversion.AppthirdMonitoranalyzeMapper;
import com.xhkjedu.sapi.mapper.system.SchoolMapper;
import com.xhkjedu.sapi.model.appversion.TAppthirdMonitor;
import com.xhkjedu.sapi.model.appversion.TAppthirdMonitoranalyze;
import com.xhkjedu.sapi.vo.appversion.AppSearchVo;
import com.xhkjedu.sapi.vo.appversion.AppthirdMonitorPVo;
import com.xhkjedu.sapi.vo.appversion.AppthirdMonitorVo;
import com.xhkjedu.sapi.vo.appversion.AppthirdMonitoranalyzeVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ywx
 * @classname AppthirdMonitorService
 * @description 
 * @date 2020/11/10 17:20
 **/
@Service
public class AppthirdMonitorService {
    @Resource
    private AppthirdMonitorMapper appthirdMonitorMapper;
    @Resource
    private AppthirdMonitoranalyzeMapper monitoranalyzeMapper;
    @Resource
    private SchoolMapper schoolMapper;

    //添加
    public Integer save(TAppthirdMonitor model) {
        model.setAmstate(0);
        model.setHandlenum(0);
        model.setCreatetime(N_Utils.getSecondTimestamp());
        appthirdMonitorMapper.insertUseGeneratedKeys(model);
        return model.getId();
    }

    //应用外监控未处理列表
    public List <AppthirdMonitorVo> listAll(AppSearchVo monitor,Integer year) {
        return appthirdMonitorMapper.listAll(monitor, year);
    }

    //获取年份
    public Integer getYear(Integer schoolid) {
        return schoolMapper.getYearById(schoolid);
    }
    public Integer getYear2(Integer userid) {
        return schoolMapper.getYearByUserId(userid);
    }

    //学生未处理监控详情
    public List<AppthirdMonitorPVo> listPeople(AppSearchVo monitor,Integer year) {
        return appthirdMonitorMapper.listPeople(monitor, year);
    }

    //标记为已处理
    public void handle(AppSearchVo monitor) {
        List<Integer> createids = monitor.getCreateids();
        if (N_Utils.isListEmpty(createids)) return;//要处理的学生为空直接返回
        Integer handleid = monitor.getCreateid();
        Integer year = schoolMapper.getYearById(monitor.getSchoolid());
        List<AppthirdMonitoranalyzeVo> analyzes = appthirdMonitorMapper.listByStudentId(monitor, year);
        for (Integer createid : createids) {
            List<AppthirdMonitoranalyzeVo> monitoranalyzes = analyzes.stream().filter(a -> a.getStudentid().equals(createid)).collect(Collectors.toList());
            for (AppthirdMonitoranalyzeVo monitoranalyze : monitoranalyzes) {
                TAppthirdMonitoranalyze analyze = new TAppthirdMonitoranalyze();
                analyze.setStudentid(createid);
                analyze.setAmapackage(monitoranalyze.getAmapackage());
                List<Integer> amids = monitoranalyze.getAmids();
                analyze.setAmanum(amids.size());
                Integer handlenum = monitoranalyze.getHandlenum();
                analyze.setHandlenum(handlenum);
                analyze.setHandleid(handleid);
                analyze.setHandletime(N_Utils.getSecondTimestamp());
                analyze.setSchoolid(monitor.getSchoolid());
                monitoranalyzeMapper.insertUseGeneratedKeys(analyze);
                Integer amaid = analyze.getId();
                analyze.setAmaid(amaid);
                appthirdMonitorMapper.handle(amids, analyze, N_Utils.getSecondTimestamp());
            }
        }
    }

    //应用外监控已处理列表
    public List<AppthirdMonitorVo> listAll2(AppSearchVo monitor,Integer year) {
        return appthirdMonitorMapper.listAll2(monitor, year);
    }

    //学生已处理监控详情
    public List<AppthirdMonitorPVo> listPeople2(AppSearchVo monitor) {
        Integer year = schoolMapper.getYearByUserId(monitor.getCreateid());
        return appthirdMonitorMapper.listPeople2(monitor, year);
    }
}
