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

import com.baomidou.mybatisplus.core.metadata.IPage;
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.monitorsite.service.IMonitorSiteRelService;
import org.springblade.modules.baseinfo.monitorsite.service.IMonitorSiteService;
import org.springblade.modules.baseinfo.wateruseconsumer.entity.WaterUseConsumerInfoEntity;
import org.springblade.modules.baseinfo.wateruseconsumer.service.IWaterUseConsumerService;
import org.springblade.modules.baseinfo.wateruseconsumer.vo.WaterUseConsumerInfoVO;
import org.springblade.modules.baseinfo.wateruseconsumer.wrapper.WaterUseConsumerWrapper;
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/wateruse")
@AllArgsConstructor
@Api(value = "监测点模块", tags = "监测点接口")
public class WaterUseConsumerController extends BladeController {
    private final IWaterUseConsumerService waterUseConsumerService;
    private final IMonitorSiteService monitorSiteService;
    private final IMonitorSiteRelService monitorSiteRelService;

    /**
     * 多表联合查询自定义分页
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wiuCd", value = "用水户代码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "wiuNm", value = "用水户名称", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<IPage<WaterUseConsumerInfoVO>> page(@ApiIgnore WaterUseConsumerInfoEntity dto, Query query) {
        IPage<WaterUseConsumerInfoVO> pages = waterUseConsumerService.selectPage(Condition.getPage(query), dto);
        List<WaterUseConsumerInfoVO> list = pages.getRecords();
        for (WaterUseConsumerInfoVO vo : list) {
            WaterUseConsumerWrapper.build().entityVO(vo);
        }
        return R.data(pages);
    }

    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wiuCd", value = "用水户代码", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "wiuNm", value = "用水户名称", paramType = "query", dataType = "string"),
    })
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页")
    public R<List<WaterUseConsumerInfoVO>> list(@ApiIgnore WaterUseConsumerInfoEntity dto) {
        List<WaterUseConsumerInfoEntity> list = waterUseConsumerService.selectAll(dto);
        return R.data(WaterUseConsumerWrapper.build().listVO(list));
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "")
    public R<WaterUseConsumerInfoVO> detail(WaterUseConsumerInfoEntity entity) {
        WaterUseConsumerInfoEntity detail = waterUseConsumerService.getOne(Condition.getQueryWrapper(entity));
        return R.data(WaterUseConsumerWrapper.build().entityVO(detail));
    }


    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "")
    public R save(@RequestBody WaterUseConsumerInfoEntity dto) {
        return R.status(waterUseConsumerService.save(dto));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "")
    public R update(@RequestBody WaterUseConsumerInfoEntity dto) {
        return R.status(waterUseConsumerService.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) {
            WaterUseConsumerInfoEntity waterUseConsumerInfoEntity = waterUseConsumerService.getById(id);
            monitorSiteService.removeSite(waterUseConsumerInfoEntity.getWiuCd());
        }
        boolean temp = waterUseConsumerService.deleteLogic(Func.toLongList(ids));
        return R.status(temp);
    }
}
