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

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.wxstoreorderdetail.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.wxstoreorderdetail.WxStoreOrderDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.wxstoreorderdetail.WxStoreOrderDetailMapper;

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

/**
 * 微信付款码订单详情 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class WxStoreOrderDetailServiceImpl implements WxStoreOrderDetailService {

    @Resource
    private WxStoreOrderDetailMapper wxStoreOrderDetailMapper;

    @Override
    public Long createWxStoreOrderDetail(WxStoreOrderDetailSaveReqVO createReqVO) {
        // 插入
        WxStoreOrderDetailDO wxStoreOrderDetail = BeanUtils.toBean(createReqVO, WxStoreOrderDetailDO.class);
        wxStoreOrderDetailMapper.insert(wxStoreOrderDetail);
        // 返回
        return wxStoreOrderDetail.getId();
    }

    @Override
    public void updateWxStoreOrderDetail(WxStoreOrderDetailSaveReqVO updateReqVO) {
        // 校验存在
        validateWxStoreOrderDetailExists(updateReqVO.getId());
        // 更新
        WxStoreOrderDetailDO updateObj = BeanUtils.toBean(updateReqVO, WxStoreOrderDetailDO.class);
        wxStoreOrderDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteWxStoreOrderDetail(Long id) {
        // 校验存在
        validateWxStoreOrderDetailExists(id);
        // 删除
        wxStoreOrderDetailMapper.deleteById(id);
    }

    private void validateWxStoreOrderDetailExists(Long id) {
        if (wxStoreOrderDetailMapper.selectById(id) == null) {
            throw exception(WX_STORE_ORDER_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public WxStoreOrderDetailDO getWxStoreOrderDetail(Long id) {
        return wxStoreOrderDetailMapper.selectById(id);
    }

    @Override
    public List<WxStoreOrderDetailDO> getWxStoreOrderDetailByOrderId(Long orderId) {
        return wxStoreOrderDetailMapper.selectList(new LambdaQueryWrapperX<WxStoreOrderDetailDO>().eq(WxStoreOrderDetailDO::getWxOrderId, orderId));
    }

    @Override
    public PageResult<WxStoreOrderDetailDO> getWxStoreOrderDetailPage(WxStoreOrderDetailPageReqVO pageReqVO) {
        return wxStoreOrderDetailMapper.selectPage(pageReqVO);
    }

}