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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.food.entity.FoodMealServiceArea;
import com.jeesharp.business.modules.food.mapper.FoodMealServiceAreaMapper;
import com.jeesharp.business.modules.food.service.FoodMealServiceAreaService;
import com.jeesharp.business.modules.food.vo.FoodMealServiceAreaQueryVO;
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.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class FoodMealServiceAreaServiceImpl extends CrudService<FoodMealServiceAreaMapper, FoodMealServiceArea, FoodMealServiceAreaQueryVO> implements FoodMealServiceAreaService {

    private static final int DEFAULT_SORT_NO = 0;
    private static final int STATUS_ENABLED = 1;
    private static final String AREA_NOT_FOUND_MESSAGE = "服务区域不存在";

    @Override
    public List<FoodMealServiceArea> listOnline() {
        FoodMealServiceAreaQueryVO query = new FoodMealServiceAreaQueryVO();
        query.setStatus(STATUS_ENABLED);
        query.closeDataScope();
        return findList(query);
    }

    @Override
    public FoodMealServiceArea getOnline(String id) {
        if (StrUtil.isBlank(id)) {
            throw new BizErrorException(AREA_NOT_FOUND_MESSAGE + "或已禁用");
        }
        FoodMealServiceAreaQueryVO query = new FoodMealServiceAreaQueryVO();
        query.setId(id);
        query.setStatus(STATUS_ENABLED);
        query.closeDataScope();
        List<FoodMealServiceArea> list = findList(query);
        if (list.isEmpty()) {
            throw new BizErrorException(AREA_NOT_FOUND_MESSAGE + "或已禁用");
        }
        return list.get(0);
    }

    @Override
    @Transactional(readOnly = false)
    public void createArea(FoodMealServiceArea entity) {
        if (entity.getStatus() == null) {
            entity.setStatus(STATUS_ENABLED);
        }
        if (entity.getSortNo() == null) {
            entity.setSortNo(DEFAULT_SORT_NO);
        }
        save(entity);
    }

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

    @Override
    public Map<String, String> mapAreaNameByIds(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<FoodMealServiceArea> areas = getBaseMapper().selectByIds(distinctIds);
        if (areas == null) {
            return map;
        }
        for (FoodMealServiceArea area : areas) {
            if (area != null && StrUtil.isNotBlank(area.getId())) {
                map.put(area.getId(), area.getAreaName());
            }
        }
        return map;
    }

    @Override
    public List<String> findIdsByNameLike(String name) {
        if (StrUtil.isBlank(name)) {
            return Collections.emptyList();
        }
        return getBaseMapper().findIdsByNameLike(name);
    }
}
