package com.ruoyi.ems.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
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.common.security.annotation.RequiresPermissions;
import com.ruoyi.ems.domain.AreaBuilding;
import com.ruoyi.ems.domain.AreaBuildingZoning;
import com.ruoyi.ems.domain.EmsDevice;
import com.ruoyi.ems.domain.common.AreaTier;
import com.ruoyi.ems.domain.vo.QueryDevice;
import com.ruoyi.ems.service.IAreaBuildingService;
import com.ruoyi.ems.service.IAreaBuildingZoningService;
import com.ruoyi.ems.service.IEmsDeviceService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 能源设备Controller
 * 
 * @author ruoyi
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IEmsDeviceService deviceService;

    @Autowired
    private IAreaBuildingService buildingService;

    @Autowired
    private IAreaBuildingZoningService zoningService;

    /**
     * 查询能源设备列表
     */
    @RequiresPermissions("ems:device:list")
    @GetMapping("/list")
    public TableDataInfo list(EmsDevice emsDevice)
    {
        startPage();
        List<EmsDevice> list = deviceService.selectEmsDeviceList(emsDevice);
        return getDataTable(list);
    }

    /**
     * 查询能源设备列表
     */
    //@RequiresPermissions("basecfg:device:list")
    @GetMapping("/listByAreaTree")
    public TableDataInfo listByAreaTree(QueryDevice queryDevice)
    {
        // 层级为区域，需要查询下级建筑，以及建筑下级区块
        if (StringUtils.equals(queryDevice.getAreaType(), AreaTier.Area.name()) && StringUtils.isNotBlank(
            queryDevice.getRefArea())) {
            AreaBuilding param = new AreaBuilding();
            param.setAreaCode(queryDevice.getRefArea());
            List<AreaBuilding> buildings = buildingService.selectAreaBuildingList(param);
            List<String> buildingCodes = buildings.stream().map(AreaBuilding::getBldgCode).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(buildings)) {
                queryDevice.setBuildingCodes(buildingCodes);
                List<String> zoningCodes = new ArrayList<>();

                for (String buildCode : buildingCodes) {
                    AreaBuildingZoning zParam = new AreaBuildingZoning();
                    zParam.setBldgCode(buildCode);
                    List<AreaBuildingZoning> list = zoningService.selectAreaBuildingZoningList(zParam);
                    zoningCodes.addAll(
                        list.stream().map(AreaBuildingZoning::getZoningCode).collect(Collectors.toList()));
                }

                if (CollectionUtils.isNotEmpty(zoningCodes)) {
                    queryDevice.setZoningCodes(zoningCodes);
                }
            }
        }
        else if (StringUtils.equals(queryDevice.getAreaType(), AreaTier.Building.name()) && StringUtils.isNotBlank(
            queryDevice.getRefArea())) {
            AreaBuildingZoning zParam = new AreaBuildingZoning();
            zParam.setBldgCode(queryDevice.getRefArea());
            List<AreaBuildingZoning> list = zoningService.selectAreaBuildingZoningList(zParam);
            List<String> zoningCodes = list.stream().map(AreaBuildingZoning::getZoningCode)
                .collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(zoningCodes)) {
                queryDevice.setZoningCodes(zoningCodes);
            }
        }

        startPage();
        List<EmsDevice> list = deviceService.selectByAreaTree(queryDevice);
        return getDataTable(list);
    }

    /**
     * 导出能源设备列表
     */
    @RequiresPermissions("ems:device:export")
    @Log(title = "能源设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmsDevice emsDevice)
    {
        List<EmsDevice> list = deviceService.selectEmsDeviceList(emsDevice);
        ExcelUtil<EmsDevice> util = new ExcelUtil<EmsDevice>(EmsDevice.class);
        util.exportExcel(response, list, "能源设备数据");
    }

    /**
     * 获取能源设备详细信息
     */
    @RequiresPermissions("ems:device:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deviceService.selectEmsDeviceById(id));
    }

    /**
     * 新增能源设备
     */
    @RequiresPermissions("ems:device:add")
    @Log(title = "能源设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmsDevice emsDevice)
    {
        return toAjax(deviceService.insertEmsDevice(emsDevice));
    }

    /**
     * 修改能源设备
     */
    @RequiresPermissions("ems:device:edit")
    @Log(title = "能源设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmsDevice emsDevice)
    {
        return toAjax(deviceService.updateEmsDevice(emsDevice));
    }

    /**
     * 删除能源设备
     */
    @RequiresPermissions("ems:device:remove")
    @Log(title = "能源设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(deviceService.deleteEmsDeviceByIds(ids));
    }
}
