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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.AiMonitorDev;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorDevService;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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


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

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI监控管理", operType = "分页查询", operDesc = "分页列表查询AI监控管理表信息")
    @ApiOperation(value = " 分页列表查询AI监控管理表信息", notes = "分页列表查询AI监控管理表信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "engineeringSn", value = "工程名称", paramType = "query", dataType = "String"),
            @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) {
        String engineeringSn = MapUtils.getString(map, "engineeringSn");
        String name = MapUtils.getString(map, "name");
        QueryWrapper<AiMonitorDev> wrapper = Wrappers.<AiMonitorDev>query();
        Page<AiMonitorDev> page = PageUtil.getPage(map);
        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(engineeringSn)) {
            wrapper.eq("d.engineering_sn", engineeringSn);
        } else {
            List<String> engineeringSns = engineeringService.getSnListByEnterprise(user.getSn());
            if (engineeringSns.size() > 0) {
                wrapper.in("d.engineering_sn", engineeringSns);
            }
        }
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("d.name", name);
        }
        return Result.success(aiMonitorDevService.pageList(page, wrapper));
    }

    /**
     * 添加
     *
     * @param aiMonitorDev
     * @return
     */
    @OperLog(operModul = "项目AI监控管理", operType = "新增", operDesc = "添加AI监控管理表信息")
    @ApiOperation(value = " 添加AI监控管理表信息", notes = "添加AI监控管理表信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<AiMonitorDev> add(@RequestBody AiMonitorDev aiMonitorDev) {
        Result<AiMonitorDev> result = new Result<AiMonitorDev>();
        try {
            aiMonitorDev.setCreateTime(new Date());
            aiMonitorDevService.save(aiMonitorDev);
            result.success("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param aiMonitorDev
     * @return
     */
    @OperLog(operModul = "项目AI监控管理", operType = "修改", operDesc = "编辑AI监控管理表信息")
    @ApiOperation(value = "编辑AI监控管理表信息", notes = "编辑AI监控管理表信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<AiMonitorDev> edit(@RequestBody AiMonitorDev aiMonitorDev) {
        Result<AiMonitorDev> result = new Result<AiMonitorDev>();
        AiMonitorDev aiMonitorDevEntity = aiMonitorDevService.getById(aiMonitorDev.getAiMonitorId());
        if (aiMonitorDevEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = aiMonitorDevService.updateById(aiMonitorDev);
            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 = "Integer")
    @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 aiMonitorDev) {
        Result<AiMonitorDev> result = new Result<AiMonitorDev>();
        AiMonitorDev aiMonitorDevEntity = aiMonitorDevService.getById(aiMonitorDev.getAiMonitorId());
        if (aiMonitorDevEntity == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(aiMonitorDevEntity);
            result.setSuccess(true);
        }
        return result;
    }
}
