package com.ruoyi.web.controller.ems;

import com.alibaba.fastjson2.JSONObject;
import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
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.EmsObjAttr;
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.IEmsObjAttrService;
import com.ruoyi.ems.service.IEmsObjAttrValueService;
import io.swagger.annotations.Api;
import org.apache.commons.collections4.CollectionUtils;
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.ArrayList;
import java.util.List;

/**
 * 能源对象属性Controller
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/ems/object/attr")
@Api(value = "EmsObjAttrController", description = "能源对象属性管理")
public class EmsObjAttrController extends BaseController {
    @Autowired
    private IEmsObjAttrService attrService;

    @Autowired
    private IEmsObjAttrValueService objAttrValueService;

    @Autowired
    private IEmsDeviceService deviceService;

    @Autowired
    private IEmsFacsService facsService;

    /**
     * 查询能源对象属性列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EmsObjAttr objAttr) {
        startPage();
        List<EmsObjAttr> list = attrService.selectObjAttrList(objAttr);
        return getDataTable(list);
    }

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

    /**
     * 查询能源对象属性值列表
     */
    @GetMapping("/getObjAttr")
    public AjaxResult list(@RequestParam(name = "objType") Integer objType,
        @RequestParam(name = "objCode") String objCode) {
        AjaxResult result = null;

        try {
            String modeCode = null;

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

            JSONObject json = new JSONObject();
            json.put("objType", objType);
            json.put("objCode", objCode);
            json.put("modeCode", modeCode);

            List<EmsObjAttr> modelAttrs = attrService.selectByModelCode(modeCode);
            json.put("attrs", CollectionUtils.isNotEmpty(modelAttrs) ? modelAttrs : new ArrayList<>());

            List<EmsObjAttrValue> attrValues = objAttrValueService.selectByObjCode(modeCode, objCode);
            json.put("attrValues", attrValues);

            result = success(json);
        }
        catch (Exception e) {
            result = error(e.getMessage());
        }

        return result;
    }

    /**
     * 新增能源对象属性
     */
    @Log(title = "能源对象属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsObjAttr objAttr) {
        return toAjax(attrService.insertObjAttr(objAttr));
    }

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

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

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