package com.ruoyi.ems.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.ems.domain.EmsObjAttr;
import com.ruoyi.ems.domain.EmsObjAttrDto;
import com.ruoyi.ems.domain.EmsObjAttrEnum;
import com.ruoyi.ems.service.IEmsObjAttrEnumService;
import com.ruoyi.ems.service.IEmsObjAttrService;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @Autowired
    private IEmsObjAttrEnumService enumService;

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

    /**
     * 查询能源对象属性列表
     */
    @GetMapping("/enum/list")
    public AjaxResult list(@RequestParam(name="modelCode") String modelCode, @RequestParam(name = "attrKey")String attrKey) {
        List<EmsObjAttrEnum> list = enumService.getObjAttrEnumList(modelCode, attrKey);
        return AjaxResult.success(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 {
            JSONObject json = new JSONObject();
            json.put("objType", objType);
            json.put("objCode", objCode);

            // 获取属性列表
            List<EmsObjAttrDto> attrDtos = attrService.selectObjAttrItem(objType, objCode);

            if (CollectionUtils.isNotEmpty(attrDtos)) {
                Map<String, List<EmsObjAttrDto>> attrMap = attrDtos.stream()
                    .collect(Collectors.groupingBy(EmsObjAttrDto::getAttrGroup, Collectors.toList()));

                json.put("Base", attrMap.get("Base"));
                json.put("Protocol", attrMap.get("Protocol"));
                json.put("State", attrMap.get("State"));
                json.put("Measure", attrMap.get("Measure"));
            } else {
                json.put("Base", new JSONArray());
                json.put("Protocol",  new JSONArray());
                json.put("State",  new JSONArray());
                json.put("Measure",  new JSONArray());
            }

            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));
    }
}
