package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dao.JkRoomMonitorClientDataDao;
import com.qmth.jkserver.dao.JkRoomMonitorClientLogDao;
import com.qmth.jkserver.dto.RoomMonitorClientData;
import com.qmth.jkserver.dto.RoomMonitorClientLog;
import com.qmth.jkserver.model.ExamPlan;
import com.qmth.jkserver.params.RoomMonitorClientParam;
import com.qmth.jkserver.service.JkExamPlanService;
import com.qmth.jkserver.service.JkRoomMonitorClientDataService;
import com.qmth.jkserver.service.JkRoomMonitorClientLogService;
import com.qmth.jkserver.service.RoomMonitorClientService;
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.util.Objects;

@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class RoomMonitorClientServiceImpl implements RoomMonitorClientService {

//    @Autowired
//    private MongoTemplate mongoTemplate;

    @Resource
    @Lazy
    private JkExamPlanService examPlanService;

    @Resource
    JkRoomMonitorClientLogDao jkRoomMonitorClientLogDao;

    @Resource
    JkRoomMonitorClientLogService jkRoomMonitorClientLogService;

    @Resource
    JkRoomMonitorClientDataDao jkRoomMonitorClientDataDao;

    @Resource
    JkRoomMonitorClientDataService jkRoomMonitorClientDataService;

    @Override
    public Page<RoomMonitorClientLog> pageQueryLog(RoomMonitorClientParam param, int pageNo, int pageSize) {
//        Query query = new Query();
//        QueryWrapper<RoomMonitorClientLog> roomMonitorClientLogQueryWrapper = new QueryWrapper<>();
//        if (StringUtils.isNotEmpty(param.getExamCode())) {
////            query.addCriteria(Criteria.where("examCode").is(param.getExamCode()));
//            roomMonitorClientLogQueryWrapper.lambda().eq(RoomMonitorClientLog::getRoomCode, param.getExamCode());
//        }
//        if (StringUtils.isNotEmpty(param.getPlaceCode())) {
////            query.addCriteria(Criteria.where("placeCode").is(param.getPlaceCode()));
//            roomMonitorClientLogQueryWrapper.lambda().eq(RoomMonitorClientLog::getPlaceCode, param.getPlaceCode());
//        }
//        if (StringUtils.isNotEmpty(param.getRoomCode())) {
////            query.addCriteria(Criteria.where("roomCode").is(param.getRoomCode()));
//            roomMonitorClientLogQueryWrapper.lambda().eq(RoomMonitorClientLog::getRoomCode, param.getRoomCode());
//        }
//        if (StringUtils.isNotEmpty(param.getPlaceName())) {
////            query.addCriteria(Criteria.where("placeName").regex(param.getPlaceName()));
//            roomMonitorClientLogQueryWrapper.lambda().like(RoomMonitorClientLog::getPlaceName, param.getPlaceName());
//        }
//        if (Objects.nonNull(param.isUpload())) {
////            query.addCriteria(Criteria.where("upload").is(param.isUpload()));
//            roomMonitorClientLogQueryWrapper.lambda().eq(RoomMonitorClientLog::getUpload, param.isUpload());
//        }
////        long count = this.mongoTemplate.count(query, RoomMonitorClientLog.class);
//        long count = jkRoomMonitorClientLogService.count(roomMonitorClientLogQueryWrapper);
//        Order order1 = new Order(Direction.ASC, "placeCode");
//        Order order2 = new Order(Direction.ASC, "roomCode");
//        Order order3 = new Order(Direction.DESC, "time");
//        List<Order> orders = new ArrayList<>();
//        orders.add(order1);
//        orders.add(order2);
//        orders.add(order3);
//        query.with(new Sort(orders));
//        query.skip((pageNo - 1) * pageSize);
//        query.limit(pageSize);
//        List<RoomMonitorClientLog> list = mongoTemplate.find(query, RoomMonitorClientLog.class);
        IPage<RoomMonitorClientLog> roomMonitorClientLogIPage = jkRoomMonitorClientLogDao.findByExamCodeByPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNo, pageSize),
                param.getExamCode(), param.getPlaceCode(), param.getRoomCode(), param.getPlaceName(), param.isUpload());
        ExamPlan examPlan = examPlanService.findByExamCode(param.getExamCode());
        if (examPlan == null) {
            throw JkServerException.withFieldValidateError("考试识别码错误:" + param.getExamCode());
        }
