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.common.exception.Assert;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.EmsFacs;
import com.ruoyi.ems.domain.EmsObjAttrValue;
import com.ruoyi.ems.enums.DevObjType;
import com.ruoyi.ems.service.IEmsDeviceService;
import com.ruoyi.ems.service.IEmsFacsService;
import com.ruoyi.ems.service.IEmsObjAttrValueService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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-09-25
 */
@RestController
@RequestMapping("/ems/object/attr/value")
@Api(value = "EmsObjAttrValueController", description = "能源对象属性值管理")
public class EmsObjAttrValueController extends BaseController {
    @Autowired
    private IEmsObjAttrValueService objAttrValueService;

    @Autowired
    private IEmsDeviceService deviceService;

    @Autowired
    private IEmsFacsService facsService;

    /**
     * 查询能源对象属性值列表
     */
    @GetMapping("/list")
    public AjaxResult list(EmsObjAttrValue objAttrValue) {
        List<EmsObjAttrValue> list = objAttrValueService.selectObjAttrValueList(objAttrValue);
        return success(list);
    }

    /**
     * 获取能源对象属性值详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(objAttrValueService.selectObjAttrValueById(id));
    }

    /**
     * 新增能源对象属性值
     */
    @Log(title = "能源对象属性值", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<EmsObjAttrValue> list) {
        return toAjax(objAttrValueService.insertBatch(list));
    }

    /**
     * 修改能源对象属性值
     */
    @Log(title = "能源对象属性值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsObjAttrValue EmsObjAttrValue) {
        return toAjax(objAttrValueService.mergeObjAttrValue(EmsObjAttrValue));
    }

    /**
     * 删除能源对象属性值
     */
    @Log(title = "能源对象属性值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(objAttrValueService.deleteObjAttrValueByIds(ids));
    }

    /**
     * 删除能源对象属性值
     */
    @Log(title = "能源对象属性值", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteByObj")
    public AjaxResult removeByObj(@RequestParam(name = "objType") Integer objType,
        @RequestParam(name = "objCode") String objCode) {

        String modeCode = null;
        int cnt = 0;

       if (objType == DevObjType.DEVC.getCode()) {
            EmsDevice device = deviceService.selectByCode(objCode);
            Assert.notNull(device, -1, "能源设备不存在");
            modeCode = device.getDeviceModel();
            cnt = objAttrValueService.deleteByObjCode(modeCode, objCode);
        }

        return toAjax(cnt);
    }
}
