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.FoodMealOrderItem;
import com.jeesharp.business.modules.food.mapper.FoodMealOrderItemMapper;
import com.jeesharp.business.modules.food.service.FoodMealOrderItemService;
import com.jeesharp.business.modules.food.vo.FoodMealHotItemQueryVO;
import com.jeesharp.business.modules.food.vo.FoodMealHotItemVO;
import com.jeesharp.business.modules.food.vo.FoodMealOrderItemQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class FoodMealOrderItemServiceImpl extends CrudService<FoodMealOrderItemMapper, FoodMealOrderItem, FoodMealOrderItemQueryVO> implements FoodMealOrderItemService {

    private static final String DEL_NORMAL = "0";
    private static final BigDecimal ZERO = BigDecimal.ZERO.setScale(2, RoundingMode.HALF_UP);

    @Override
    public List<FoodMealOrderItem> listByOrderId(String orderId) {
        return getBaseMapper().selectList(new LambdaQueryWrapper<FoodMealOrderItem>()
            .eq(FoodMealOrderItem::getOrderId, orderId)
            .eq(FoodMealOrderItem::getDelFlag, DEL_NORMAL)
            .orderByAsc(FoodMealOrderItem::getCreateDate));
    }

    @Override
    public List<FoodMealOrderItem> listByOrderIdsAndMenuItemId(Collection<String> orderIds, String menuItemId) {
        if (CollUtil.isEmpty(orderIds)) {
            return Collections.emptyList();
        }
        return getBaseMapper().selectList(new LambdaQueryWrapper<FoodMealOrderItem>()
            .in(FoodMealOrderItem::getOrderId, orderIds)
            .eq(FoodMealOrderItem::getMenuItemId, menuItemId)
            .eq(FoodMealOrderItem::getDelFlag, DEL_NORMAL));
    }

    @Override
    @Transactional(readOnly = false)
    public void insertOrderItem(FoodMealOrderItem orderItem) {
        orderItem.setIsNewRecord(true);
        orderItem.preInsert();
        getBaseMapper().insert(orderItem);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateOrderItem(FoodMealOrderItem orderItem) {
        orderItem.preUpdate();
        getBaseMapper().updateById(orderItem);
    }

    @Override
    @Transactional(readOnly = false)
    public void markRefunded(List<FoodMealOrderItem> items, boolean includeCompanyPayAmount) {
        for (FoodMealOrderItem item : items) {
            item.setRefundQuantity(item.getQuantity());
            BigDecimal refundAmount = money(item.getUserPayAmount());
            if (includeCompanyPayAmount) {
                refundAmount = refundAmount.add(money(item.getCompanyPayAmount()));
            }
            item.setRefundAmount(refundAmount);
            updateOrderItem(item);
        }
    }

    @Override
    public List<FoodMealHotItemVO> listHotItems(FoodMealHotItemQueryVO query, String tenantCode) {
        Integer limit = query == null ? null : query.getLimit();
        return getBaseMapper().selectHotItems(query, tenantCode, limit);
    }

    private BigDecimal money(BigDecimal amount) {
        if (amount == null) {
            return ZERO;
        }
        return amount.setScale(2, RoundingMode.HALF_UP);
    }
}
