package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.toilet.domain.Toilet;
import com.ruoyi.ss.toilet.domain.ToiletQuery;
import com.ruoyi.ss.toilet.domain.ToiletVO;
import com.ruoyi.ss.toilet.service.IToiletService;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 卫生间Controller
 *
 * @author qzz
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/system/toilet")
public class ToiletController extends BaseController
{
    @Resource
    private IToiletService toiletService;

    /**
     * 查询卫生间列表
     */
    @PreAuthorize("@ss.hasPermi('system:toilet:list')")
    @GetMapping("/list")
    public TableDataInfo list(ToiletQuery query)
    {
        startPage();
        startOrderBy();
        List<ToiletVO> list = toiletService.selectToiletList(query);
        return getDataTable(list);
    }

    /**
     * 导出卫生间列表
     */
    @PreAuthorize("@ss.hasPermi('system:toilet:export')")
    @Log(title = "卫生间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ToiletQuery query)
    {
        List<ToiletVO> list = toiletService.selectToiletList(query);
        ExcelUtil<ToiletVO> util = new ExcelUtil<ToiletVO>(ToiletVO.class);
        util.exportExcel(response, list, "卫生间数据");
    }

    /**
     * 获取卫生间详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:toilet:query')")
    @GetMapping(value = "/{toiletId}")
    public AjaxResult getInfo(@PathVariable("toiletId") Long toiletId)
    {
        return success(toiletService.selectToiletByToiletId(toiletId));
    }

    /**
     * 新增卫生间
     */
    @PreAuthorize("@ss.hasPermi('system:toilet:add')")
    @Log(title = "卫生间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Toilet toilet)
    {
        if(toilet.getMerchantId() == null){
            toilet.setMerchantId(getUserId());
        }
        return toAjax(toiletService.insertToilet(toilet));
    }

    /**
     * 修改卫生间
     */
    @PreAuthorize("@ss.hasPermi('system:toilet:edit')")
    @Log(title = "卫生间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Toilet toilet)
    {
        return toAjax(toiletService.updateToilet(toilet));
    }

    /**
     * 删除卫生间
     */
    @PreAuthorize("@ss.hasPermi('system:toilet:remove')")
    @Log(title = "卫生间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{toiletIds}")
    public AjaxResult remove(@PathVariable Long[] toiletIds)
    {
        return toAjax(toiletService.deleteToiletByToiletIds(toiletIds));
    }

    /**
     * 开卫生间(管理员)
     */
    @GetMapping(value = "/open/{toiletId}")
    public AjaxResult openToilet(@PathVariable("toiletId") Long toiletId)
    {
        logger.info("开卫生间：【toiletId={}】", toiletId);
        ToiletVO toiletVO = toiletService.selectToiletByToiletId(toiletId);
        return success(toiletService.openToilet(toiletVO, "管理员开卫生间",false));
    }
}
