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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.dto.MonitorTreeDto;
import com.zhgd.xmgl.modules.basicdata.entity.Engineering;
import com.zhgd.xmgl.modules.basicdata.entity.MonitorDev;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import com.zhgd.xmgl.modules.basicdata.service.IMonitorDevService;
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: 视频管理表
 * @author： pds
 * @date： 2023-02-27
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/monitorDev")
@Slf4j
@Api(tags = "视频管理")
public class MonitorDevController {
    @Autowired
    private IMonitorDevService monitorDevService;

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "项目视频管理", operType = "分页查询", operDesc = "分页列表查询视频管理表信息")
    @ApiOperation(value = " 分页列表查询视频管理表信息", notes = "分页列表查询视频管理表信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", 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<MonitorDev>> queryPageList(@RequestBody Map<String, Object> map) {
        Result<IPage<MonitorDev>> result = new Result<IPage<MonitorDev>>();
        QueryWrapper<MonitorDev> queryWrapper = QueryGenerator.initPageQueryWrapper(MonitorDev.class, map);
        Page<MonitorDev> 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", "");
        }
        IPage<MonitorDev> pageList = monitorDevService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param monitorDev
     * @return
     */
    @OperLog(operModul = "项目视频管理", operType = "新增", operDesc = "添加视频管理表信息")
    @ApiOperation(value = " 添加视频管理表信息", notes = "添加视频管理表信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<MonitorDev> add(@RequestBody MonitorDev monitorDev) {
        Result<MonitorDev> result = new Result<MonitorDev>();
        try {
            SecurityUser user = SecurityUtil.getUser();
            monitorDev.setProjectSn(user.getSn());
            monitorDev.setEngineeringSn(user.getEngineeringSn());
            monitorDev.setCreateTime(new Date());
            monitorDevService.save(monitorDev);
            result.success("添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            result.error500("操作失败");
        }
        return result;
    }

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

    /**
     * 通过id删除
     *
     * @param monitorDevEntity
     * @return
     */
    @OperLog(operModul = "项目视频管理", operType = "删除", operDesc = "删除视频管理表信息")
    @ApiOperation(value = "删除视频管理表信息", notes = "删除视频管理表信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "视频管理表ID", paramType = "query", required = true, dataType = "Integer")
    @DeleteMapping(value = "/delete")
    public Result<MonitorDev> delete(@RequestBody MonitorDev monitorDevEntity) {
        Result<MonitorDev> result = new Result<MonitorDev>();
        MonitorDev monitorDev = monitorDevService.getById(monitorDevEntity.getMonitorId());
        if (monitorDev == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = monitorDevService.removeById(monitorDevEntity.getMonitorId());
            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 = "Integer")
    @PostMapping(value = "/queryById")
    public Result<MonitorDev> queryById(@RequestBody MonitorDev monitorDevEntity) {
        Result<MonitorDev> result = new Result<MonitorDev>();
        MonitorDev monitorDev = monitorDevService.getById(monitorDevEntity.getMonitorId());
        if (monitorDev == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(monitorDev);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 监控点树形列表
     * @return
     */
    @OperLog(operModul = "项目视频管理", operType = "查询", operDesc = "监控点树形列表")
    @ApiOperation(value = "监控点树形列表", notes = "监控点树形列表", httpMethod = "POST")
    @ApiImplicitParam(name = "monitorName", value = "视频名称", paramType = "query", dataType = "String")
    @PostMapping(value = "/tree")
    public Result<List<MonitorTreeDto>> tree(@RequestBody Map<String, Object> map) {
        String monitorName = MapUtils.getString(map, "monitorName");
        SecurityUser user = SecurityUtil.getUser();
        LambdaQueryWrapper<Engineering> wrapper = Wrappers.<Engineering>lambdaQuery();
        if (StringUtils.isNotBlank(user.getEngineeringSn())) {
            wrapper.eq(Engineering::getEngineeringSn, user.getEngineeringSn());
        } else {
            wrapper.eq(Engineering::getProjectSn, user.getSn());
        }
        List<Engineering> engineeringList = engineeringService.list(wrapper);
        List<MonitorTreeDto> monitorTrees = monitorDevService.getMonitorTreeByEngineering(engineeringList, monitorName);
        return Result.success(monitorTrees);
    }
}
