package com.jeesharp.business.modules.food.application;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.food.convert.FoodRestaurantConvert;
import com.jeesharp.business.modules.food.dto.FoodRestaurantSaveDTO;
import com.jeesharp.business.modules.food.entity.FoodRestaurant;
import com.jeesharp.business.modules.food.service.FoodRestaurantService;
import com.jeesharp.business.modules.food.vo.FoodRestaurantInfoVO;
import com.jeesharp.business.modules.food.vo.FoodRestaurantListVO;
import com.jeesharp.business.modules.food.vo.FoodRestaurantQueryVO;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@AllArgsConstructor
@Transactional(readOnly = true)
public class FoodRestaurantApplicationService {

    private final FoodRestaurantService foodRestaurantService;

    /**
     * 按主键查询门店详情。
     */
    public FoodRestaurantInfoVO get(String id) {
        return FoodRestaurantConvert.INSTANCE.toInfoVO(foodRestaurantService.get(id));
    }

    public FoodRestaurantInfoVO getOnline(String id) {
        return FoodRestaurantConvert.INSTANCE.toInfoVO(foodRestaurantService.getOnline(id));
    }

    public PageRespVO<FoodRestaurantListVO> findPage(Page<FoodRestaurant> page, FoodRestaurantQueryVO query) {
        Page<FoodRestaurant> result = foodRestaurantService.findPage(page, query);
        return FoodRestaurantConvert.INSTANCE.toPageRespVO(result);
    }

    public List<FoodRestaurantInfoVO> listOnline() {
        return foodRestaurantService.listOnline().stream()
                .map(FoodRestaurantConvert.INSTANCE::toInfoVO)
                .toList();
    }

    @Transactional(readOnly = false)
    public void save(FoodRestaurantSaveDTO dto) {
        FoodRestaurant entity = FoodRestaurantConvert.INSTANCE.toEntity(dto);
        if (StrUtil.isBlank(dto.getId())) {
            foodRestaurantService.createRestaurant(entity);
            return;
        }
        foodRestaurantService.updateRestaurant(entity);
    }

    @Transactional(readOnly = false)
    public void delete(String ids) {
        foodRestaurantService.batchDelete(StrUtil.splitTrim(ids, ','));
    }
}
