package co.yixiang.yshop.module.order.service.storeorderelectronics;

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

import java.util.*;
import co.yixiang.yshop.module.order.controller.admin.storeorderelectronics.vo.*;
import co.yixiang.yshop.module.order.dal.dataobject.storeorderelectronics.StoreOrderElectronicsDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.order.convert.storeorderelectronics.StoreOrderElectronicsConvert;
import co.yixiang.yshop.module.order.dal.mysql.storeorderelectronics.StoreOrderElectronicsMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.order.enums.ErrorCodeConstants.*;

/**
 * 订单电子面单记录 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class StoreOrderElectronicsServiceImpl implements StoreOrderElectronicsService {

    @Resource
    private StoreOrderElectronicsMapper storeOrderElectronicsMapper;

    @Override
    public Long createStoreOrderElectronics(StoreOrderElectronicsCreateReqVO createReqVO) {
        // 插入
        StoreOrderElectronicsDO storeOrderElectronics = StoreOrderElectronicsConvert.INSTANCE.convert(createReqVO);
        storeOrderElectronicsMapper.insert(storeOrderElectronics);
        // 返回
        return storeOrderElectronics.getId();
    }

    @Override
    public void updateStoreOrderElectronics(StoreOrderElectronicsUpdateReqVO updateReqVO) {
        // 校验存在
        validateStoreOrderElectronicsExists(updateReqVO.getId());
        // 更新
        StoreOrderElectronicsDO updateObj = StoreOrderElectronicsConvert.INSTANCE.convert(updateReqVO);
        storeOrderElectronicsMapper.updateById(updateObj);
    }

    @Override
    public void deleteStoreOrderElectronics(Long id) {
        // 校验存在
        validateStoreOrderElectronicsExists(id);
        // 删除
        storeOrderElectronicsMapper.deleteById(id);
    }

    private void validateStoreOrderElectronicsExists(Long id) {
        if (storeOrderElectronicsMapper.selectById(id) == null) {
            throw exception(STORE_ORDER_ELECTRONICS_NOT_EXISTS);
        }
    }

    @Override
    public StoreOrderElectronicsDO getStoreOrderElectronics(Long id) {
        return storeOrderElectronicsMapper.selectById(id);
    }

    @Override
    public List<StoreOrderElectronicsDO> getStoreOrderElectronicsList(Collection<Long> ids) {
        return storeOrderElectronicsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StoreOrderElectronicsDO> getStoreOrderElectronicsPage(StoreOrderElectronicsPageReqVO pageReqVO) {
        return storeOrderElectronicsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StoreOrderElectronicsDO> getStoreOrderElectronicsList(StoreOrderElectronicsExportReqVO exportReqVO) {
        return storeOrderElectronicsMapper.selectList(exportReqVO);
    }

}
