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.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.AiMonitorQuestion;
import com.zhgd.xmgl.modules.basicdata.service.IAiMonitorQuestionService;
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.lang3.StringUtils;
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： pds
 * @date： 2023-02-28
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/aiMonitorQuestion")
@Slf4j
@Api(tags = "AI预警整改记录")
public class AiMonitorQuestionController {
    @Autowired
    private IAiMonitorQuestionService aiMonitorQuestionService;

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "AI预警整改管理", operType = "分页查询", operDesc = "分页列表查询AI预警整改记录信息")
    @ApiOperation(value = " 分页列表查询AI预警整改记录信息", notes = "分页列表查询AI预警整改记录信息", httpMethod = "POST")
    @ApiImplicitParams({
            @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<AiMonitorQuestion>> queryPageList(@RequestBody Map<String, Object> map) {
        QueryWrapper<AiMonitorQuestion> wrapper = Wrappers.<AiMonitorQuestion>query();
        Page<AiMonitorQuestion> page = PageUtil.getPage(map);
        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
            wrapper.eq("q.engineering_sn", user.getSn());
        } else {
            wrapper.eq("q.project_sn", user.getSn()).eq("q.engineering_sn", "");
        }
        return Result.success(aiMonitorQuestionService.getPageList(page, wrapper));
    }

    /**
     * 问题整改
     *
     * @param aiMonitorQuestion
     * @return
     */
    @OperLog(operModul = "AI预警整改管理", operType = "修改", operDesc = "问题整改")
    @ApiOperation(value = "问题整改", notes = "问题整改", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<AiMonitorQuestion> edit(@RequestBody AiMonitorQuestion aiMonitorQuestion) {
        Result<AiMonitorQuestion> result = new Result<AiMonitorQuestion>();
        AiMonitorQuestion aiMonitorQuestionEntity = aiMonitorQuestionService.getById(aiMonitorQuestion.getId());
        if (aiMonitorQuestionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = aiMonitorQuestionService.updateById(aiMonitorQuestion);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     * @return
     */
    @OperLog(operModul = "AI预警整改管理", operType = "删除", operDesc = "删除AI预警整改记录信息")
    @ApiOperation(value = "删除AI预警整改记录信息", notes = "删除AI预警整改记录信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "AI预警整改记录ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/delete")
    public Result<AiMonitorQuestion> delete(@RequestBody AiMonitorQuestion aiMonitorQuestion) {
        Result<AiMonitorQuestion> result = new Result<AiMonitorQuestion>();
        AiMonitorQuestion aiMonitorQuestionEntity = aiMonitorQuestionService.getById(aiMonitorQuestion.getId());
        if (aiMonitorQuestionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = aiMonitorQuestionService.removeById(aiMonitorQuestion.getId());
            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 = "id", value = "AI预警整改记录ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryById")
    public Result<AiMonitorQuestion> queryById(@RequestBody AiMonitorQuestion aiMonitorQuestion) {
        Result<AiMonitorQuestion> result = new Result<AiMonitorQuestion>();
        AiMonitorQuestion aiMonitorQuestionEntity = aiMonitorQuestionService.getById(aiMonitorQuestion.getId());
        if (aiMonitorQuestionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(aiMonitorQuestionEntity);
            result.setSuccess(true);
        }
        return result;
    }
}
