package com.jojubanking.boot.module.system.service.orderview;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.orderview.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.orderview.OrderViewDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.orderview.OrderViewConvert;
import com.jojubanking.boot.module.system.dal.mysql.orderview.OrderViewMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * VIEW Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class OrderViewServiceImpl implements OrderViewService {

    @Resource
    private OrderViewMapper orderViewMapper;

    @Override
    public Long createOrderView(OrderViewCreateReqVO createReqVO) {
        // 插入
        OrderViewDO orderView = OrderViewConvert.INSTANCE.convert(createReqVO);
        orderViewMapper.insert(orderView);
        // 返回
        return orderView.getId();
    }

    @Override
    public void updateOrderView(OrderViewUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateOrderViewExists(updateReqVO.getId());
        // 更新
        OrderViewDO updateObj = OrderViewConvert.INSTANCE.convert(updateReqVO);
        orderViewMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderView(Long id) {
        // 校验存在
        this.validateOrderViewExists(id);
        // 删除
        orderViewMapper.deleteById(id);
    }

    private void validateOrderViewExists(Long id) {
        if (orderViewMapper.selectById(id) == null) {
            throw exception(ORDER_VIEW_NOT_EXISTS);
        }
    }

    @Override
    public OrderViewDO getOrderView(Long id) {
        return orderViewMapper.selectById(id);
    }

    @Override
    public List<OrderViewDO> getOrderViewList(Collection<Long> ids) {
        return orderViewMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OrderViewDO> getOrderViewPage(OrderViewPageReqVO pageReqVO) {
        return orderViewMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrderViewDO> getOrderViewList(OrderViewExportReqVO exportReqVO) {
        return orderViewMapper.selectList(exportReqVO);
    }

}
