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.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.MonitorQuestion;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IMonitorQuestionService;
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.List;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: 视频抓拍记录
 * @author： pds
 * @date： 2023-02-28
 * @version： V1.0
 */
@RestController
@RequestMapping("/ent/monitorQuestion")
@Slf4j
@Api(tags = "抓拍记录")
public class EntMonitorQuestionController {
    @Autowired
    private IMonitorQuestionService monitorQuestionService;

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "视频抓拍管理", operType = "分页查询", operDesc = "分页列表查询视频抓拍记录信息")
    @ApiOperation(value = " 分页列表查询视频抓拍记录信息", notes = "分页列表查询视频抓拍记录信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "engineeringSn", value = "工程sn", 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<MonitorQuestion>> queryPageList(@RequestBody Map<String, Object> map) {
        QueryWrapper<MonitorQuestion> wrapper = Wrappers.<MonitorQuestion>query();
        String engineeringSn = MapUtils.getString(map, "engineeringSn");
        Page<MonitorQuestion> page = PageUtil.getPage(map);
        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(engineeringSn)) {
            wrapper.eq("m.engineering_sn", engineeringSn);
        } else {
            List<String> engineeringSns = engineeringService.getSnListByEnterprise(user.getSn());
            if (engineeringSns.size() > 0) {
                wrapper.in("m.engineering_sn", engineeringSns);
            }
        }
        return Result.success(monitorQuestionService.getPageList(page, wrapper));
    }

    /**
     * 问题整改
     *
     * @param monitorQuestion
     * @return
     */
    @OperLog(operModul = "视频抓拍管理", operType = "修改", operDesc = "问题整改")
    @ApiOperation(value = "问题整改", notes = "问题整改", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<MonitorQuestion> edit(@RequestBody MonitorQuestion monitorQuestion) {
        Result<MonitorQuestion> result = new Result<MonitorQuestion>();
        MonitorQuestion monitorQuestionEntity = monitorQuestionService.getById(monitorQuestion.getId());
        if (monitorQuestionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = monitorQuestionService.updateById(monitorQuestion);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     * @return
     */
    @OperLog(operModul = "视频抓拍管理", operType = "删除", operDesc = "删除视频抓拍记录信息")
    @ApiOperation(value = "删除视频抓拍记录信息", notes = "删除视频抓拍记录信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "视频抓拍记录ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/delete")
    public Result<MonitorQuestion> delete(@RequestBody MonitorQuestion monitorQuestion) {
        Result<MonitorQuestion> result = new Result<MonitorQuestion>();
        MonitorQuestion monitorQuestionEntity = monitorQuestionService.getById(monitorQuestion.getId());
        if (monitorQuestionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = monitorQuestionService.removeById(monitorQuestion.getId());
            if (ok) {
                result.success("删除成功!");
            }
        }

        return result;
    }

    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "视频抓拍管理", operType = "查询", operDesc = "通过id查询视频抓拍记录信息")
    @ApiOperation(value = "通过id查询视频抓拍记录信息", notes = "通过id查询视频抓拍记录信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "视频抓拍记录ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryById")
    public Result<MonitorQuestion> queryById(@RequestBody MonitorQuestion monitorQuestion) {
        Result<MonitorQuestion> result = new Result<MonitorQuestion>();
        MonitorQuestion monitorQuestionEntity = monitorQuestionService.getById(monitorQuestion.getId());
        if (monitorQuestionEntity == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(monitorQuestion);
            result.setSuccess(true);
        }
        return result;
    }
}
