package com.jeeplus.hotelorder.dish.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.hotelorder.dish.domain.HotelDishCategory;
import com.jeeplus.hotelorder.dish.mapper.HotelDishCategoryMapper;
import com.jeeplus.hotelorder.dish.service.dto.HotelDishCategoryDTO;
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;

@Service
@Transactional(rollbackFor = Exception.class)
public class HotelDishCategoryService extends ServiceImpl<HotelDishCategoryMapper, HotelDishCategory> {

    @Resource
    private HotelDishCategoryMapper hotelDishCategoryMapper;

    /**
     * 分页查询分类(带菜品数量)
     */
    public IPage<HotelDishCategoryDTO> findPage(Page<HotelDishCategory> page) {
        IPage<HotelDishCategory> result = hotelDishCategoryMapper.selectCategoryWithCount(page);
        IPage<HotelDishCategoryDTO> dtoPage = new Page<>(result.getCurrent(), result.getSize(), result.getTotal());
        List<HotelDishCategoryDTO> dtoList = result.getRecords().stream().map(entity -> {
            HotelDishCategoryDTO dto = new HotelDishCategoryDTO();
            dto.setId(entity.getId());
            dto.setName(entity.getName());
            dto.setIcon(entity.getIcon());
            dto.setSortOrder(entity.getSortOrder());
            dto.setStatus(entity.getStatus());
            dto.setDishCount(entity.getDishCount());
            dto.setCreateTime(entity.getCreateTime());
            dto.setUpdateTime(entity.getUpdateTime());
            return dto;
        }).collect(java.util.stream.Collectors.toList());
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }

    /**
     * 查询所有启用的分类
     */
    public List<HotelDishCategoryDTO> findAll() {
        QueryWrapper<HotelDishCategory> qw = new QueryWrapper<>();
        qw.eq("status", "ENABLED").eq("del_flag", 0).orderByAsc("sort_order");
        List<HotelDishCategory> list = hotelDishCategoryMapper.selectList(qw);
        return list.stream().map(entity -> {
            HotelDishCategoryDTO dto = new HotelDishCategoryDTO();
            dto.setId(entity.getId());
            dto.setName(entity.getName());
            dto.setIcon(entity.getIcon());
            dto.setSortOrder(entity.getSortOrder());
            dto.setStatus(entity.getStatus());
            return dto;
        }).collect(java.util.stream.Collectors.toList());
    }

    /**
     * 新增/编辑分类
     */
    public String save(HotelDishCategoryDTO dto) {
        if (StringUtils.isBlank(dto.getId())) {
            HotelDishCategory entity = new HotelDishCategory();
            entity.setId(UUID.randomUUID().toString().replace("-", ""));
            entity.setName(dto.getName());
            entity.setIcon(dto.getIcon());
            entity.setSortOrder(dto.getSortOrder() != null ? dto.getSortOrder() : 0);
            entity.setStatus("ENABLED");
            entity.setDelFlag(0);
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
            hotelDishCategoryMapper.insert(entity);
        } else {
            HotelDishCategory entity = hotelDishCategoryMapper.selectById(dto.getId());
            if (entity == null) {
                throw new RuntimeException("分类不存在");
            }
            entity.setName(dto.getName());
            entity.setIcon(dto.getIcon());
            entity.setSortOrder(dto.getSortOrder());
            entity.setUpdateTime(new Date());
            hotelDishCategoryMapper.updateById(entity);
        }
        return "操作成功";
    }

    /**
     * 切换分类状态(启用/禁用)
     */
    public String toggleStatus(String id) {
        HotelDishCategory entity = hotelDishCategoryMapper.selectById(id);
        if (entity == null) {
            throw new RuntimeException("分类不存在");
        }
        entity.setStatus("ENABLED".equals(entity.getStatus()) ? "DISABLED" : "ENABLED");
        entity.setUpdateTime(new Date());
        hotelDishCategoryMapper.updateById(entity);
        return "操作成功";
    }

    /**
     * 删除分类
     */
    public String delete(String id) {
        HotelDishCategory entity = hotelDishCategoryMapper.selectById(id);
        if (entity == null) {
            throw new RuntimeException("分类不存在");
        }
        entity.setDelFlag(1);
        entity.setUpdateTime(new Date());
        hotelDishCategoryMapper.updateById(entity);
        return "操作成功";
    }
}
