package com.cool.store.dao.order;

import com.cool.store.entity.order.StoreOrderDO;
import com.cool.store.mapper.order.StoreOrderMapper;
import com.cool.store.request.order.PCStoreOrderQueryRequest;
import com.cool.store.vo.order.PCStoreOrderListVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class StoreOrderDAO {

    private final StoreOrderMapper storeOrderMapper;

    public boolean insertSelective(StoreOrderDO order) {
        return storeOrderMapper.insertSelective(order) > 0;
    }

    public StoreOrderDO getById(Long id) {
        return storeOrderMapper.getById(id);
    }

    public Integer deleteById(StoreOrderDO storeOrderDO){
        return storeOrderMapper.delete(storeOrderDO);
    }

    public List<StoreOrderDO> queryPage(Long shopId, Integer status) {
        return storeOrderMapper.queryPage(shopId, status);
    }

    public List<StoreOrderDO> queryPageByLineId(Long lineId, Integer status) {
        return storeOrderMapper.queryPageByLineId(lineId, status);
    }

    public List<PCStoreOrderListVO> queryPCPage(PCStoreOrderQueryRequest request) {
        return storeOrderMapper.queryPCPage(request);
    }

    public Long count(PCStoreOrderQueryRequest request) {
        return storeOrderMapper.count(request);
    }

    public boolean updateSelective(StoreOrderDO order) {
        return storeOrderMapper.updateByPrimaryKeySelective(order) > 0;
    }

    public boolean existsUnfinishedOrder(Long shopId, List<Integer> statuses) {
        if (shopId == null || statuses == null || statuses.isEmpty()) {
            return false;
        }
        return storeOrderMapper.countByShopIdAndStatusIn(shopId, statuses) > 0;
    }

    public boolean existsPaidOrder(Long shopId, Integer status) {
        if (shopId == null || status == null) {
            return false;
        }
        return storeOrderMapper.countByShopIdAndStatus(shopId, status) > 0;
    }

    public StoreOrderDO getLatestByShopIdAndStatus(Long shopId, Integer status) {
        if (shopId == null || status == null) {
            return null;
        }
        return storeOrderMapper.getLatestDraftByShopId(shopId, status);
    }

    public StoreOrderDO getLatestOrderByShopId(Long shopId) {
        if (shopId == null ) {
            return null;
        }
        return storeOrderMapper.getLatestOrderByShopId(shopId);
    }
}
