package com.jeesharp.business.modules.food.service.impl;

import com.jeesharp.business.modules.common.enums.ShelfStatusEnum;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.food.entity.FoodRestaurant;

import com.jeesharp.business.modules.food.mapper.FoodRestaurantMapper;
import com.jeesharp.business.modules.food.service.FoodRestaurantService;
import com.jeesharp.business.modules.food.vo.FoodRestaurantQueryVO;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 美食门店领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-04
 */
@Service
@Transactional(readOnly = true)
public class FoodRestaurantServiceImpl extends CrudService<FoodRestaurantMapper, FoodRestaurant, FoodRestaurantQueryVO> implements FoodRestaurantService {

    private static final int DEFAULT_SORT_NO = 0;
    private static final String IDS_SEPARATOR = ",";
    private static final String RESTAURANT_NOT_FOUND_MESSAGE = "门店不存在";

    @Override
    public List<FoodRestaurant> listOnline() {
        FoodRestaurantQueryVO query = new FoodRestaurantQueryVO();
        query.setStatus(ShelfStatusEnum.ONLINE.getValue());
        query.closeDataScope();
        return findList(query);
    }

    @Override
    public FoodRestaurant getOnline(String id) {
        FoodRestaurantQueryVO query = new FoodRestaurantQueryVO();
        query.setId(id);
        query.setStatus(ShelfStatusEnum.ONLINE.getValue());
        query.closeDataScope();
        List<FoodRestaurant> list = findList(query);
        if (list.isEmpty()) {
            throw new BizErrorException(RESTAURANT_NOT_FOUND_MESSAGE + "或已下线");
        }
        return list.get(0);
    }

    @Override
    public List<String> findIdsByNameLike(String name) {
        if (StrUtil.isBlank(name)) {
            return List.of();
        }
        FoodRestaurantQueryVO query = new FoodRestaurantQueryVO();
        query.setName(name.trim());
        return findList(query).stream()
            .map(FoodRestaurant::getId)
            .filter(StrUtil::isNotBlank)
            .distinct()
            .collect(Collectors.toList());
    }

    @Override
    public Map<String, String> mapRestaurantNameByIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyMap();
        }
        List<String> distinctIds = ids.stream().filter(StrUtil::isNotBlank).distinct().toList();
        if (distinctIds.isEmpty()) {
            return Collections.emptyMap();
        }
        Map<String, String> map = new HashMap<>();
        List<FoodRestaurant> restaurants = getBaseMapper().selectByIds(distinctIds);
        if (restaurants == null) {
            return map;
        }
        for (FoodRestaurant restaurant : restaurants) {
            if (restaurant != null && StrUtil.isNotBlank(restaurant.getId())) {
                map.put(restaurant.getId(), restaurant.getName());
            }
        }
        return map;
    }

    @Override
    @Transactional(readOnly = false)
    public void createRestaurant(FoodRestaurant entity) {
        if (entity.getSortNo() == null) {
            entity.setSortNo(DEFAULT_SORT_NO);
        }
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateRestaurant(FoodRestaurant entity) {
        if (entity.getId() == null || entity.getId().isBlank() || get(entity.getId()) == null) {
            throw new BizErrorException(RESTAURANT_NOT_FOUND_MESSAGE);
        }
        if (entity.getSortNo() == null) {
            entity.setSortNo(DEFAULT_SORT_NO);
        }
        update(entity);
    }

}
