package co.yixiang.yshop.module.express.service.electronicsorder;

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

import java.util.*;
import co.yixiang.yshop.module.express.controller.admin.electronicsorder.vo.*;
import co.yixiang.yshop.module.express.dal.dataobject.electronicsorder.ElectronicsOrderDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.express.convert.electronicsorder.ElectronicsOrderConvert;
import co.yixiang.yshop.module.express.dal.mysql.electronicsorder.ElectronicsOrderMapper;

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

/**
 * 电子面单 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class ElectronicsOrderServiceImpl implements ElectronicsOrderService {

    @Resource
    private ElectronicsOrderMapper electronicsOrderMapper;

    @Override
    public Long createElectronicsOrder(ElectronicsOrderCreateReqVO createReqVO) {
        // 插入
        ElectronicsOrderDO electronicsOrder = ElectronicsOrderConvert.INSTANCE.convert(createReqVO);
        electronicsOrderMapper.insert(electronicsOrder);
        // 返回
        return electronicsOrder.getId();
    }

    @Override
    public void updateElectronicsOrder(ElectronicsOrderUpdateReqVO updateReqVO) {
        // 校验存在
        validateElectronicsOrderExists(updateReqVO.getId());
        // 更新
        ElectronicsOrderDO updateObj = ElectronicsOrderConvert.INSTANCE.convert(updateReqVO);
        electronicsOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteElectronicsOrder(Long id) {
        // 校验存在
        validateElectronicsOrderExists(id);
        // 删除
        electronicsOrderMapper.deleteById(id);
    }

    private void validateElectronicsOrderExists(Long id) {
        if (electronicsOrderMapper.selectById(id) == null) {
            throw exception(ELECTRONICS_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public ElectronicsOrderDO getElectronicsOrder(Long id) {
        return electronicsOrderMapper.selectById(id);
    }

    @Override
    public List<ElectronicsOrderDO> getElectronicsOrderList() {
        return electronicsOrderMapper.selectList();
    }

    @Override
    public PageResult<ElectronicsOrderDO> getElectronicsOrderPage(ElectronicsOrderPageReqVO pageReqVO) {
        return electronicsOrderMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ElectronicsOrderDO> getElectronicsOrderList(ElectronicsOrderExportReqVO exportReqVO) {
        return electronicsOrderMapper.selectList(exportReqVO);
    }

}
