package com.usky.ems.controller.web;

import com.usky.common.core.bean.ApiResult;
import com.usky.ems.domain.DmpDevice;
import com.usky.ems.domain.EmsEnergyItemCode;
import com.usky.ems.service.EmsEnergyItemCodeService;
import com.usky.ems.service.EmsProjectService;
import com.usky.ems.service.vo.EmsDeviceItemCodeSaveRequest;
import com.usky.ems.service.vo.EmsProductEnergyTypeSaveRequest;
import com.usky.ems.service.vo.EmsProjectResponse;
import com.usky.ems.service.vo.EmsProjectSaveRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目（ems_project）新增、修改、删除
 * 基础路径前缀：/project（网关再加 /prod-api/service-ems）
 */
@RestController
@RequestMapping("/project")
public class EmsProjectController {

    @Autowired
    private EmsProjectService emsProjectService;
    @Autowired
    private EmsEnergyItemCodeService emsEnergyItemCodeService;

    /**
     * 能耗类型列表（SELECT * FROM ems_energy_item_code WHERE parent_code = 0）
     */
    @GetMapping("/energy-type/list")
    public ApiResult<List<EmsEnergyItemCode>> listEnergyTypes(
            @RequestParam(required = false) Integer energyType) {
        return ApiResult.success(emsEnergyItemCodeService.listEnergyTypes(energyType));
    }

    /**
     * 能耗分项列表（parent_code 为能耗类型编码）
     */
    @GetMapping("/energy-item/list")
    public ApiResult<List<EmsEnergyItemCode>> listEnergySubItems() {
        return ApiResult.success(emsEnergyItemCodeService.listEnergySubItems());
    }

    /**
     * 区域设备信息：按空间及其子空间关联网关，查询 dmp_device 子设备列表
     */
    @GetMapping("/area/device/list")
    public ApiResult<List<DmpDevice>> listAreaDevices(@RequestParam Integer spaceId) {
        return ApiResult.success(emsProjectService.listAreaDevices(spaceId));
    }

    /**
     * 区域能耗分项类型信息：按区域设备关联的分项编码查询 ems_energy_item_code
     */
    @GetMapping("/area/device-item-code/list")
    public ApiResult<List<EmsEnergyItemCode>> listAreaDeviceItemCodes(@RequestParam Integer spaceId) {
        return ApiResult.success(emsProjectService.listAreaDeviceItemCodes(spaceId));
    }

    /**
     * 产品关联的能源类型（ems_product_energy_type）
     */
    @PostMapping("/product-energy-type")
    public ApiResult<Void> saveProductEnergyTypes(@RequestBody EmsProductEnergyTypeSaveRequest request) {
        emsProjectService.saveProductEnergyTypes(request);
        return ApiResult.success();
    }

    /**
     * 设备关联的能源分项（ems_device_item_code）
     */
    @PostMapping("/device-item-code")
    public ApiResult<Void> saveDeviceItemCodes(@RequestBody EmsDeviceItemCodeSaveRequest request) {
        emsProjectService.saveDeviceItemCodes(request);
        return ApiResult.success();
    }

    @PostMapping
    public ApiResult<EmsProjectResponse> create(@RequestBody EmsProjectSaveRequest request) {
        return ApiResult.success(emsProjectService.save(request));
    }

    @PutMapping
    public ApiResult<Void> update(@RequestBody EmsProjectSaveRequest request) {
        emsProjectService.update(request);
        return ApiResult.success();
    }

    @DeleteMapping("/{spaceId}")
    public ApiResult<Void> delete(@PathVariable Long spaceId) {
        emsProjectService.remove(spaceId);
        return ApiResult.success();
    }
}
