/*
 *      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.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.util.DateUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.smartapplication.smartmonitor.entity.SmartVideoMonitor;
import org.springblade.smartapplication.smartmonitor.entity.SmartVideoMonitorStat;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorService;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorStatService;
import org.springblade.smartapplication.smartmonitor.vo.PersonInOutStatVO;
import org.springblade.smartapplication.smartmonitor.vo.SmartVideoMonitorStatVO;
import org.springblade.smartapplication.smartmonitor.wrapper.SmartVideoMonitorStatWrapper;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;
import java.util.Map;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-09-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("smartmonitorstat/smartvideomonitorstat")
@Api(value = "", tags = "接口")
public class SmartVideoMonitorStatController extends BladeController {

	private ISmartVideoMonitorStatService smartVideoMonitorStatService;
	private ISmartVideoMonitorService smartVideoMonitorService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入smartVideoMonitorStat")
	public R<SmartVideoMonitorStatVO> detail(SmartVideoMonitorStat smartVideoMonitorStat) {
		SmartVideoMonitorStat detail = smartVideoMonitorStatService.getOne(Condition.getQueryWrapper(smartVideoMonitorStat));
		return R.data(SmartVideoMonitorStatWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入smartVideoMonitorStat")
	public R<IPage<SmartVideoMonitorStatVO>> list(SmartVideoMonitorStat smartVideoMonitorStat, Query query) {
		IPage<SmartVideoMonitorStat> pages = smartVideoMonitorStatService.page(Condition.getPage(query), Condition.getQueryWrapper(smartVideoMonitorStat));
		return R.data(SmartVideoMonitorStatWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入smartVideoMonitorStat")
	public R<IPage<SmartVideoMonitorStatVO>> page(SmartVideoMonitorStatVO smartVideoMonitorStat, Query query) {
		IPage<SmartVideoMonitorStatVO> pages = smartVideoMonitorStatService.selectSmartVideoMonitorStatPage(Condition.getPage(query), smartVideoMonitorStat);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入smartVideoMonitorStat")
	public R save(@Valid @RequestBody SmartVideoMonitorStat smartVideoMonitorStat) {
		return R.status(smartVideoMonitorStatService.save(smartVideoMonitorStat));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入smartVideoMonitorStat")
	public R update(@Valid @RequestBody SmartVideoMonitorStat smartVideoMonitorStat) {
		return R.status(smartVideoMonitorStatService.updateById(smartVideoMonitorStat));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入smartVideoMonitorStat")
	public R submit(@Valid @RequestBody SmartVideoMonitorStat smartVideoMonitorStat) {
		return R.status(smartVideoMonitorStatService.saveOrUpdate(smartVideoMonitorStat));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(smartVideoMonitorStatService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/personInOutStat")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "人员出入统计")
	public R<PersonInOutStatVO> personInOutStat(@RequestParam Map<String, Object> params) {
		String serialNum = (String)params.get("serialNum");
		Assert.hasText(serialNum, "设备序列号必传");
		SmartVideoMonitor monitor = smartVideoMonitorService.getBySerialNum(serialNum);
		Assert.notNull(monitor, "设备不存在");
		String beginDate = (String)params.get("beginDate");
		String endDate = (String)params.get("endDate");
		if(StringUtil.isNotBlank(beginDate) && StringUtil.isNotBlank(endDate)){
			PersonInOutStatVO vo = smartVideoMonitorStatService.getPersonInOutStat(serialNum, beginDate, endDate);
			return R.data(vo);
		}else{
			//默认获取本周的日期段。
			String timeInterval = DateUtil.getTimeInterval(new Date());
			PersonInOutStatVO vo = smartVideoMonitorStatService.getPersonInOutStat(serialNum, timeInterval.split(",")[0], timeInterval.split(",")[1]);
			return R.data(vo);
		}
	}

}
