/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/10/19
 */
package org.springblade.modules.baseinfo.monitorpoint.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.baseinfo.monitorpoint.dto.MonitorPointInfoDTO;
import org.springblade.modules.baseinfo.monitorpoint.entity.MonitorPointInfoEntity;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointRelService;
import org.springblade.modules.baseinfo.monitorpoint.service.IMonitorPointService;
import org.springblade.modules.baseinfo.monitorpoint.vo.MonitorPointInfoVO;
import org.springblade.modules.baseinfo.monitorpoint.wrapper.MonitorPointWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/***
 * Date:2023/10/19
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/base/monitorpoint")
@AllArgsConstructor
@Api(value = "监测点模块", tags = "监测点接口")
public class MonitorPointController extends BladeController {

    private final IMonitorPointService monitorPointService;

    private final IMonitorPointRelService monitorPointRelService;

    /**
     * 多表联合查询自定义分页
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mpCd", value = "监测点代码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "mpNm", value = "监测点名称", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "orgId", value = "机构ID", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "wiustNm", value = "监测站名称", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<IPage<MonitorPointInfoVO>> page(@ApiIgnore MonitorPointInfoDTO dto, Query query) {
        IPage<MonitorPointInfoVO> pages = monitorPointService.selectPage(Condition.getPage(query), dto);
        List<MonitorPointInfoVO> list = pages.getRecords();
        for (MonitorPointInfoVO vo : list) {
            MonitorPointWrapper.build().entityVO(vo);
        }
        return R.data(pages);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R<MonitorPointInfoVO> detail(MonitorPointInfoEntity dto) {
        MonitorPointInfoEntity detail = monitorPointService.getOne(Condition.getQueryWrapper(dto));
        return R.data(MonitorPointWrapper.build().entityVO(detail));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R save(@RequestBody MonitorPointInfoDTO dto) {
        if (Func.isNull(dto.getWiustCd()) || Func.isNull(dto.getMpCd())) {
            return R.fail("参数错误");
        }
        LambdaQueryWrapper<MonitorPointInfoEntity> wrapper = Wrappers.<MonitorPointInfoEntity>query().lambda();
        wrapper.eq(MonitorPointInfoEntity::getIsDeleted, 0);
        wrapper.eq(MonitorPointInfoEntity::getMpCd, dto.getMpCd());
        long count = monitorPointService.count(wrapper);
        if (count > 0) {
            return R.fail("测点已经存在");
        }
        monitorPointRelService.addRel(dto.getWiustCd(), dto.getMpCd());
        return R.status(monitorPointService.save(dto));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "")
    public R update(@RequestBody MonitorPointInfoEntity dto) {
        return R.status(monitorPointService.updateById(dto));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "")
    public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
        List<Long> idArr = Func.toLongList(ids);
        for (Long id : idArr) {
            MonitorPointInfoEntity entity = monitorPointService.getById(id);
            monitorPointRelService.removeRel(entity.getMpCd());
        }
        boolean temp = monitorPointService.deleteLogic(Func.toLongList(ids));
        return R.status(temp);
    }
}
