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.FoodMealMenuTemplate;
import com.jeesharp.business.modules.food.mapper.FoodMealMenuTemplateMapper;
import com.jeesharp.business.modules.food.service.FoodMealMenuTemplateService;
import com.jeesharp.business.modules.food.vo.FoodMealMenuTemplateQueryVO;
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 FoodMealMenuTemplateServiceImpl extends CrudService<FoodMealMenuTemplateMapper, FoodMealMenuTemplate, FoodMealMenuTemplateQueryVO> implements FoodMealMenuTemplateService {

    private static final int DEFAULT_SORT_NO = 0;
    private static final int STATUS_ENABLED = 1;
    private static final String TEMPLATE_NOT_FOUND_MESSAGE = "菜单模板不存在";

    @Override
    @Transactional(readOnly = false)
    public void createTemplate(FoodMealMenuTemplate 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 updateTemplate(FoodMealMenuTemplate entity) {
        if (StrUtil.isBlank(entity.getId()) || get(entity.getId()) == null) {
            throw new BizErrorException(TEMPLATE_NOT_FOUND_MESSAGE);
        }
        if (entity.getSortNo() == null) {
            entity.setSortNo(DEFAULT_SORT_NO);
        }
        update(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateStatus(String id, Integer status) {
        if (StrUtil.isBlank(id) || get(id) == null) {
            throw new BizErrorException(TEMPLATE_NOT_FOUND_MESSAGE);
        }
        FoodMealMenuTemplate entity = new FoodMealMenuTemplate();
        entity.setId(id);
        entity.setStatus(status);
        update(entity);
    }

    @Override
    public FoodMealMenuTemplate getEnabled(String id) {
        FoodMealMenuTemplate template = get(id);
        if (template == null || template.getStatus() == null || template.getStatus() != STATUS_ENABLED) {
            throw new BizErrorException(TEMPLATE_NOT_FOUND_MESSAGE + "或已禁用");
        }
        return template;
    }

    @Override
    public Map<String, String> mapTemplateNameByIds(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<FoodMealMenuTemplate> templates = getBaseMapper().selectByIds(distinctIds);
        if (templates == null) {
            return map;
        }
        for (FoodMealMenuTemplate template : templates) {
            if (template != null && StrUtil.isNotBlank(template.getId())) {
                map.put(template.getId(), template.getTemplateName());
            }
        }
        return map;
    }
}
