package cn.iocoder.yudao.module.member.service.storeorderdetail;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.DishVO;
import cn.iocoder.yudao.module.member.controller.admin.screen.vo.GoodsVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorderdetail.vo.StoreOrderDetailPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storeorderdetail.vo.StoreOrderDetailSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.storeorder.vo.AppStoreOrderDetailVo;
import cn.iocoder.yudao.module.member.dal.dataobject.storeorderdetail.StoreOrderDetailDO;
import cn.iocoder.yudao.module.member.dal.mysql.storeorderdetail.StoreOrderDetailMapper;
import cn.iocoder.yudao.module.member.util.MemberTimeUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.STORE_ORDER_DETAIL_NOT_EXISTS;

/**
 * 商品订单详情 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class StoreOrderDetailServiceImpl implements StoreOrderDetailService {

    @Resource
    private StoreOrderDetailMapper storeOrderDetailMapper;

    @Override
    public Integer createStoreOrderDetail(StoreOrderDetailSaveReqVO createReqVO) {
        // 插入
        StoreOrderDetailDO storeOrderDetail = BeanUtils.toBean(createReqVO, StoreOrderDetailDO.class);
        storeOrderDetailMapper.insert(storeOrderDetail);
        // 返回
        return storeOrderDetail.getId();
    }

    @Override
    public void updateStoreOrderDetail(StoreOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateStoreOrderDetailExists(updateReqVO.getId());
        // 更新
        StoreOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, StoreOrderDetailDO.class);
        storeOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreOrderDetail(Integer id) {
        // 校验存在
        validateStoreOrderDetailExists(id);
        // 删除
        storeOrderDetailMapper.deleteById(id);
    }

    private void validateStoreOrderDetailExists(Integer id) {
        if (storeOrderDetailMapper.selectById(id) == null) {
            throw exception(STORE_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public StoreOrderDetailDO getStoreOrderDetail(Integer id) {
        return storeOrderDetailMapper.selectById(id);
    }

    @Override
    public PageResult<StoreOrderDetailDO> getStoreOrderDetailPage(StoreOrderDetailPageReqVO pageReqVO) {
        return storeOrderDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<StoreOrderDetailDO> list) {
        storeOrderDetailMapper.insertBatch(list);
    }

    @Override
    public List<AppStoreOrderDetailVo> getListByOrderNo(Integer orderId) {
        return storeOrderDetailMapper.selectByOrderId(orderId);
    }

    @Override
    public void deleteByOrderNo(Integer orderId) {
        storeOrderDetailMapper.delete(Wrappers.<StoreOrderDetailDO>lambdaQuery().eq(StoreOrderDetailDO::getOrderId,orderId));
    }

    @Override
    public List<StoreOrderDetailDO> getListByOrderId(Integer orderId) {
        return storeOrderDetailMapper.selectList(Wrappers.<StoreOrderDetailDO>lambdaQuery().eq(StoreOrderDetailDO::getOrderId,orderId));
    }

    @Override
    public List<GoodsVO> selectGoodsSale(Long storeId, String startTime, String endTime,Integer type) {
        LocalDateTime start = MemberTimeUtils.string2Time(startTime);
        LocalDateTime end = MemberTimeUtils.string2Time(endTime);
        return storeOrderDetailMapper.selectGoodsSale(storeId, start, end,type);
    }
}