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

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.chain.api.operatedetail.dto.OperateDetailSaveReqDTO;
import cn.iocoder.yudao.module.chain.dal.dataobject.operatedetail.OperateDetailDO;
import cn.iocoder.yudao.module.chain.service.operatedetail.OperateDetailService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.module.chain.enums.ApiConstants.VERSION;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
public class OperateDetailApiImpl implements OperateDetailApi {

    @Resource
    private OperateDetailService operateDetailService;

    @Override
    @Transactional
    public CommonResult<List<Long>> createOperateDetailBatch(List<OperateDetailSaveReqDTO> operateDetails) {
        List<OperateDetailDO> operateDetailDOs = new ArrayList<>();
        operateDetails.forEach(detail -> {
            OperateDetailDO operateDetailDO = new OperateDetailDO();
            BeanUtil.copyProperties(detail, operateDetailDO);
            operateDetailDOs.add(operateDetailDO);
        });
        return CommonResult.success(operateDetailService.createOperateDetailBatch(operateDetailDOs));
    }

    @Override
    @Transactional
    public CommonResult<Boolean> deleteOperateDetailBatch(List<Long> ids) {
        operateDetailService.deleteOperateDetailBatch(ids);
        return CommonResult.success(true);
    }

}