//        list.stream().forEach(m -> m.setExamPlan(examPlan));
        roomMonitorClientLogIPage.getRecords().stream().forEach(m -> m.setExamPlanId(examPlan.getId()));
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        return new PageImpl<>(roomMonitorClientLogIPage.getRecords(), pageRequest, roomMonitorClientLogIPage.getTotal());
    }

    @Override
    public Page<RoomMonitorClientData> pageQueryData(RoomMonitorClientParam param, int pageNo, int pageSize) {
//        Query query = new Query();
//        QueryWrapper<RoomMonitorClientData> roomMonitorClientDataQueryWrapper = new QueryWrapper<>();
//        if (StringUtils.isNotEmpty(param.getExamCode())) {
////            query.addCriteria(Criteria.where("examCode").is(param.getExamCode()));
//            roomMonitorClientDataQueryWrapper.lambda().eq(RoomMonitorClientData::getExamCode, param.getExamCode());
//        }
//        if (StringUtils.isNotEmpty(param.getPlaceCode())) {
////            query.addCriteria(Criteria.where("placeCode").is(param.getPlaceCode()));
//            roomMonitorClientDataQueryWrapper.lambda().eq(RoomMonitorClientData::getPlaceCode, param.getPlaceCode());
//        }
//        if (StringUtils.isNotEmpty(param.getRoomCode())) {
////            query.addCriteria(Criteria.where("roomCode").is(param.getRoomCode()));
//            roomMonitorClientDataQueryWrapper.lambda().eq(RoomMonitorClientData::getRoomCode, param.getRoomCode());
//        }
//        if (StringUtils.isNotEmpty(param.getPlaceName())) {
////            query.addCriteria(Criteria.where("placeName").regex(param.getPlaceName()));
//            roomMonitorClientDataQueryWrapper.lambda().like(RoomMonitorClientData::getPlaceName, param.getPlaceName());
//        }
//        if (Objects.nonNull(param.isUpload())) {
////            query.addCriteria(Criteria.where("upload").is(param.isUpload()));
//            roomMonitorClientDataQueryWrapper.lambda().eq(RoomMonitorClientData::getUpload, param.isUpload());
//        }
////        long count = this.mongoTemplate.count(query, RoomMonitorClientData.class);
//        long count = jkRoomMonitorClientDataService.count(roomMonitorClientDataQueryWrapper);
//        Order order1 = new Order(Direction.ASC, "placeCode");
//        Order order2 = new Order(Direction.ASC, "roomCode");
//        Order order3 = new Order(Direction.DESC, "time");
//        List<Order> orders = new ArrayList<>();
//        orders.add(order1);
//        orders.add(order2);
//        orders.add(order3);
//        query.with(new Sort(orders));
//        query.skip((pageNo - 1) * pageSize);
//        query.limit(pageSize);
//        List<RoomMonitorClientData> list = mongoTemplate.find(query, RoomMonitorClientData.class);
        IPage<RoomMonitorClientData> roomMonitorClientDataIPage = jkRoomMonitorClientDataDao.findByExamCodeByPage(new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(pageNo, pageSize),
                param.getExamCode(), param.getPlaceCode(), param.getRoomCode(), param.getPlaceName(), param.isUpload());
        ExamPlan examPlan = examPlanService.findByExamCode(param.getExamCode());
        if (examPlan == null) {
            throw JkServerException.withFieldValidateError("考试识别码错误:" + param.getExamCode());
        }
//        list.stream().forEach(m -> m.setExamPlan(examPlan));
        roomMonitorClientDataIPage.getRecords().stream().forEach(m -> m.setExamPlanId(examPlan.getId()));
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        return new PageImpl<>(roomMonitorClientDataIPage.getRecords(), pageRequest, roomMonitorClientDataIPage.getTotal());
    }
}
