package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dao.*;
import com.qmth.jkserver.dto.ExamRoomMonitorInfoDto;
import com.qmth.jkserver.enums.ExamStatus;
import com.qmth.jkserver.model.*;
import com.qmth.jkserver.params.ExamRoomMonitorInfoParams;
import com.qmth.jkserver.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class ExamRoomMonitorInfoServiceImpl extends ServiceImpl<ExamRoomMonitorInfoDao, ExamRoomMonitorInfo> implements ExamRoomMonitorInfoService {

    @Resource
    private ExamRoomMonitorInfoDao examRoomMonitorInfoDao;

    @Resource
    @Lazy
    private JkExamPlanService examPlanService;

//    @Autowired
//    private MongoTemplate mongoTemplate;

    @Resource
    private ExamPlanDao examPlanDao;

    @Resource
    @Lazy
    private ExamRoomMonitorInfoService examRoomMonitorInfoService;

    @Resource
    JkExamRoomAddressService jkExamRoomAddressService;

    @Resource
    JkExamRoomService jkExamRoomService;

    @Resource
    @Lazy
    ExamStudentService examStudentService;

    @Resource
    ExamRoomAddressDao examRoomAddressDao;

    @Resource
    ExamStudentDao examStudentDao;

    @Resource
    StudentInfoDao studentInfoDao;

    @Override
    public void save(List<ExamRoomMonitorInfo> examRoomMonitors) {
        if (examRoomMonitors.size() > 0) {
            examRoomMonitorInfoService.saveOrUpdateBatch(examRoomMonitors);
        }
    }

    @Override
    public Page<ExamRoomMonitorInfoDto> pageQuery(ExamRoomMonitorInfoParams params, int curPage, int pageSize) {
//        Query query = new Query();
        QueryWrapper<ExamRoomAddress> examRoomAddressQueryWrapper = new QueryWrapper<>();
        Set<String> placeCodeSet = new HashSet<>();
        Set<String> roomCodeSet = new HashSet<>();
//        query.addCriteria(Criteria.where("examCode").is(params.getExamCode()));
        examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getExamCode, params.getExamCode());
        if (StringUtils.isNotEmpty(params.getProvince())) {
//            query.addCriteria(Criteria.where("province").is(params.getProvince()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getProvince, params.getProvince());
        }
        if (StringUtils.isNotEmpty(params.getCity())) {
//            query.addCriteria(Criteria.where("city").is(params.getCity()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getCity, params.getCity());
        }
        if (StringUtils.isNotEmpty(params.getRoomCode())) {
//            query.addCriteria(Criteria.where("roomCode").is(params.getRoomCode()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getRoomCode, params.getRoomCode());
        }
        if (StringUtils.isNotEmpty(params.getPlaceCode())) {
//            query.addCriteria(Criteria.where("placeCode").is(params.getPlaceCode()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getPlaceCode, params.getPlaceCode());
        }
        if (StringUtils.isNotEmpty(params.getPlaceName())) {
//            Pattern pattern = Pattern.compile("^.*" + params.getPlaceName() + ".*$", Pattern.CASE_INSENSITIVE);
//            query.addCriteria(Criteria.where("placeName").regex(pattern));
            examRoomAddressQueryWrapper.lambda().like(ExamRoomAddress::getPlaceName, params.getPlaceName());
        }
        List<ExamRoomMonitorInfoDto> infoDtos = new ArrayList<ExamRoomMonitorInfoDto>();
        //查询所有考点
//        List<ExamRoomAddress> list = mongoTemplate.find(query, ExamRoomAddress.class);
        List<ExamRoomAddress> list = jkExamRoomAddressService.list(examRoomAddressQueryWrapper);
        if (list != null && list.size() > 0) {
            for (ExamRoomAddress examRoomAddress : list) {
                placeCodeSet.add(examRoomAddress.getPlaceCode());
                roomCodeSet.add(examRoomAddress.getRoomCode());
            }
        }
//        Query queryInfos = new Query();
//        Criteria criteria = new Criteria();
//        criteria.and("examCode").is(params.getExamCode());
        QueryWrapper<ExamRoomMonitorInfo> examRoomMonitorInfoQueryWrapper = new QueryWrapper<>();
        examRoomMonitorInfoQueryWrapper.lambda().eq(ExamRoomMonitorInfo::getExamCode, params.getExamCode());
        if (placeCodeSet.size() > 0) {
//            criteria.and("placeCode").in(placeCodeSet);
            examRoomMonitorInfoQueryWrapper.lambda().in(ExamRoomMonitorInfo::getPlaceCode, placeCodeSet);
        }
        if (roomCodeSet.size() > 0) {
//            criteria.and("roomCode").in(roomCodeSet);
            examRoomMonitorInfoQueryWrapper.lambda().in(ExamRoomMonitorInfo::getRoomCode, roomCodeSet);
        }
//        queryInfos.addCriteria(criteria);
//        long count = this.mongoTemplate.count(queryInfos, ExamRoomMonitorInfo.class);
//        long count = examRoomMonitorInfoService.count(examRoomMonitorInfoQueryWrapper);
//        queryInfos.limit(pageSize);
//        queryInfos.skip((curPage - 1) * pageSize);
//        List<ExamRoomMonitorInfo> examRoomMonitors = this.mongoTemplate.find(queryInfos, ExamRoomMonitorInfo.class);
        IPage<ExamRoomMonitorInfo> examRoomMonitors = examRoomMonitorInfoDao.findByExamCodeAndPlaceCodeAndRoomCode(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(curPage, pageSize),
                params.getExamCode(), placeCodeSet, roomCodeSet);
        ExamPlan examPlan = examPlanService.findByExamCode(params.getExamCode());
        for (ExamRoomMonitorInfo examRoomMonitorInfo : examRoomMonitors.getRecords()) {
            ExamRoomMonitorInfoDto dto = new ExamRoomMonitorInfoDto();
            ExamRoom examRoom = findRoom(examRoomMonitorInfo.getExamCode(), examRoomMonitorInfo.getPlaceCode(), examRoomMonitorInfo.getRoomCode());
            dto.setExamPlan(examPlan);
            dto.setPlaceCode(examRoomMonitorInfo.getPlaceCode());
            dto.setRoomCode(examRoomMonitorInfo.getRoomCode());
            dto.setPlaceName(examRoom.getPlaceName());
            dto.setRoomName(examRoom.getRoomName());
            dto.setYkCount(countYkNumber(examRoomMonitorInfo.getExamCode(),
                    examRoomMonitorInfo.getPlaceCode(), examRoomMonitorInfo.getRoomCode(), examRoomMonitorInfo.getCourseCode()));
            dto.setLoginCount(examRoomMonitorInfo.getLoginCount());
            dto.setContactErrorCount(examRoomMonitorInfo.getContactErrorCount());
            dto.setCourseName(findCourseName(examRoomMonitorInfo.getExamCode(),
                    examRoomMonitorInfo.getPlaceCode(), examRoomMonitorInfo.getRoomCode(), examRoomMonitorInfo.getCourseCode()));
            infoDtos.add(dto);
        }
        return new PageImpl<ExamRoomMonitorInfoDto>(infoDtos, PageRequest.of(curPage - 1, pageSize), examRoomMonitors.getTotal());
    }


    public ExamRoom findRoom(String examCode, String placeCode, String roomCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("placeCode").is(placeCode));
