package com.zehong.web.controller.risk;

import java.util.List;

import com.zehong.system.domain.SpaceListaw;
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.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.system.domain.TConfinedSpace;
import com.zehong.system.domain.SpaceListaw;
import com.zehong.system.service.ITConfinedSpaceService;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;

/**
 * 密闭空间和人员密集场所Controller
 *
 * @author zehong
 * @date 2026-03-18
 */
@RestController
@RequestMapping("/system/space")
public class TConfinedSpaceController extends BaseController
{
    @Autowired
    private ITConfinedSpaceService tConfinedSpaceService;

    /**
     * 查询密闭空间和人员密集场所列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TConfinedSpace tConfinedSpace)
    {
        startPage();
        List<TConfinedSpace> list = tConfinedSpaceService.selectTConfinedSpaceList(tConfinedSpace);
        return getDataTable(list);
    }

    /**
     * 导出密闭空间和人员密集场所列表
     */
    @Log(title = "密闭空间和人员密集场所", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TConfinedSpace tConfinedSpace)
    {
        List<TConfinedSpace> list = tConfinedSpaceService.selectTConfinedSpaceList(tConfinedSpace);
        ExcelUtil<TConfinedSpace> util = new ExcelUtil<TConfinedSpace>(TConfinedSpace.class);
        return util.exportExcel(list, "密闭空间和人员密集场所数据");
    }

    /**
     * 获取密闭空间和人员密集场所详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tConfinedSpaceService.selectTConfinedSpaceById(id));
    }

    /**
     * 新增密闭空间和人员密集场所
     */
    @Log(title = "密闭空间和人员密集场所", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SpaceListaw listaw)
    {
        //添加密闭空间表数据 并且返回新增 id
        tConfinedSpaceService.insertTConfinedSpace(listaw.gettConfinedSpace());
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(listaw.gettDeviceInfoS())){
            //将关联设备信息与密闭空间进行绑定
            tConfinedSpaceService.updatetRelationDeviceDetailInfo(listaw.gettDeviceInfoS(), listaw.gettConfinedSpace().getId(), listaw.gettConfinedSpace().getRelationDeviceType());
        }
        return AjaxResult.success();
    }

    /**
     * 修改密闭空间和人员密集场所
     */
    @Log(title = "密闭空间和人员密集场所", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SpaceListaw listaw)
    {
        //先更新密闭空间基本信息
        int result = tConfinedSpaceService.updateTConfinedSpace(listaw.gettConfinedSpace());
        
        //如果有关联设备数据，则更新关联关系
        if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(listaw.gettDeviceInfoS())) {
            tConfinedSpaceService.updatetRelationDeviceDetailInfo(
                listaw.gettDeviceInfoS(), 
                listaw.gettConfinedSpace().getId(), 
                listaw.gettConfinedSpace().getRelationDeviceType()
            );
        }
        
        return toAjax(result);
    }

    /**
     * 删除密闭空间和人员密集场所
     */
    @Log(title = "密闭空间和人员密集场所", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tConfinedSpaceService.deleteTConfinedSpaceByIds(ids));
    }
}