/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.smartapplication.smartmonitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springblade.smartapplication.smartmonitor.entity.SmartVideoMonitorStat;
import org.springblade.smartapplication.smartmonitor.mapper.SmartVideoMonitorStatMapper;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorStatService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.smartapplication.smartmonitor.vo.PersonInOutStatVO;
import org.springblade.smartapplication.smartmonitor.vo.SmartVideoMonitorStatVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-09-02
 */
@Service
public class SmartVideoMonitorStatServiceImpl extends BaseServiceImpl<SmartVideoMonitorStatMapper, SmartVideoMonitorStat> implements ISmartVideoMonitorStatService {

	@Override
	public IPage<SmartVideoMonitorStatVO> selectSmartVideoMonitorStatPage(IPage<SmartVideoMonitorStatVO> page, SmartVideoMonitorStatVO smartVideoMonitorStat) {
		return page.setRecords(baseMapper.selectSmartVideoMonitorStatPage(page, smartVideoMonitorStat));
	}

	@Override
	public PersonInOutStatVO getPersonInOutStat(String serialNum, String beginDate, String endDate) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		LambdaQueryWrapper<SmartVideoMonitorStat> lambda = new LambdaQueryWrapper<>();
		lambda.eq(SmartVideoMonitorStat::getSerialNum, serialNum);
		lambda.ge(SmartVideoMonitorStat::getStatDate, beginDate);
		lambda.le(SmartVideoMonitorStat::getStatDate, endDate);
		List<SmartVideoMonitorStat> list = baseMapper.selectList(lambda);
		List<String> dateList = list.stream().map(stat -> {
			Date statDate = stat.getStatDate();
			return sdf.format(statDate);
		}).collect(Collectors.toList());
		List<Integer> enterCtnList = list.stream().map(SmartVideoMonitorStat::getEnterCnt).collect(Collectors.toList());
		List<Integer> leaveCtnList = list.stream().map(SmartVideoMonitorStat::getLeaveCnt).collect(Collectors.toList());
		PersonInOutStatVO vo = new PersonInOutStatVO();
		vo.setDateList(dateList);
		vo.setEnterCtnList(enterCtnList);
		vo.setLeaveCtnList(leaveCtnList);
		return vo;
	}

}
