package com.pm.web.controller.publicBroadcasting;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.enums.BusinessType;
import com.pm.common.config.PageData;
import com.pm.common.utils.poi.ExcelUtilPageData;
import com.pm.subsystem.service.IIbmsBroadcastZoneService;
import com.pm.subsystem.service.IIbmsBroadcastDeviceService;
import com.pm.subsystem.service.IIbmsBroadcastContentService;
import com.pm.common.core.page.TableDataInfo;

/**
 * 广播系统Controller
 *
 * @author system
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/broadcast/system")
public class IbmsBroadcastController extends BaseController
{
    @Autowired
    private IIbmsBroadcastZoneService ibmsBroadcastZoneService;
    
    @Autowired
    private IIbmsBroadcastDeviceService ibmsBroadcastDeviceService;
    
    @Autowired
    private IIbmsBroadcastContentService ibmsBroadcastContentService;

    /**
     * 查询广播分区列表
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:list')")
    @GetMapping("/zone/list")
    public TableDataInfo zoneList()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = ibmsBroadcastZoneService.selectIbmsBroadcastZoneList(pd);
        return getDataTable(list);
    }

    /**
     * 导出广播分区列表
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:export')")
    @Log(title = "广播分区", businessType = BusinessType.EXPORT)
    @PostMapping("/zone/export")
    public void exportZone(HttpServletResponse response)
    {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = ibmsBroadcastZoneService.selectIbmsBroadcastZoneList(pd);
            String filename = "广播分区数据";
            String[] titles = {
                    "分区编码,zoneCode",
                    "分区名称,zoneName",
                    "分区类型,zoneType",
                    "楼栋,buildingName",
                    "楼层,floorName",
                    "空间位置,spaceLocation",
                    "设备数量,deviceCount",
                    "音量,volume",
                    "静音状态,isMute",
                    "在线状态,isOnline",
                    "广播状态,broadcastStatus",
                    "当前播放内容,currentContent",
                    "内容代号,contentCode",
                    "优先级,priority"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取广播分区详细信息
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:query')")
    @GetMapping(value = "/zone/{id}")
    public AjaxResult getZoneInfo(@PathVariable("id") Long id)
    {
        return success(ibmsBroadcastZoneService.selectIbmsBroadcastZoneById(id));
    }

    /**
     * 新增广播分区
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:add')")
    @Log(title = "广播分区", businessType = BusinessType.INSERT)
    @PostMapping("/zone")
    public AjaxResult addZone(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastZoneService.insertIbmsBroadcastZone(pd));
    }

    /**
     * 修改广播分区
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:edit')")
    @Log(title = "广播分区", businessType = BusinessType.UPDATE)
    @PutMapping("/zone")
    public AjaxResult editZone(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastZoneService.updateIbmsBroadcastZone(pd));
    }

    /**
     * 更新音量和静音状态
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:control')")
    @Log(title = "音量控制", businessType = BusinessType.UPDATE)
    @PostMapping("/zone/volume")
    public AjaxResult updateVolume(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastZoneService.updateZoneVolume(pd));
    }

    /**
     * 控制广播播放
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:broadcast')")
    @Log(title = "广播控制", businessType = BusinessType.OTHER)
    @PostMapping("/zone/control")
    public AjaxResult controlBroadcast(@RequestBody PageData pd)
    {
        Map<String, Object> result = ibmsBroadcastZoneService.controlBroadcast(pd);
        if ((boolean) result.get("success")) {
            return success(result.get("msg"));
        } else {
            return error((String) result.get("msg"));
        }
    }

    /**
     * 紧急广播
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:emergency')")
    @Log(title = "紧急广播", businessType = BusinessType.OTHER)
    @PostMapping("/zone/emergency")
    public AjaxResult emergencyBroadcast(@RequestBody PageData pd)
    {
        Map<String, Object> result = ibmsBroadcastZoneService.emergencyBroadcast(pd);
        if ((boolean) result.get("success")) {
            return AjaxResult.success(result);
        } else {
            return AjaxResult.error((String) result.get("msg"));
        }
    }

    /**
     * 删除广播分区
     */
    @PreAuthorize("@ss.hasPermi('broadcast:zone:remove')")
    @Log(title = "广播分区", businessType = BusinessType.DELETE)
    @DeleteMapping("/zone/{ids}")
    public AjaxResult removeZone(@PathVariable Long[] ids)
    {
        return toAjax(ibmsBroadcastZoneService.deleteIbmsBroadcastZoneByIds(ids));
    }

    /**
     * 查询广播设备列表
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:list')")
    @GetMapping("/device/list")
    public TableDataInfo deviceList()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = ibmsBroadcastDeviceService.selectIbmsBroadcastDeviceList(pd);
        
        // 获取统计信息
        PageData stats = ibmsBroadcastDeviceService.getDeviceStatistics();
        
        TableDataInfo dataTable = getDataTable(list);
        if (stats != null) {
            dataTable.setExtData(stats);
        }
        
        return dataTable;
    }

    /**
     * 根据分区ID查询设备
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:list')")
    @GetMapping("/device/zone/{zoneId}")
    public AjaxResult getZoneDevices(@PathVariable("zoneId") Long zoneId)
    {
        List<PageData> list = ibmsBroadcastDeviceService.getDevicesByZoneId(zoneId);
        return success(list);
    }

    /**
     * 导出广播设备列表
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:export')")
    @Log(title = "广播设备", businessType = BusinessType.EXPORT)
    @PostMapping("/device/export")
    public void exportDevice(HttpServletResponse response)
    {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = ibmsBroadcastDeviceService.selectIbmsBroadcastDeviceList(pd);
            String filename = "广播设备数据";
            String[] titles = {
                    "设备编码,deviceCode",
                    "设备名称,deviceName",
                    "设备类型,deviceType",
                    "所属分区,zoneName",
                    "IP地址,ipAddress",
                    "MAC地址,macAddress",
                    "品牌,brand",
                    "型号,model",
                    "功率,power",
                    "状态,status",
                    "在线状态,isOnline",
                    "故障代码,faultCode",
                    "故障描述,faultDesc",
                    "最后心跳时间,lastHeartbeat"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取广播设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:query')")
    @GetMapping(value = "/device/{id}")
    public AjaxResult getDeviceInfo(@PathVariable("id") Long id)
    {
        return success(ibmsBroadcastDeviceService.selectIbmsBroadcastDeviceById(id));
    }

    /**
     * 新增广播设备
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:add')")
    @Log(title = "广播设备", businessType = BusinessType.INSERT)
    @PostMapping("/device")
    public AjaxResult addDevice(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastDeviceService.insertIbmsBroadcastDevice(pd));
    }

    /**
     * 修改广播设备
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:edit')")
    @Log(title = "广播设备", businessType = BusinessType.UPDATE)
    @PutMapping("/device")
    public AjaxResult editDevice(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastDeviceService.updateIbmsBroadcastDevice(pd));
    }

    /**
     * 删除广播设备
     */
    @PreAuthorize("@ss.hasPermi('broadcast:device:remove')")
    @Log(title = "广播设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/device/{ids}")
    public AjaxResult removeDevice(@PathVariable Long[] ids)
    {
        return toAjax(ibmsBroadcastDeviceService.deleteIbmsBroadcastDeviceByIds(ids));
    }

    /**
     * 查询广播内容列表
     */
    @PreAuthorize("@ss.hasPermi('broadcast:content:list')")
    @GetMapping("/content/list")
    public TableDataInfo contentList()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = ibmsBroadcastContentService.selectIbmsBroadcastContentList(pd);
        return getDataTable(list);
    }

    /**
     * 获取广播内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('broadcast:content:query')")
    @GetMapping(value = "/content/{id}")
    public AjaxResult getContentInfo(@PathVariable("id") Long id)
    {
        return success(ibmsBroadcastContentService.selectIbmsBroadcastContentById(id));
    }

    /**
     * 新增广播内容
     */
    @PreAuthorize("@ss.hasPermi('broadcast:content:add')")
    @Log(title = "广播内容", businessType = BusinessType.INSERT)
    @PostMapping("/content")
    public AjaxResult addContent(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastContentService.insertIbmsBroadcastContent(pd));
    }

    /**
     * 修改广播内容
     */
    @PreAuthorize("@ss.hasPermi('broadcast:content:edit')")
    @Log(title = "广播内容", businessType = BusinessType.UPDATE)
    @PutMapping("/content")
    public AjaxResult editContent(@RequestBody PageData pd)
    {
        return toAjax(ibmsBroadcastContentService.updateIbmsBroadcastContent(pd));
    }

    /**
     * 删除广播内容
     */
    @PreAuthorize("@ss.hasPermi('broadcast:content:remove')")
    @Log(title = "广播内容", businessType = BusinessType.DELETE)
    @DeleteMapping("/content/{ids}")
    public AjaxResult removeContent(@PathVariable Long[] ids)
    {
        return toAjax(ibmsBroadcastContentService.deleteIbmsBroadcastContentByIds(ids));
    }
}