//        query.addCriteria(Criteria.where("roomCode").is(roomCode));
//        List<ExamRoom> examRooms = mongoTemplate.find(query, ExamRoom.class);
        QueryWrapper<ExamRoom> examRoomQueryWrapper = new QueryWrapper<>();
        examRoomQueryWrapper.lambda().eq(ExamRoom::getExamCode, examCode)
                .eq(ExamRoom::getPlaceCode, placeCode)
                .eq(ExamRoom::getRoomCode, roomCode);
        List<ExamRoom> examRooms = jkExamRoomService.list(examRoomQueryWrapper);
        return examRooms.size() > 0 ? examRooms.get(0) : null;
    }

    public String findPlaceName(String examCode, String placeCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("placeCode").is(placeCode));
//        List<ExamRoom> examRooms = mongoTemplate.find(query, ExamRoom.class);
        QueryWrapper<ExamRoom> examRoomQueryWrapper = new QueryWrapper<>();
        examRoomQueryWrapper.lambda().eq(ExamRoom::getExamCode, examCode)
                .eq(ExamRoom::getPlaceCode, placeCode);
        List<ExamRoom> examRooms = jkExamRoomService.list(examRoomQueryWrapper);
        return examRooms.size() > 0 ? examRooms.get(0).getPlaceName() : null;
    }

    public long countYkNumber(String examCode, String placeCode, String roomCode, String courseCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("placeCode").is(placeCode));
