package com.jeeplus.hotelorder.room.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.hotelorder.room.domain.HotelRoomType;
import com.jeeplus.hotelorder.room.mapper.HotelRoomTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 房间类型服务类
 * @author jeeplus
 */
@Service
public class HotelRoomTypeService extends ServiceImpl<HotelRoomTypeMapper, HotelRoomType> {

    @Resource
    private HotelRoomTypeMapper hotelRoomTypeMapper;

    /**
     * 分页查询房间类型
     */
    public IPage<HotelRoomType> findPage(Page<HotelRoomType> page, HotelRoomType hotelRoomType) throws Exception {
        QueryWrapper<HotelRoomType> queryWrapper = QueryWrapperGenerator.buildQueryCondition(hotelRoomType, HotelRoomType.class);
        queryWrapper.eq("del_flag", 0);
        queryWrapper.orderByAsc("sort_order");
        return hotelRoomTypeMapper.selectPage(page, queryWrapper);
    }

    /**
     * 查询所有启用的房间类型（下拉框用）
     */
    public List<HotelRoomType> findAllEnabled() {
        QueryWrapper<HotelRoomType> qw = new QueryWrapper<>();
        qw.eq("status", "ENABLED").eq("del_flag", 0).orderByAsc("sort_order");
        return hotelRoomTypeMapper.selectList(qw);
    }

    /**
     * 根据ID查询
     */
    public HotelRoomType queryById(String id) {
        return hotelRoomTypeMapper.selectById(id);
    }

    /**
     * 保存（新增/编辑）
     */
    @Transactional(rollbackFor = Exception.class)
    public String saveRoomType(HotelRoomType hotelRoomType) {
        if (StringUtils.isBlank(hotelRoomType.getId())) {
            hotelRoomType.setId(UUID.randomUUID().toString().replace("-", ""));
            hotelRoomType.setDelFlag(0);
            if (StringUtils.isBlank(hotelRoomType.getStatus())) {
                hotelRoomType.setStatus("ENABLED");
            }
            if (hotelRoomType.getSortOrder() == null) {
                hotelRoomType.setSortOrder(0);
            }
            hotelRoomType.setCreateTime(new Date());
            hotelRoomType.setUpdateTime(new Date());
            hotelRoomTypeMapper.insert(hotelRoomType);
        } else {
            hotelRoomType.setUpdateTime(new Date());
            hotelRoomTypeMapper.updateById(hotelRoomType);
        }
        return "操作成功";
    }

    /**
     * 切换状态（启用/禁用）
     */
    @Transactional(rollbackFor = Exception.class)
    public String toggleStatus(String id) {
        HotelRoomType roomType = hotelRoomTypeMapper.selectById(id);
        if (roomType == null) {
            throw new RuntimeException("房间类型不存在");
        }
        String newStatus = "ENABLED".equals(roomType.getStatus()) ? "DISABLED" : "ENABLED";
        roomType.setStatus(newStatus);
        roomType.setUpdateTime(new Date());
        hotelRoomTypeMapper.updateById(roomType);
        return "操作成功";
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoomType(String id) {
        hotelRoomTypeMapper.deleteById(id);
    }
}
