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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.food.entity.FoodMealMenuItem;
import com.jeesharp.business.modules.food.entity.FoodMealOrderItem;
import com.jeesharp.business.modules.food.mapper.FoodMealMenuItemMapper;
import com.jeesharp.business.modules.food.service.FoodMealMenuItemService;
import com.jeesharp.business.modules.food.vo.FoodMealMenuItemQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.utils.WebFrameworkUtils;
import com.jeesharp.framework.exception.BizErrorException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class FoodMealMenuItemServiceImpl extends CrudService<FoodMealMenuItemMapper, FoodMealMenuItem, FoodMealMenuItemQueryVO> implements FoodMealMenuItemService {

    private static final String DEL_NORMAL = "0";

    @Override
    public List<FoodMealMenuItem> listByMenuId(String menuId) {
        FoodMealMenuItemQueryVO query = new FoodMealMenuItemQueryVO();
        query.setMenuId(menuId);
        return findList(query);
    }

    @Override
    public List<FoodMealMenuItem> listByMenuIdAndIds(String menuId, Collection<String> ids, boolean forUpdate) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<FoodMealMenuItem> wrapper = new LambdaQueryWrapper<FoodMealMenuItem>()
            .eq(FoodMealMenuItem::getMenuId, menuId)
            .in(FoodMealMenuItem::getId, ids)
            .eq(FoodMealMenuItem::getDelFlag, DEL_NORMAL);
        if (forUpdate) {
            wrapper.last("FOR UPDATE");
        }
        return getBaseMapper().selectList(wrapper);
    }

    @Override
    @Transactional(readOnly = false)
    public int increaseSoldQuantity(String id, Integer quantity, String tenantCode) {
        return getBaseMapper().increaseSoldQuantity(id, quantity, tenantCode);
    }

    @Override
    @Transactional(readOnly = false)
    public int decreaseSoldQuantity(String id, Integer quantity, String tenantCode) {
        return getBaseMapper().decreaseSoldQuantity(id, quantity, tenantCode);
    }

    @Override
    @Transactional(readOnly = false)
    public void increaseSoldQuantity(Map<String, Integer> quantityMap, String tenantCode) {
        for (Map.Entry<String, Integer> entry : quantityMap.entrySet()) {
            int updated = increaseSoldQuantity(entry.getKey(), entry.getValue(), tenantCode);
            if (updated != 1) {
                throw new BizErrorException("菜单库存不足");
            }
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void releaseSoldQuantity(Collection<FoodMealOrderItem> items, String tenantCode) {
        for (FoodMealOrderItem item : items) {
            int updated = decreaseSoldQuantity(item.getMenuItemId(), item.getQuantity(), tenantCode);
            if (updated != 1) {
                throw new BizErrorException("库存释放失败");
            }
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void updatePublishStatus(String id, String publishStatus) {
        int updated = getBaseMapper().updatePublishStatus(id, publishStatus, WebFrameworkUtils.getLoginUserTenantCode());
        if (updated != 1) {
            throw new BizErrorException("菜单明细不存在");
        }
    }

    @Override
    @Transactional(readOnly = false)
    public void deleteByMenuId(String menuId) {
        getBaseMapper().deleteByMenuId(menuId);
    }
}