//        query.addCriteria(Criteria.where("roomCode").is(roomCode));
//        query.addCriteria(Criteria.where("courseCode").is(courseCode));
//        return mongoTemplate.count(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getPlaceCode, placeCode)
                .eq(ExamStudent::getRoomCode, roomCode)
                .eq(ExamStudent::getCourseCode, courseCode);
        return (long) examStudentService.count(examStudentQueryWrapper);
    }

    public String findCourseName(String examCode, String placeCode, String roomCode, String courseCode) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(examCode));
//        query.addCriteria(Criteria.where("courseCode").is(courseCode));
//        query.limit(1);
//        List<ExamStudent> examStudents = mongoTemplate.find(query, ExamStudent.class);
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                .eq(ExamStudent::getCourseCode, courseCode)
                .last(" limit 1 ");
        List<ExamStudent> examStudents = examStudentService.list(examStudentQueryWrapper);
        if (examStudents.size() > 0) {
            return examStudents.get(0).getCourseName();
        } else {
            return null;
        }
    }

    @Override
    public Object getExamInProgress() {
//        Date date = new Date();
//        Query query = new Query(Criteria.where("startTime").lte(date).and("endTime").gte(date).and("examStatus").is(ExamStatus.PUBLISHED));
//        query.with(new Sort(Sort.Direction.ASC, "startTime"));
        long time = System.currentTimeMillis();
        QueryWrapper<ExamPlan> examPlanQueryWrapper = new QueryWrapper<>();
        examPlanQueryWrapper.lambda().le(ExamPlan::getEndTime, time)
                .ge(ExamPlan::getEndTime, time)
                .eq(ExamPlan::getExamStatus, ExamStatus.PUBLISHED);
//        return mongoTemplate.find(query, ExamPlan.class);
        return examPlanService.list(examPlanQueryWrapper);
    }

    @Override
    public List<Map<String, Object>> getPlaceByProvinceAndCityAndExamCode(String province, String city, String examCode) {
        List<ExamPlan> list = examPlanDao.findByExamCode(examCode);
        if (list.size() == 0) {
            throw new JkServerException("错误的考试识别码:" + examCode);
        }
//        Criteria criteria = Criteria.where("examCode").is(examCode);
//        if (StringUtils.isNotEmpty(province)) {
//            criteria = criteria.and("province").is(province);
//        }
//        if (StringUtils.isNotEmpty(city)) {
//            criteria = criteria.and("city").is(city);
//        }
//        AggregationOptions aggregationOptions = new AggregationOptions.Builder().allowDiskUse(true).build();
//        TypedAggregation<ExamRoomAddress> aggregation = Aggregation.newAggregation(ExamRoomAddress.class,
//                Aggregation.match(criteria),
//                Aggregation.group("placeCode", "placeName").count().as("count"),
//                Aggregation.sort(Sort.Direction.ASC, "placeCode"),
//                Aggregation.project("placeCode", "placeName")).withOptions(aggregationOptions);
//        AggregationResults<DBObject> map = mongoTemplate.aggregate(aggregation, ExamRoomAddress.class, DBObject.class);
//        List<DBObject> results = map.getMappedResults();
        List<Map> results = examRoomAddressDao.findByProvinceAndCity(province, city);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map object : results) {
            Map<String, Object> data = new HashMap<>();
            data.put("name", object.get("placeName"));
            data.put("value", object.get("placeCode"));
            result.add(data);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getProvinceByExamCode(String examCode) {
//        Query query = new Query(Criteria.where("examCode").is(examCode));
//        List<ExamRoomAddress> list = mongoTemplate.find(query, ExamRoomAddress.class);
        QueryWrapper<ExamRoomAddress> examRoomAddressQueryWrapper = new QueryWrapper<>();
        examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getExamCode, examCode);
        List<ExamRoomAddress> list = jkExamRoomAddressService.list(examRoomAddressQueryWrapper);
        if (list != null && list.size() > 0) {
            List<Map<String, Object>> result = list.stream().map(m -> {
                Map<String, Object> map = new HashMap();
                map.put("id", m.getProvince());
                map.put("name", m.getProvince());
                return map;
            }).distinct().collect(Collectors.toList());
            return result;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> getCityByProvince(String province) {
//        Query query = new Query(Criteria.where("province").is(province));
//        List<ExamRoomAddress> list = mongoTemplate.find(query, ExamRoomAddress.class);
        QueryWrapper<ExamRoomAddress> examRoomAddressQueryWrapper = new QueryWrapper<>();
        examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getProvince, province);
        List<ExamRoomAddress> list = jkExamRoomAddressService.list(examRoomAddressQueryWrapper);
        if (list != null && list.size() > 0) {
            List<Map<String, Object>> result = list.stream().map(m -> {
                Map<String, Object> map = new HashMap();
                map.put("id", m.getCity());
                map.put("name", m.getCity());
                return map;
            }).distinct().collect(Collectors.toList());

            return result;
        }
        return null;
    }

    @Override
    public Object queryCountInfo(ExamRoomMonitorInfoParams params) {
//        Query where = new Query();
        QueryWrapper<ExamRoomAddress> examRoomAddressQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(params.getProvince())) {
//            where.addCriteria(Criteria.where("province").is(params.getProvince()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getProvince, params.getProvince());
        }
        if (StringUtils.isNotEmpty(params.getCity())) {
//            where.addCriteria(Criteria.where("city").is(params.getCity()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getCity, params.getCity());
        }
        if (StringUtils.isNotEmpty(params.getRoomCode())) {
//            where.addCriteria(Criteria.where("roomCode").is(params.getRoomCode()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getRoomCode, params.getRoomCode());
        }
        if (StringUtils.isNotEmpty(params.getPlaceCode())) {
//            where.addCriteria(Criteria.where("placeCode").is(params.getPlaceCode()));
            examRoomAddressQueryWrapper.lambda().eq(ExamRoomAddress::getPlaceCode, params.getPlaceCode());
        }
        if (StringUtils.isNotEmpty(params.getPlaceName())) {
//            Pattern pattern = Pattern.compile("^.*" + params.getPlaceName() + ".*$", Pattern.CASE_INSENSITIVE);
//            where.addCriteria(Criteria.where("placeName").regex(pattern));
            examRoomAddressQueryWrapper.lambda().like(ExamRoomAddress::getPlaceName, params.getPlaceName());
        }
//        List<ExamRoomAddress> list = mongoTemplate.find(where, ExamRoomAddress.class);
        List<ExamRoomAddress> list = jkExamRoomAddressService.list(examRoomAddressQueryWrapper);
        Set<String> placeCodeSet = new HashSet<>();
        Set<String> roomCodeSet = new HashSet<>();
        if (list != null && list.size() > 0) {
            for (ExamRoomAddress examRoomAddress : list) {
                placeCodeSet.add(examRoomAddress.getPlaceCode());
                roomCodeSet.add(examRoomAddress.getRoomCode());
            }
        }
//        Criteria criteria = new Criteria();
//        criteria.and("examCode").is(params.getExamCode());
        QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
        if (placeCodeSet.size() > 0) {
//            criteria.and("placeCode").in(placeCodeSet);
            examStudentQueryWrapper.lambda().in(ExamStudent::getPlaceCode, placeCodeSet);
        }
        if (roomCodeSet.size() > 0) {
//            criteria.and("roomCode").in(roomCodeSet);
            examStudentQueryWrapper.lambda().in(ExamStudent::getRoomCode, roomCodeSet);
        }
//        Query query = new Query();
//        query.addCriteria(Criteria.where("examCode").is(params.getExamCode()));
        examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, params.getExamCode());
        //计算应考考次
