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

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.device.service.DeviceService;
import com.ruoyi.ss.equipment.domain.Equipment;
import com.ruoyi.ss.equipment.domain.EquipmentQuery;
import com.ruoyi.ss.equipment.domain.EquipmentVO;
import com.ruoyi.ss.equipment.domain.unified.UnifiedEquipment;
import com.ruoyi.ss.equipment.domain.unified.UnifiedEquipmentQueryDTO;
import com.ruoyi.ss.equipment.mapper.EquipmentMapper;
import com.ruoyi.ss.equipment.service.IEquipmentService;
import com.ruoyi.ss.room.domain.Room;
import com.ruoyi.ss.room.domain.RoomQuery;
import com.ruoyi.ss.room.domain.RoomVO;
import com.ruoyi.ss.room.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.ruoyi.common.constant.ServiceConstants.ROOM_TYPE2_LOBBY_DEVICE;
import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 设施Service业务层处理
 *
 * @author qzz
 * @date 2024-12-21
 */
@Service
public class EquipmentServiceImpl implements IEquipmentService
{
    @Autowired
    private EquipmentMapper equipmentMapper;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private TransactionTemplate transactionTemplate;
    /**
     * 查询设施
     *
     * @param equipmentId 设施主键
     * @return 设施
     */
    @Override
    public EquipmentVO selectEquipmentByEquipmentId(Long equipmentId)
    {
        return equipmentMapper.selectEquipmentByEquipmentId(equipmentId);
    }

    /**
     * 查询设施列表
     *
     * @param equipment 设施
     * @return 设施
     */
    @Override
    public List<EquipmentVO> selectEquipmentList(EquipmentQuery equipment)
    {
        return equipmentMapper.selectEquipmentList(equipment);
    }


    /**
     * 根据房间id查询设施列表
     *
     * @param roomId 房间id
     * @return 设施集合
     */
    @Override
    public List<EquipmentVO> selectEquipmentListByRoomId(Long roomId) {
        return equipmentMapper.selectEquipmentListByRoomId(roomId);
    }

    /**
     * 新增设施
     *
     * @param equipment 设施
     * @return 结果
     */
    @Override
    public int insertEquipment(Equipment equipment)
    {
        equipment.setCreateTime(DateUtils.getNowDate());
        RoomVO roomVO = roomService.selectERoomByRoomId(equipment.getRoomId());
        if(ObjectUtil.isNotNull(roomVO)){
            equipment.setStoreId(roomVO.getStoreId());
            equipment.setStoreName(roomVO.getStoreName());
            Long merchantId = roomVO.getMerchantId();
            if(ObjectUtil.isNull(roomVO.getMerchantId())){
                merchantId = getUserId();
            }
            equipment.setMerchantId(merchantId);
        }
        return equipmentMapper.insertEquipment(equipment);
    }

    /**
     * 修改设施
     *
     * @param equipment 设施
     * @return 结果
     */
    @Override
    public int updateEquipment(Equipment equipment)
    {
        return equipmentMapper.updateEquipment(equipment);
    }

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

    /**
     * 根据房间id修改设施
     *
     * @param equipment 设施
     * @return 结果
     */
    @Override
    public int updateEquipmentByRoomId(Equipment equipment)
    {
        return equipmentMapper.updateEquipmentByRoomId(equipment);
    }

    /**
     * 批量删除设施
     *
     * @param equipmentIds 需要删除的设施主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEquipmentIds(Long[] equipmentIds)
    {
        Integer result = transactionTemplate.execute(status -> {
            int i = equipmentMapper.deleteEquipmentByEquipmentIds(equipmentIds);
            for(Long equipmentId: equipmentIds){
                EquipmentVO equipmentVO = equipmentMapper.selectEquipmentByEquipmentId(equipmentId);
                if(equipmentVO != null && equipmentVO.getDeviceId() != null){
                    int unbind = deviceService.unbind(equipmentVO.getDeviceId());
                    ServiceUtil.assertion(unbind == 0, "设备【"+equipmentVO.getDeviceId()+"】解绑失败");
                }
            }
            return i;
        });
        return result == null ? 0 : result;
    }

    /**
     * 根据房间删除设施
     *
     * @param roomIds 需要删除的房间id集合
     * @return 结果
     */
    @Override
    public int deleteEquipmentByRoomId(Long[] roomIds) {
        List<Long> longs = equipmentMapper.selectAllEquipmentIdsByRoomIds(Arrays.asList(roomIds));
        if(!longs.isEmpty()){
            deleteEquipmentByEquipmentIds(longs.toArray(new Long[0]));
        }
        return 1;
    }


