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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jeesharp.business.modules.food.entity.FoodMealMenuItemSpec;
import com.jeesharp.business.modules.food.entity.FoodMealOrderItem;
import com.jeesharp.business.modules.food.mapper.FoodMealMenuItemSpecMapper;
import com.jeesharp.business.modules.food.service.FoodMealMenuItemSpecService;
import com.jeesharp.business.modules.food.vo.FoodMealMenuItemSpecQueryVO;
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.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true)
public class FoodMealMenuItemSpecServiceImpl extends CrudService<FoodMealMenuItemSpecMapper, FoodMealMenuItemSpec, FoodMealMenuItemSpecQueryVO> implements FoodMealMenuItemSpecService {

    private static final String DEL_NORMAL = "0";

    @Override
    public List<FoodMealMenuItemSpec> listByMenuItemId(String menuItemId) {
        if (StrUtil.isBlank(menuItemId)) {
            return Collections.emptyList();
        }
        FoodMealMenuItemSpecQueryVO query = new FoodMealMenuItemSpecQueryVO();
        query.setMenuItemId(menuItemId);
        return findList(query);
    }

    @Override
    public List<FoodMealMenuItemSpec> listByMenuItemIds(Collection<String> menuItemIds) {
        if (CollUtil.isEmpty(menuItemIds)) {
            return Collections.emptyList();
        }
        return getBaseMapper().selectList(new LambdaQueryWrapper<FoodMealMenuItemSpec>()
            .in(FoodMealMenuItemSpec::getMenuItemId, menuItemIds)
            .eq(FoodMealMenuItemSpec::getDelFlag, DEL_NORMAL)
            .orderByAsc(FoodMealMenuItemSpec::getSortNo, FoodMealMenuItemSpec::getCreateDate));
    }

    @Override
    public List<FoodMealMenuItemSpec> listByIds(Collection<String> ids, boolean forUpdate) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<FoodMealMenuItemSpec> wrapper = new LambdaQueryWrapper<FoodMealMenuItemSpec>()
            .in(FoodMealMenuItemSpec::getId, ids)
            .eq(FoodMealMenuItemSpec::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) {
            if (StrUtil.isBlank(item.getSpecId())) {
                continue;
            }
            int updated = decreaseSoldQuantity(item.getSpecId(), item.getQuantity(), tenantCode);
            if (updated != 1) {
                throw new BizErrorException("规格库存释放失败");
            }
        }
    }

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

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