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.core.query.QueryWrapperGenerator;
import com.jeeplus.hotelorder.dish.domain.HotelDishSpecOption;
import com.jeeplus.hotelorder.dish.mapper.HotelDishSpecOptionMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

/**
 * 菜品规格选项服务类
 * @author jeeplus
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HotelDishSpecOptionService extends ServiceImpl<HotelDishSpecOptionMapper, HotelDishSpecOption> {

    private static final Logger log = LoggerFactory.getLogger(HotelDishSpecOptionService.class);

    @Resource
    private HotelDishSpecOptionMapper hotelDishSpecOptionMapper;

    /**
     * 分页查询规格选项
     * @param page 分页参数
     * @param specOption 查询条件
     * @return 分页结果
     * @throws Exception 查询异常
     */
    public IPage<HotelDishSpecOption> findPage(Page<HotelDishSpecOption> page, HotelDishSpecOption specOption) throws Exception {
        QueryWrapper<HotelDishSpecOption> queryWrapper = QueryWrapperGenerator.buildQueryCondition(specOption, HotelDishSpecOption.class);
        queryWrapper.eq("del_flag", 0);
        queryWrapper.orderByAsc("sort_order");
        return hotelDishSpecOptionMapper.selectPage(page, queryWrapper);
    }

    /**
     * 根据规格组ID查询所有选项
     * @param groupId 规格组ID
     * @return 选项列表
     */
    public List<HotelDishSpecOption> findByGroupId(String groupId) {
        QueryWrapper<HotelDishSpecOption> qw = new QueryWrapper<>();
        qw.eq("group_id", groupId).eq("del_flag", 0).orderByAsc("sort_order");
        return hotelDishSpecOptionMapper.selectList(qw);
    }

    /**
     * 根据ID查询规格选项
     * @param id 选项ID
     * @return 选项信息
     */
    public HotelDishSpecOption queryById(String id) {
        return hotelDishSpecOptionMapper.selectById(id);
    }

    /**
     * 保存规格选项（新增/编辑）
     * @param specOption 选项信息
     * @return 操作结果
     */
    public String saveSpecOption(HotelDishSpecOption specOption) {
        if (StringUtils.isBlank(specOption.getId())) {
            specOption.setId(UUID.randomUUID().toString().replace("-", ""));
            specOption.setDelFlag(0);
            if (specOption.getPriceExtra() == null) {
                specOption.setPriceExtra(BigDecimal.ZERO);
            }
            if (specOption.getSortOrder() == null) {
                specOption.setSortOrder(0);
            }
            hotelDishSpecOptionMapper.insert(specOption);
            log.info("新增规格选项: {}", specOption.getOptionName());
        } else {
            hotelDishSpecOptionMapper.updateById(specOption);
            log.info("编辑规格选项: {}", specOption.getOptionName());
        }
        return "操作成功";
    }

    /**
     * 根据规格组ID删除所有选项
     * @param groupId 规格组ID
     */
    public void deleteByGroupId(String groupId) {
        QueryWrapper<HotelDishSpecOption> qw = new QueryWrapper<>();
        qw.eq("group_id", groupId);
        hotelDishSpecOptionMapper.delete(qw);
        log.info("删除规格组下所有选项: groupId={}", groupId);
    }

    /**
     * 删除规格选项
     * @param id 选项ID
     */
    public void deleteSpecOption(String id) {
        hotelDishSpecOptionMapper.deleteById(id);
        log.info("删除规格选项: {}", id);
    }
}
