package com.usky.park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.park.domain.DmpDevice;
import com.usky.park.domain.EventEg;
import com.usky.park.mapper.EventEgMapper;
import com.usky.park.service.DmpDeviceService;
import com.usky.park.service.EventEgService;
import com.usky.common.mybatis.core.AbstractCrudService;
import java.util.Comparator;
import com.usky.park.service.vo.EventEgStatisticVO;
import org.bouncycastle.util.StringList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 门禁历史通行记录表 服务实现类
 * </p>
 *
 * @author zyj
 * @since 2023-03-31
 */
@Service
public class EventEgServiceImpl extends AbstractCrudService<EventEgMapper, EventEg> implements EventEgService {
    @Autowired
    private DmpDeviceService deviceService;

    @Override
    public CommonPage<EventEg> eventEgList(String deviceName,String installAddress,String cardNo,String orgName,Integer eventType,Integer pageNum,Integer pageSize){
        IPage<EventEg> page = new Page<>(pageNum,pageSize);

        List<Integer> idList = new ArrayList<>();
        QueryWrapper<EventEg> query = Wrappers.query();
        query.select("Max(id) as id")
                .groupBy("device_id");
        List<Map<String,Object>> maps = this.listMaps(query);
        if(maps.size()>0){
            for(int i=0;i<maps.size();i++){
                idList.add(Integer.parseInt(maps.get(i).get("id").toString()));
            }
        }

        LambdaQueryWrapper<EventEg> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(idList),EventEg::getId,idList)
                .like(StringUtils.isNotBlank(deviceName),EventEg::getDeviceName,deviceName)
                .like(StringUtils.isNotBlank(installAddress),EventEg::getInstallAddress,installAddress)
                .like(StringUtils.isNotBlank(cardNo),EventEg::getCardNo,cardNo)
                .like(StringUtils.isNotBlank(orgName),EventEg::getOrgName,orgName)
                .eq(eventType != null,EventEg::getEventType,eventType)
                .orderByDesc(EventEg::getEventTime);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),pageNum,pageSize);

    }

    @Override
    public CommonPage<EventEg> eventEgOne(String deviceId, String personName, String startDate, String endDate,Integer pageNum,Integer pageSize){
        IPage<EventEg> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<EventEg> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(EventEg::getDeviceId,deviceId)
                .like(StringUtils.isNotBlank(personName),EventEg::getPersonName,personName)
                .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate),EventEg::getEventTime,startDate,endDate)
                .orderByDesc(EventEg::getId);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),pageNum,pageSize);
    }

    @Override
    public List<EventEgStatisticVO> eventEgStatistic(List<String> enterDeviceId, List<String> outDeviceId, String startDate, String endDate){
        List<EventEgStatisticVO> list = new ArrayList<>();
        EventEgStatisticVO eventEgStatisticVO = new EventEgStatisticVO();
        eventEgStatisticVO.setEnterNum(numStatistic(enterDeviceId,startDate,endDate));
        eventEgStatisticVO.setOutNum(numStatistic(outDeviceId,startDate,endDate));
        list.add(eventEgStatisticVO);

        return list;
    }

    public Integer numStatistic(List<String> deviceId,String startDate,String endDate){
        Integer num = 0;
        LambdaQueryWrapper<EventEg> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(EventEg::getDeviceId,deviceId)
                .between(EventEg::getEventTime,startDate,endDate);
        num = this.count(queryWrapper);

        return num;
    }

}
