package cn.iocoder.yudao.module.chain.service.operatedetail;

import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.chain.controller.admin.operatedetail.vo.OperateDetailExportReqVO;
import cn.iocoder.yudao.module.chain.controller.admin.operatedetail.vo.OperateDetailPageReqVO;
import cn.iocoder.yudao.module.chain.dal.dataobject.operatedetail.OperateDetailDO;
import cn.iocoder.yudao.module.chain.dal.mysql.operatedetail.OperateDetailMapper;
import cn.iocoder.yudao.module.chain.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 区块上链操作具体上链信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OperateDetailServiceImpl implements OperateDetailService {

    @Resource
    private OperateDetailMapper operateDetailMapper;

    @Override
    public List<Long> createOperateDetailBatch(List<OperateDetailDO> operateDetails) {
        // 插入
        operateDetailMapper.insertBatch(operateDetails);
        // 返回
        return operateDetails.stream().map(OperateDetailDO::getId).collect(Collectors.toList());
    }

    @Override
    public void deleteOperateDetail(Long id) {
        // 校验存在
        validateOperateDetailExists(id);
        // 删除
        operateDetailMapper.deleteById(id);
    }

    @Override
    public void deleteOperateDetailBatch(List<Long> ids) {
        // 校验是否存在
        List<OperateDetailDO> operateDetailDOs =  operateDetailMapper.selectList(new LambdaQueryWrapperX<OperateDetailDO>().in(OperateDetailDO::getId, ids));
        if(operateDetailDOs.size() != ids.size()) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.BLOCK_CERTIFICATE_NOT_EXISTS);
        }
        operateDetailMapper.deleteBatchIds(ids);
    }

    private void validateOperateDetailExists(Long id) {
        if (operateDetailMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.OPERATE_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public OperateDetailDO getOperateDetail(Long id) {
        return operateDetailMapper.selectById(id);
    }

    @Override
    public List<OperateDetailDO> getOperateDetailList(Collection<Long> ids) {
        return operateDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OperateDetailDO> getOperateDetailPage(OperateDetailPageReqVO pageReqVO) {
        return operateDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OperateDetailDO> getOperateDetailList(OperateDetailExportReqVO exportReqVO) {
        return operateDetailMapper.selectList(exportReqVO);
    }

}
