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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.EnvironmentAlarm;
import com.zhgd.xmgl.modules.basicdata.service.IEnvironmentAlarmService;
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: 扬尘报警台账表
 * @author： pengj
 * @date：   2023-03-01
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/environmentAlarm")
@Slf4j
@Api(tags = "扬尘报警台账管理")
public class EnvironmentAlarmController {
    @Autowired
    private IEnvironmentAlarmService environmentAlarmService;

    /**
     * 分页列表查询
     * @return
     */
    @OperLog(operModul = "扬尘报警管理", operType = "分页查询", operDesc = "分页列表查询扬尘报警台账表信息")
    @ApiOperation(value = " 分页列表查询扬尘报警台账表信息", notes = "分页列表查询扬尘报警台账表信息", httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "设备名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", 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<IPage<EnvironmentAlarm>> queryPageList(@RequestBody Map<String, Object> map) {
        String name = MapUtils.getString(map, "name");
        String type = MapUtils.getString(map, "type");
        String startTime = MapUtils.getString(map, "startTime");
        String endTime = MapUtils.getString(map, "endTime");
        QueryWrapper<EnvironmentAlarm> wrapper = Wrappers.<EnvironmentAlarm>query();
        SecurityUser user = SecurityUtil.getUser();
        Page<EnvironmentAlarm> page = PageUtil.getPage(map);
        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
            wrapper.eq("a.engineering_sn", user.getSn());
        } else {
            wrapper.eq("a.project_sn", user.getSn());
        }
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("d.name", name);
        }
        if (StringUtils.isNotBlank(type)) {
            wrapper.eq("a.type", type);
        }
        if (StringUtils.isNotBlank(startTime)) {
            wrapper.ge("a.create_time", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            wrapper.le("a.create_time", endTime);
        }
        IPage<EnvironmentAlarm> pageList = environmentAlarmService.pageList(page, wrapper);
        return Result.success(pageList);
    }

    /**
     *  编辑
     * @param environmentAlarm
     * @return
     */
    @OperLog(operModul = "扬尘报警管理", operType = "修改", operDesc = "编辑扬尘报警台账表信息")
    @ApiOperation(value = "编辑扬尘报警台账表信息", notes = "编辑扬尘报警台账表信息" , httpMethod="POST")
    @PostMapping(value = "/edit")
    public Result<EnvironmentAlarm> edit(@RequestBody EnvironmentAlarm environmentAlarm) {
        Result<EnvironmentAlarm> result = new Result<EnvironmentAlarm>();
        EnvironmentAlarm environmentAlarmEntity = environmentAlarmService.getById(environmentAlarm.getId());
        if(environmentAlarmEntity==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = environmentAlarmService.updateById(environmentAlarm);
            if(ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }
}
