package com.zhgd.xmgl.modules.basicdata.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.AiMonitorAlarm;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorAlarmService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * @Title: Controller
 * @Description: AI报警记录
 * @author： pengj
 * @date： 2023-03-04
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/aiMonitorAlarm")
@Slf4j
@Api(tags = "AI报警记录管理")
public class AiMonitorAlarmController {
    @Autowired
    private IAiMonitorAlarmService aiMonitorAlarmService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI报警记录管理", operType = "列表查询", operDesc = "分页列表查询AI报警记录信息")
    @ApiOperation(value = " 分页列表查询AI报警记录信息", notes = "分页列表查询AI报警记录信息", httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "deviceName", value = "监控名称", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
	})
    @PostMapping(value = "/page")
    public Result<Page<AiMonitorAlarm>> queryPageList(@RequestBody Map<String, Object> map) {
		String deviceName = MapUtils.getString(map, "deviceName");
		String startTime = MapUtils.getString(map, "startTime");
		String endTime = MapUtils.getString(map, "endTime");
		QueryWrapper<AiMonitorAlarm> wrapper = Wrappers.<AiMonitorAlarm>query();
		Page page = PageUtil.getPage(map);
		SecurityUser user = SecurityUtil.getUser();
		if (StringUtils.isNotBlank(user.getEngineeringSn())) {
			wrapper.eq("a.engineering_sn", user.getEngineeringSn());
		} else {
			wrapper.eq("a.project_sn", user.getSn())
					.eq("a.engineering_sn", "");
		}
		if (StringUtils.isNotBlank(deviceName)) {
			wrapper.like("d.name", deviceName);
		}
		if (StringUtils.isNotBlank(startTime)) {
			wrapper.ge("a.create_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			wrapper.le("a.create_time", endTime);
		}
        Page<AiMonitorAlarm> list = aiMonitorAlarmService.pageList(page, wrapper);
        return Result.success(list);
    }
}
