package com.ruoyi.fire.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.fire.domain.FireFacility;
import com.ruoyi.fire.service.IFireFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * facilitiesController
 *
 * @author Junhua
 * @date 2025-07-15
 */
@Anonymous
@RestController
@RequestMapping("/fire/facility")
public class FireFacilityController extends BaseController {


    @Autowired
    private IFireFacilityService fireFacilityService;

    /**
     * 检查并更新设施状态
     */
    @GetMapping("/check-status/{id}")
    public AjaxResult checkAndUpdateStatus(@PathVariable Long id) {



        int result = fireFacilityService.checkAndUpdateStatus(id);


        if (result > 0) {
            return AjaxResult.success("状态已更新");
        }
        return AjaxResult.success("状态无需更新");
    }


/// 获取所有设施列表



@GetMapping("/list")
public TableDataInfo list(FireFacility fireFacility) {
    // 获取当前分页信息
    PageDomain pageDomain = TableSupport.buildPageRequest();
    Integer pageNum = pageDomain.getPageNum();
    Integer pageSize = pageDomain.getPageSize();

    // 传递分页参数到状态更新方法
    fireFacilityService.checkAndUpdateExpirationStatus(fireFacility, pageNum, pageSize);

    // 执行分页查询
    startPage();
    List<FireFacility> list = fireFacilityService.selectFireFacilityList(fireFacility);

    return getDataTable(list);
}

    /**
     * 查询根据id查询设施列表
     */
//    @PreAuthorize("@ss.hasPermi('fire:facility:list')")
    @GetMapping("/details")
    public TableDataInfo getDetails(FireFacility fireFacility) {
        startPage();
        List<FireFacility> list = fireFacilityService.getDetails(fireFacility);
        return getDataTable(list);
    }

    /**
     * 导出facilities列表
     */
//    @PreAuthorize("@ss.hasPermi('fire:facility:export')")
    @Log(title = "facilities", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FireFacility fireFacility) {

        List<FireFacility> list = fireFacilityService.selectFireFacilityList(fireFacility);
        ExcelUtil<FireFacility> util = new ExcelUtil<FireFacility>(FireFacility.class);
        // 调试：打印前 5 条数据的 remark 值
        list.stream().limit(5).forEach(f -> {
            System.out.println("ID: " + f.getId() + ", Remark: " + f.getRemark());
        });
        util.exportExcel(response, list, "facilities数据");
    }

    /**
     * 获取facilities详细信息
     */
//    @PreAuthorize("@ss.hasPermi('fire:facility:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fireFacilityService.selectFireFacilityById(id));
    }

    /**
     * 新增facilities
     */
//    @PreAuthorize("@ss.hasPermi('fire:facility:add')")
    @Log(title = "facilities", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FireFacility fireFacility) {
        return toAjax(fireFacilityService.insertFireFacility(fireFacility));
    }

    /**
     * 修改facilities
     */
//    @PreAuthorize("@ss.hasPermi('fire:facility:edit')")
    @Log(title = "facilities", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FireFacility fireFacility) {
        return toAjax(fireFacilityService.updateFireFacility(fireFacility));
    }

    /**
     * 删除facilities
     */
//    @PreAuthorize("@ss.hasPermi('fire:facility:remove')")
    @Log(title = "facilities", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fireFacilityService.deleteFireFacilityByIds(ids));
    }
    /**
     * 导入Excel数据
     * @param file Excel文件
     * @param updateSupport 是否支持更新（1=支持，0=不支持）
     * @return 导入结果
     */
    @Log(title = "消防设施", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, @RequestParam(defaultValue = "0") String updateSupport) throws Exception {

        boolean isUpdate = "1".equals(updateSupport); // 1表示支持更新，0表示仅新增
        String result = fireFacilityService.importFireFacility(file, isUpdate);
        return success(result);
    }

    /**
     * 下载导入模板
     * @param response 响应对象
     */
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<FireFacility> util = new ExcelUtil<>(FireFacility.class);
        util.exportExcel(response, null, "消防设施导入模板");
    }
}
