package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.enums.OrderStatusEnums;
import cn.ysk.cashier.enums.OrderUseTypeEnum;
import cn.ysk.cashier.enums.ShopInfoEatModelEnum;
import cn.ysk.cashier.enums.ShopInfoRegisterlEnum;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.mapper.MpShopInfoMapper;
import cn.ysk.cashier.mybatis.mapper.TbCashierCartMapper;
import cn.ysk.cashier.mybatis.service.MpCashierCartService;
import cn.ysk.cashier.pojo.order.TbCashierCart;
import cn.ysk.cashier.pojo.shop.TbShopInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (TbShopPermission)表服务实现类
 *
 * @author makejava
 * @since 2024-09-14 17:08:49
 */
@Service
public class MpCashierCartServiceImpl extends ServiceImpl<TbCashierCartMapper, TbCashierCart> implements MpCashierCartService {
    private final MpShopInfoMapper mpShopInfoMapper;

    public MpCashierCartServiceImpl(MpShopInfoMapper mpShopInfoMapper) {
        this.mpShopInfoMapper = mpShopInfoMapper;
    }

    @Override
    public List<TbCashierCart> selectTakeoutCart(String masterId, Integer shopId) {
        return list(new LambdaQueryWrapper<TbCashierCart>()
                .eq(TbCashierCart::getShopId, shopId)
                .eq(TbCashierCart::getMasterId, masterId)
                .eq(TbCashierCart::getUseType, OrderUseTypeEnum.TAKEOUT.getValue())
                .gt(TbCashierCart::getCreatedAt, DateUtil.offsetDay(DateUtil.date(), -1).getTime()));
    }

    @Override
    public List<TbCashierCart> selectUnTableCart(String masterId, Integer shopId) {
        TbShopInfo shopInfo = mpShopInfoMapper.selectOne(new LambdaQueryWrapper<TbShopInfo>()
                .eq(TbShopInfo::getId, shopId)
                .eq(TbShopInfo::getStatus, 1));
        if (shopInfo == null) {
            throw new BadRequestException("店铺信息不存在");
        }

        boolean isMunchies = StrUtil.isNotBlank(shopInfo.getRegisterType()) &&
                ShopInfoRegisterlEnum.MUNCHIES.getValue().equals(shopInfo.getRegisterType());

        return list(new LambdaQueryWrapper<TbCashierCart>()
                .eq(TbCashierCart::getShopId, shopId)
                .eq(TbCashierCart::getMasterId, masterId)
                .eq(TbCashierCart::getUseType, isMunchies ? OrderUseTypeEnum.DINE_IN_BEFORE.getValue() : OrderUseTypeEnum.DINE_IN_AFTER.getValue())
                .gt(TbCashierCart::getCreatedAt, DateUtil.offsetDay(DateUtil.date(), -1).getTime()));
    }

    @Override
    public long countByTableId(Integer shopId, String tableId, OrderStatusEnums... statusEnums) {
        LambdaQueryWrapper<TbCashierCart> queryWrapper = new LambdaQueryWrapper<TbCashierCart>()
                .eq(TbCashierCart::getShopId, shopId)
                .gt(TbCashierCart::getCreatedAt, DateUtil.offsetDay(DateUtil.date(), -1).getTime())
                .eq(TbCashierCart::getTableId, tableId);
        if (statusEnums.length > 0) {
            ArrayList<String> stateList = new ArrayList<>();
            for (OrderStatusEnums statusEnum : statusEnums) {
                stateList.add(statusEnum.getValue());
            }
            queryWrapper.in(TbCashierCart::getStatus, stateList);
        }
        return count(queryWrapper);
    }

    @Override
    public boolean clearCartByTableIdAndUseType(String tableId, String useType, Integer shopId) {
        return remove(new LambdaQueryWrapper<TbCashierCart>()
                .in(TbCashierCart::getStatus, OrderStatusEnums.CREATE.getValue(), OrderStatusEnums.RETURN.getValue())
                .gt(TbCashierCart::getCreatedAt, DateUtil.offsetDay(DateUtil.date(), -1).getTime())
                .eq(TbCashierCart::getShopId, shopId)
                .eq(TbCashierCart::getTableId, tableId)
                .eq(TbCashierCart::getUseType, useType));
    }
}

