package com.ruoyi.web.controller.app;

import com.alibaba.fastjson2.JSON;
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.domain.ValidGroup;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.store.service.IStoreService;
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.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 卫生间Controller
 *
 * @author qzz
 * @date 2025-01-07
 */
@RestController
@RequestMapping("/app/toilet")
public class AppToiletController extends BaseController {

    @Resource
    private IToiletService toiletService;

    @Autowired
    private IStoreService storeService;


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

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

    /**
     * 新增卫生间
     */
    @Log(title = "卫生间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) Toilet toilet)
    {
        logger.info("新增卫生间：【toilet={}】", JSON.toJSONString(toilet));
        if(toilet.getMerchantId() == null){
            toilet.setMerchantId(getUserId());
        }
        return toAjax(toiletService.insertToilet(toilet));
    }

    /**
     * 修改卫生间
     */
    @Log(title = "卫生间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) Toilet toilet)
    {
        logger.info("修改卫生间：【toilet={}】", JSON.toJSONString(toilet));
        return toAjax(toiletService.updateToilet(toilet));
    }

    /**
     * 删除卫生间
     */
    @Log(title = "卫生间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{toiletIds}")
    public AjaxResult remove(@PathVariable Long[] toiletIds)
    {
        logger.info("删除卫生间：【toiletIds={}】", JSON.toJSONString(toiletIds));
        for(Long toiletId : toiletIds){
            ToiletVO toiletVO = toiletService.selectToiletByToiletId(toiletId);
            ToiletQuery toiletQuery = new ToiletQuery();
            toiletQuery.setStoreId(toiletVO.getStoreId());
            // 写一个根据店铺id查询是否只有一个卫生间的方法
            int i = toiletService.selectCount(toiletQuery);
            ServiceUtil.assertion(i<=1,"至少保留一个卫生间");
        }
        return toAjax(toiletService.deleteToiletByToiletIds(toiletIds));
    }

}
