package com.pm.web.controller.device;

import java.util.List;
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.control.service.IIbmsDeviceControlLogService;
import com.pm.common.core.page.TableDataInfo;

/**
 * 设备控制记录Controller
 *
 * @author LiXiangFei
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/device/control")
public class IbmsDeviceControlLogController extends BaseController
{
    @Autowired
    private IIbmsDeviceControlLogService ibmsDeviceControlLogService;

    /**
     * 查询设备控制记录列表
     */
    @PreAuthorize("@ss.hasPermi('device:control:list')")
    @GetMapping("/list")
    public TableDataInfo list()
    {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = ibmsDeviceControlLogService.selectIbmsDeviceControlLogList(pd);
        return getDataTable(list);
    }

    /**
     * 导出设备控制记录列表
     */
    @PreAuthorize("@ss.hasPermi('device:control:export')")
    @Log(title = "设备控制记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = ibmsDeviceControlLogService.selectIbmsDeviceControlLogList(pd);
            String filename = "设备控制记录数据";
            String[] titles = {
                    "主键ID,id",
                    "设备ID,deviceId",
                    "点位ID,pointId",
                    "控制类型,controlType",
                    "原值,oldValue",
                    "新值,newValue",
                    "控制结果(0-失败,1-成功),controlResult",
                    "操作人ID,operatorId",
                    "操作人姓名,operatorName",
                    "操作时间,operateTime"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取设备控制记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:control:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ibmsDeviceControlLogService.selectIbmsDeviceControlLogById(id));
    }

    /**
     * 新增设备控制记录
     */
    @PreAuthorize("@ss.hasPermi('device:control:add')")
    @Log(title = "设备控制记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceControlLogService.insertIbmsDeviceControlLog(pd));
    }

    /**
     * 修改设备控制记录
     */
    @PreAuthorize("@ss.hasPermi('device:control:edit')")
    @Log(title = "设备控制记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd)
    {
        return toAjax(ibmsDeviceControlLogService.updateIbmsDeviceControlLog(pd));
    }

    /**
     * 删除设备控制记录
     */
    @PreAuthorize("@ss.hasPermi('device:control:remove')")
    @Log(title = "设备控制记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ibmsDeviceControlLogService.deleteIbmsDeviceControlLogByIds(ids));
    }
}
