package com.ruoyi.web.controller.ems;

import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ems.domain.MeterBoundaryRel;
import com.ruoyi.ems.service.IMeterBoundaryRelService;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 计量设备绑定边界对象关系Controller
 *
 * @author ruoyi
 * @date 2024-12-09
 */
@RestController
@RequestMapping("/ems/meterBoundaryRel")
@Api(value = "MeterBoundaryRelController", description = "计量设备绑定边界对象关系")
public class MeterBoundaryRelController extends BaseController {
    @Autowired
    private IMeterBoundaryRelService relService;

    /**
     * 查询计量设备绑定边界对象关系列表
     */
    @PreAuthorize("@ss.hasPermi('ems:meterBoundaryRel:list')")
    @GetMapping("/listByObj")
    public AjaxResult list(@RequestParam(value = "objType") Integer objType,
        @RequestParam(value = "boundaryObj") String boundaryObj, @RequestParam(value = "meterCls") Integer meterCls) {
        List<MeterBoundaryRel> list = relService.selectRelByObj(boundaryObj, objType, meterCls);
        return success(list);
    }

    /**
     * 新增计量设备绑定边界对象关系
     */
    @PreAuthorize("@ss.hasPermi('ems:meterBoundaryRel:add')")
    @Log(title = "计量设备绑定边界对象关系", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public AjaxResult addBatch(@RequestBody List<MeterBoundaryRel> list) {
        return toAjax(relService.insertRelBatch(list));
    }

    /**
     * 新增计量设备绑定边界对象关系
     */
    @PreAuthorize("@ss.hasPermi('ems:meterBoundaryRel:add')")
    @Log(title = "计量设备绑定边界对象关系", businessType = BusinessType.UPDATE)
    @PutMapping("/merge")
    public AjaxResult merge(@RequestParam(value = "objType") Integer objType,
        @RequestParam(value = "boundaryObj") String boundaryObj, @RequestParam(value = "meterCls") Integer meterCls,
        @RequestBody List<MeterBoundaryRel> list) {

        relService.deleteRelByObj(objType, boundaryObj, meterCls);
        int cnt = 0;

        if (CollectionUtils.isNotEmpty(list)) {
            cnt = relService.insertRelBatch(list);
        }

        return toAjax(cnt);
    }

    /**
     * 删除计量设备绑定边界对象关系
     */
    @PreAuthorize("@ss.hasPermi('ems:meterBoundaryRel:remove')")
    @Log(title = "计量设备绑定边界对象关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/delByObj")
    public AjaxResult delByObj(@RequestParam(value = "objType") Integer objType,
        @RequestParam(value = "boundaryObj") String boundaryObj, @RequestParam(value = "meterCls") Integer meterCls) {
        return toAjax(relService.deleteRelByObj(objType, boundaryObj, meterCls));
    }
}