    /**
     * 删除设施信息
     *
     * @param equipmentId 设施主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentByEquipmentId(Long equipmentId)
    {
        return equipmentMapper.deleteEquipmentByEquipmentId(equipmentId);
    }

    /**
     * 设备投放的房间id
     * @param deviceId 设备id
     * @return String
     */
    @Override
    public List<Long> selectRoomIdByDeviceId(Long deviceId) {
        return equipmentMapper.selectRoomIdByDeviceId(deviceId);
    }

    /**
     * 通过房间id查询所有设备id（除房间门外）
     * @param roomId 房间id
     * @return String
     */
    @Override
    public List<Long> selectDeviceIdListByRoomId(Long roomId) {
        return equipmentMapper.selectDeviceIdListByRoomId(roomId);
    }


    @Override
    public List<Long> selectAllEquipmentIdsByRoomId(Long roomId) {
        return equipmentMapper.selectAllEquipmentIdsByRoomId(roomId);
    }

    @Override
    public List<Long> selectAllHallEquIdsByStoreId(Long storeId) {
        RoomQuery query = new RoomQuery();
        query.setStoreId(storeId);
        query.setType2(ROOM_TYPE2_LOBBY_DEVICE);
        List<Long> roomIds = roomService.selectRoomIdsList(query);
        return equipmentMapper.selectAllEquipmentIdsByRoomIds(roomIds);
    }

    /**
     * 校验店铺是否已经绑定了大门
     * @param storeId 店铺id
     * @return String
     */
    @Override
    public boolean uniqueGate(Long storeId) {
        return equipmentMapper.uniqueGate(storeId);
    }

//    /**
//     * 根据店铺查询大门
//     * @param storeId 店铺id
//     * @return String
//     */
//    @Override
//    public EquipmentVO selectGate(Long storeId) {
//        return equipmentMapper.selectGate(storeId);
//    }

    /**
     * 根据设备id查询店铺
     * @param deviceId 设备id
     * @return String
     */
    @Override
    public Long selectStoreIdByDeviceId(Long deviceId) {
        return equipmentMapper.selectStoreIdByDeviceId(deviceId);
    }

    /**
     * 根据设备id查询设施对象
     * @param deviceId 设备id
     * @return String
     */
    @Override
    public EquipmentVO selectByDeviceId(Long deviceId) {
        return equipmentMapper.selectByDeviceId(deviceId);
    }

//    /**
//     * 下架设备
//     * @param equipmentId 设施id
//     * @return String
//     */
//    @Override
//    public int offlineDevice(Long equipmentId) {
//        return equipmentMapper.offlineDevice(equipmentId);
//    }

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

    @Override
    public int selectIdleEquipmentNumByStoreId(Long storeId) {
        return equipmentMapper.selectIdleEquipmentNumByStoreId(storeId);
    }

    /**
     * 根据房间id查询门
     * @param roomId 房间id
     * @param type 设施类型
     * @return String
     */
    @Override
    public EquipmentVO selectDoorByRoomId(Long roomId,String type) {
        return equipmentMapper.selectDoorByRoomId(roomId,type);
    }

    /**
     * 统一查询设施列表
     * @param query 查询条件
     * @return String
     */
    @Override
    public List<UnifiedEquipment> selectUnifiedEquipmentList(UnifiedEquipmentQueryDTO query) {
        return equipmentMapper.selectUnifiedEquipmentList(query);
    }

//    /**
//     * 套餐是否被应用
//     */
//    @Override
//    public List<String> selectIsExistRule(Long ruleId) {
//        return equipmentMapper.selectIsExistRule(ruleId);
//    }


}
