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

import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofit.PtProfitDO;
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.partitionson.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.partitionson.PartitionSonDO;
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.partitionson.PartitionSonMapper;

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 PartitionSonServiceImpl implements PartitionSonService {

    @Resource
    private PartitionSonMapper partitionSonMapper;

    @Override
    public Long createPartitionSon(PartitionSonSaveReqVO createReqVO) {
        // 插入
        PartitionSonDO partitionSon = BeanUtils.toBean(createReqVO, PartitionSonDO.class);
        partitionSonMapper.insert(partitionSon);
        // 返回
        return partitionSon.getId();
    }

    @Override
    public void updatePartitionSon(PartitionSonSaveReqVO updateReqVO) {
        // 校验存在
        validatePartitionSonExists(updateReqVO.getId());
        // 更新
        PartitionSonDO updateObj = BeanUtils.toBean(updateReqVO, PartitionSonDO.class);
        partitionSonMapper.updateById(updateObj);
    }

    @Override
    public void deletePartitionSon(Long id) {
        // 校验存在
        validatePartitionSonExists(id);
        // 删除
        partitionSonMapper.deleteById(id);
    }

    private void validatePartitionSonExists(Long id) {
        if (partitionSonMapper.selectById(id) == null) {
            throw exception(PARTITION_SON_NOT_EXISTS);
        }
    }

    @Override
    public PartitionSonDO getPartitionSon(Long id) {
        return partitionSonMapper.selectById(id);
    }

    @Override
    public PageResult<PartitionSonDO> getPartitionSonPage(PartitionSonPageReqVO pageReqVO) {
        return partitionSonMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBatch(List<PartitionSonSaveReqVO> partitionSonSaveReqVOS) {
        // 批量存储数据
        List<PartitionSonDO> ptProfit = BeanUtils.toBean(partitionSonSaveReqVOS, PartitionSonDO.class);
        partitionSonMapper.insertBatch(ptProfit);
    }

}