package com.ruoyi.ss.toilet.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.store.domain.StoreVO;
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.mapper.ToiletMapper;
import com.ruoyi.ss.toilet.service.IToiletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 卫生间Service业务层处理
 *
 * @author qzz
 * @date 2025-01-07
 */
@Service
public class ToiletServiceImpl implements IToiletService
{
    @Autowired
    private ToiletMapper toiletMapper;

    @Autowired
    private IStoreService storeService;

    @Autowired
    private DeviceService deviceService;

    /**
     * 查询卫生间
     *
     * @param toiletId 卫生间主键
     * @return 卫生间
     */
    @Override
    public ToiletVO selectToiletByToiletId(Long toiletId)
    {
        ToiletVO toiletVO = toiletMapper.selectToiletByToiletId(toiletId);
        if(toiletVO != null && toiletVO.getDeviceId() != null){
            toiletVO.setDevice(deviceService.selectById(toiletVO.getDeviceId()));
        }
        return toiletVO;
    }

    /**
     * 查询卫生间列表
     *
     * @param toilet 卫生间
     * @return 卫生间
     */
    @Override
    public List<ToiletVO> selectToiletList(ToiletQuery toilet)
    {
        return toiletMapper.selectToiletList(toilet);
    }

    /**
     * 新增卫生间
     *
     * @param toilet 卫生间
     * @return 结果
     */
    @Override
    public int insertToilet(Toilet toilet)
    {
        if(StrUtil.isNotBlank(toilet.getCode())){
            ServiceUtil.assertion(toiletMapper.checkCodeIsExist(toilet.getCode(), toilet.getToiletId()), "房间码已存在,请重新生成");
        }
        StoreVO storeVO = storeService.selectEStoreByStoreId(toilet.getStoreId());
        ServiceUtil.assertion(storeVO == null,"店铺不存在");
        toilet.setStoreName(storeVO.getName());
        return toiletMapper.insertToilet(toilet);
    }

    /**
     * 修改卫生间
     *
     * @param toilet 卫生间
     * @return 结果
     */
    @Override
    public int updateToilet(Toilet toilet)
    {
        return toiletMapper.updateToilet(toilet);
    }

    /**
     * 修改店铺名称
     * @param storeId 店铺id
     * @param storeName 店铺名称
     * @return 结果
     */
    @Override
    public int updateStoreName(Long storeId, String storeName) {
        Toilet toilet =  new Toilet();
        toilet.setStoreId(storeId);
        toilet.setStoreName(storeName);
        return toiletMapper.updateStoreName(toilet);
    }

    /**
     * 批量删除卫生间
     *
     * @param toiletIds 需要删除的卫生间主键
     * @return 结果
     */
    @Override
    public int deleteToiletByToiletIds(Long[] toiletIds)
    {
        return toiletMapper.deleteToiletByToiletIds(toiletIds);
    }

    /**
     * 删除卫生间信息
     *
     * @param toiletId 卫生间主键
     * @return 结果
     */
    @Override
    public int deleteToiletByToiletId(Long toiletId)
    {
        return toiletMapper.deleteToiletByToiletId(toiletId);
    }

    /**
     * 下架设备（根据设备id）
     * @param deviceId 设备id
     * @return String
     */
    @Override
    public int offlineDeviceByDeviceId(Long deviceId) {
        return toiletMapper.offlineDeviceByDeviceId(deviceId);
    }

    /**
     * 查询数量
     * @param toilet 查询条件
     * @return String
     */
    @Override
    public int selectCount(ToiletQuery toilet) {
        return toiletMapper.selectCount(toilet);
    }

    /**
     * 开卫生间门
     * @param toiletVO 卫生间对象
     */
    @Override
    public boolean openToilet(ToiletVO toiletVO, String reason, boolean returnMac) {
        ServiceUtil.assertion(toiletVO == null, "卫生间不存在");
        ServiceUtil.assertion(toiletVO.getDeviceId() == null, "卫生间未绑定设备");
        return deviceService.controlDoor(toiletVO.getDeviceId(), toiletVO.getUnlockTime(), toiletVO.getUnlockMode(), true,reason,returnMac);
    }

}
