package com.pm.web.controller.patrol;

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.*;
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.patrol.service.IPatrolPointService;
import com.pm.common.core.page.TableDataInfo;

/**
 * 巡更点信息Controller
 *
 * @author pm
 * @date 2025-06-03
 */
@RestController
@RequestMapping("/patrol/point")
public class PatrolPointController extends BaseController {
    
    @Autowired
    private IPatrolPointService patrolPointService;

    /**
     * 查询巡更点列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:list')")
    @GetMapping("/list")
    public TableDataInfo list() {
        PageData pd = this.getPageData();
        startPage();
        List<PageData> list = patrolPointService.selectPatrolPointList(pd);
        return getDataTable(list);
    }

    /**
     * 获取巡更点详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(patrolPointService.selectPatrolPointById(id));
    }

    /**
     * 新增巡更点
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:add')")
    @Log(title = "巡更点信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageData pd) {
        return toAjax(patrolPointService.insertPatrolPoint(pd));
    }

    /**
     * 修改巡更点
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:edit')")
    @Log(title = "巡更点信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageData pd) {
        return toAjax(patrolPointService.updatePatrolPoint(pd));
    }

    /**
     * 删除巡更点
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:remove')")
    @Log(title = "巡更点信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(patrolPointService.deletePatrolPointByIds(ids));
    }

    /**
     * 修改巡更点状态
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:edit')")
    @Log(title = "巡更点状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody PageData pd) {
        return toAjax(patrolPointService.updatePatrolPointStatus(pd));
    }

    /**
     * 导出巡更点列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:point:export')")
    @Log(title = "巡更点信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            PageData pd = this.getPageData();
            List<PageData> list = patrolPointService.selectPatrolPointList(pd);
            String filename = "巡更点信息数据";
            String[] titles = {
                    "巡更点ID,id",
                    "巡更点编号,point_code",
                    "巡更点名称,point_name",
                    "区域ID,area_id",
                    "区域名称,area_name",
                    "楼栋ID,building_id",
                    "楼栋名称,building_name",
                    "楼层ID,floor_id",
                    "楼层名称,floor_name",
                    "X坐标,position_x",
                    "Y坐标,position_y",
                    "Z坐标,position_z",
                    "巡更点类型,point_type",
                    "巡更频率,patrol_frequency",
                    "关联摄像头,camera_ids",
                    "巡更设备ID,device_id",
                    "二维码内容,qr_code",
                    "是否启用,is_active",
                    "备注,remark"
            };
            ExcelUtilPageData.exportXLSX(response, null, null, filename, titles, list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}