package cn.newfeifan.mall.module.distri.service.partitionbrothers;

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.newfeifan.mall.module.distri.controller.admin.partitionbrothers.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.partitionbrothers.PartitionBrothersDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.partitionbrothers.PartitionBrothersMapper;

import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;

/**
 * 兄弟分区 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
public class PartitionBrothersServiceImpl implements PartitionBrothersService {

    @Resource
    private PartitionBrothersMapper partitionBrothersMapper;

    @Override
    public Long createPartitionBrothers(PartitionBrothersSaveReqVO createReqVO) {
        // 插入
        PartitionBrothersDO partitionBrothers = BeanUtils.toBean(createReqVO, PartitionBrothersDO.class);
        partitionBrothersMapper.insert(partitionBrothers);
        // 返回
        return partitionBrothers.getId();
    }

    @Override
    public void updatePartitionBrothers(PartitionBrothersSaveReqVO updateReqVO) {
        // 校验存在
        validatePartitionBrothersExists(updateReqVO.getId());
        // 更新
        PartitionBrothersDO updateObj = BeanUtils.toBean(updateReqVO, PartitionBrothersDO.class);
        partitionBrothersMapper.updateById(updateObj);
    }

    @Override
    public void deletePartitionBrothers(Long id) {
        // 校验存在
        validatePartitionBrothersExists(id);
        // 删除
        partitionBrothersMapper.deleteById(id);
    }

    private void validatePartitionBrothersExists(Long id) {
        if (partitionBrothersMapper.selectById(id) == null) {
            throw exception(PARTITION_BROTHERS_NOT_EXISTS);
        }
    }

    @Override
    public PartitionBrothersDO getPartitionBrothers(Long id) {
        return partitionBrothersMapper.selectById(id);
    }

    @Override
    public PageResult<PartitionBrothersDO> getPartitionBrothersPage(PartitionBrothersPageReqVO pageReqVO) {
        return partitionBrothersMapper.selectPage(pageReqVO);
    }

}