//        long countYkkc = mongoTemplate.count(query, ExamStudent.class);
        long countYkkc = examStudentService.count(examStudentQueryWrapper);
        //计算报名人数
//        TypedAggregation<ExamStudent> aggregation = Aggregation.newAggregation(
//                ExamStudent.class,
//                Aggregation.match(Criteria.where("examCode").is(params.getExamCode())),
//                Aggregation.group("ticketNo")
//        );
//        List<DBObject> pvs = mongoTemplate.aggregate(aggregation, ExamStudent.class, DBObject.class).getMappedResults();
        List<ExamStudent> pvs = examStudentDao.findByExamCode(params.getExamCode());
        long countBm = 0;
        if (pvs != null && pvs.size() > 0) {
            countBm = pvs.size();
        }
//        TypedAggregation<ExamRoomMonitorInfo> aggregation1 = Aggregation.newAggregation(
//                ExamRoomMonitorInfo.class,
//                Aggregation.match(criteria),
//                Aggregation.group("examCode", "placeCode", "roomCode").sum("loginCount").as("countDl").sum("contactErrorCount").as("countGz")
//        );
//        List<DBObject> cntList = mongoTemplate.aggregate(aggregation1, ExamRoomMonitorInfo.class, DBObject.class).getMappedResults();
        List<Map> cntList = examRoomMonitorInfoDao.findByExamCodeAndPlaceCodeAndRoomCodeByList(params.getExamCode(), placeCodeSet, roomCodeSet);
        Map map = new HashMap();
        long countDl = 0, countGz = 0;
        if (cntList != null && cntList.size() > 0) {
            for (Map dbObject : cntList) {
                countDl = countDl + (long) dbObject.get("countDl");
                countGz = countGz + (long) dbObject.get("countGz");
            }
        }
        map.put("countBm", countBm);
        map.put("countYkkc", countYkkc);
        map.put("countDl", countDl);
        map.put("countGz", countGz);
        return map;
    }

    @Override
    public List<Map<String, Object>> queryCategoryPercent(String examCode) {
//        Criteria criteria = new Criteria();
//        criteria.andOperator(Criteria.where("examCode").is(examCode));
//        TypedAggregation<StudentInfo> aggregation = Aggregation.newAggregation(
//                StudentInfo.class,
//                Aggregation.match(criteria),
//                Aggregation.group("category").count().as("countCategory"),
//                Aggregation.sort(Sort.Direction.ASC, "category"));
//        List<DBObject> list = mongoTemplate.aggregate(aggregation, StudentInfo.class, DBObject.class).getMappedResults();
        List<Map> list = studentInfoDao.findByExamCode(examCode);
        if (list != null && list.size() > 0) {
            int sum = list.stream().mapToInt(map -> (int) map.get("countCategory")).sum();
            List<Map<String, Object>> result = new ArrayList<>();
            for (Map object : list) {
                Map<String, Object> data = new HashMap<>();
//                data.put("category", object.get("_id"));
                data.put("category", object.get("category"));
                //计算各类别占比
                BigDecimal count = new BigDecimal((int) object.get("countCategory"));
                BigDecimal percent = sum == 0 ? BigDecimal.ZERO : count.divide(new BigDecimal(sum), 4, BigDecimal.ROUND_HALF_UP);
                data.put("percent", percent);
                result.add(data);
            }
            return result;
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> queryPlacePercent(String examCode) {
//        TypedAggregation<ExamStudent> aggregation = Aggregation.newAggregation(
//                ExamStudent.class,
//                Aggregation.match(Criteria.where("examCode").is(examCode)),
//                Aggregation.group("examCode", "placeCode").sum("loginCount").as("count1").sum("contactErrorCount").as("count2")
//        );
//        List<DBObject> pvs = mongoTemplate.aggregate(aggregation, ExamRoomMonitorInfo.class, DBObject.class).getMappedResults();
        List<Map> pvs = examRoomMonitorInfoDao.findByExamCodeAndPlaceCodeAndRoomCodeByList(examCode, null, null);
        List<Map<String, Object>> result = new ArrayList<>();
        if (pvs != null && pvs.size() > 0) {
            for (Map dbObject : pvs) {
                String placeCode = dbObject.get("placeCode").toString();
                String placeName = findPlaceName(examCode, placeCode);
                BigDecimal dlCount = new BigDecimal(dbObject.get("countDl").toString());
                BigDecimal ycCount = new BigDecimal(dbObject.get("countGz").toString());
                //计算当前考点应考考次
//                Query sub = new Query();
//                sub.addCriteria(Criteria.where("examCode").is(examCode));
//                sub.addCriteria(Criteria.where("placeCode").is(placeCode));
//                long ykCount = mongoTemplate.count(sub, ExamStudent.class);
                QueryWrapper<ExamStudent> examStudentQueryWrapper = new QueryWrapper<>();
                examStudentQueryWrapper.lambda().eq(ExamStudent::getExamCode, examCode)
                        .eq(ExamStudent::getPlaceCode, placeCode);
                long ykCount = examStudentService.count(examStudentQueryWrapper);
                BigDecimal ykNumber = new BigDecimal(ykCount);
                //计算占比
                Map<String, Object> data = new HashMap<>();
                BigDecimal percentYk = ykNumber.compareTo(BigDecimal.ZERO) == 0 ? new BigDecimal("0") : ykNumber.divide(ykNumber, 4, BigDecimal.ROUND_HALF_UP);
                data.put("percentYk", percentYk);
                BigDecimal percentDl = ykNumber.compareTo(BigDecimal.ZERO) == 0 ? new BigDecimal("0") : dlCount.divide(ykNumber, 4, BigDecimal.ROUND_HALF_UP);
                data.put("percentDl", percentDl);
                BigDecimal percentYc = ykNumber.compareTo(BigDecimal.ZERO) == 0 ? new BigDecimal("0") : ycCount.divide(ykNumber, 4, BigDecimal.ROUND_HALF_UP);
                data.put("percentYc", percentYc);
                data.put("placeName", placeName);
                result.add(data);
            }
        }
        return result;
    }

    @Override
    public void remove(String examCode, String placeCode, String roomCode) {
//        DBObject queryDel = new BasicDBObject("exam_code", examCode);
//        queryDel.put("place_code", placeCode);
//        queryDel.put("room_code", roomCode);
//        mongoTemplate.getCollection("jk_exam_room_monitor_info").remove(queryDel);
        QueryWrapper<ExamRoomMonitorInfo> examRoomMonitorInfoQueryWrapper = new QueryWrapper<>();
        examRoomMonitorInfoQueryWrapper.lambda().eq(ExamRoomMonitorInfo::getPlaceCode, placeCode)
                .eq(ExamRoomMonitorInfo::getRoomCode, roomCode);
        examRoomMonitorInfoService.remove(examRoomMonitorInfoQueryWrapper);
    }
}
