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.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.AiMonitorDev;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorDevService;
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.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


/**
 * @Title: Controller
 * @Description: AI监控管理表
 * @author： pds
 * @date： 2023-02-27
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/aiMonitorDev")
@Slf4j
@Api(tags = "AI监控管理")
public class AiMonitorDevController {
    @Autowired
    private IAiMonitorDevService aiMonitorDevService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI监控管理", operType = "分页查询", operDesc = "分页列表查询AI监控管理表信息")
    @ApiOperation(value = " 分页列表查询AI监控管理表信息", notes = "分页列表查询AI监控管理表信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "AI监控名称", 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<AiMonitorDev>> queryPageList(@RequestBody Map<String, Object> map) {
        QueryWrapper<AiMonitorDev> queryWrapper = QueryGenerator.initPageQueryWrapper(AiMonitorDev.class, map);
        Page<AiMonitorDev> page = PageUtil.getPage(map);
        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
            queryWrapper.eq("engineering_sn", user.getSn());
        } else {
            queryWrapper.eq("project_sn", user.getSn()).eq("engineering_sn", "");
        }
        return Result.success(aiMonitorDevService.page(page, queryWrapper));
    }

    /**
     * 添加
     *
     * @param aiMonitorDev
     * @return
     */
    @OperLog(operModul = "项目AI监控管理", operType = "新增", operDesc = "添加AI监控管理表信息")
    @ApiOperation(value = " 添加AI监控管理表信息", notes = "添加AI监控管理表信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody AiMonitorDev aiMonitorDev) {
        SecurityUser user = SecurityUtil.getUser();
        aiMonitorDev.setProjectSn(user.getSn());
        aiMonitorDev.setEngineeringSn(user.getEngineeringSn());
        aiMonitorDev.setCreateTime(new Date());
        aiMonitorDevService.save(aiMonitorDev);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     * @param aiMonitorDevEntity
     * @return
     */
    @OperLog(operModul = "项目AI监控管理", operType = "修改", operDesc = "编辑AI监控管理表信息")
    @ApiOperation(value = "编辑AI监控管理表信息", notes = "编辑AI监控管理表信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<AiMonitorDev> edit(@RequestBody AiMonitorDev aiMonitorDevEntity) {
        Result<AiMonitorDev> result = new Result<AiMonitorDev>();
        AiMonitorDev aiMonitorDev = aiMonitorDevService.getById(aiMonitorDevEntity.getAiMonitorId());
        if (aiMonitorDev == null) {
            result.error500("未找到对应实体");
        } else {
            SecurityUser user = SecurityUtil.getUser();
            aiMonitorDevEntity.setProjectSn(user.getSn());
            aiMonitorDevEntity.setEngineeringSn(user.getEngineeringSn());
            boolean ok = aiMonitorDevService.updateById(aiMonitorDevEntity);
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 通过id删除
     *
     * @param aiMonitorDev
     * @return
     */
    @OperLog(operModul = "项目AI监控管理", operType = "删除", operDesc = "删除AI监控管理表信息")
    @ApiOperation(value = "删除AI监控管理表信息", notes = "删除AI监控管理表信息", httpMethod = "POST")
    @ApiImplicitParam(name = "aiMonitorId", value = "AI监控管理表ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/delete")
    public Result<AiMonitorDev> delete(@RequestBody AiMonitorDev aiMonitorDev) {
        Result<AiMonitorDev> result = new Result<AiMonitorDev>();
        AiMonitorDev aiMonitorDevEntity = aiMonitorDevService.getById(aiMonitorDev.getAiMonitorId());
        if (aiMonitorDevEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = aiMonitorDevService.removeById(aiMonitorDev.getAiMonitorId());
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "项目AI监控管理", operType = "查询", operDesc = "通过id查询AI监控管理表信息")
    @ApiOperation(value = "通过id查询AI监控管理表信息", notes = "通过id查询AI监控管理表信息", httpMethod = "POST")
    @ApiImplicitParam(name = "aiMonitorId", value = "AI监控管理表ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<AiMonitorDev> queryById(@RequestBody AiMonitorDev aiMonitorDevEntity) {
        Result<AiMonitorDev> result = new Result<AiMonitorDev>();
        AiMonitorDev aiMonitorDev = aiMonitorDevService.getById(aiMonitorDevEntity.getAiMonitorId());
        if (aiMonitorDev == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(aiMonitorDev);
            result.setSuccess(true);
        }
        return result;
    }
}
