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

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.chain.api.blockcertificate.dto.*;
import cn.iocoder.yudao.module.chain.controller.admin.blockcertificate.vo.*;
import cn.iocoder.yudao.module.chain.controller.admin.operatedetail.vo.OperateDetailSaveReqVO;
import cn.iocoder.yudao.module.chain.convert.blockcertificate.BlockCertificateConvert;
import cn.iocoder.yudao.module.chain.convert.operatedetail.OperateDetailConvert;
import cn.iocoder.yudao.module.chain.service.blockcertificate.BlockCertificateService;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class BlockCertificateApiImpl implements BlockCertificateApi {

    @Resource
    private BlockCertificateService blockCertificateService;

    @Override
    public CommonResult<List<BlockCertificateRespDTO>> getBlockCertificateList(BlockCertificateReqDTO blockCertificate) {
        List<BlockCertificateRespVO> resps = blockCertificateService.getBlockCertificateList(BlockCertificateConvert.INSTANCE.convertToReq(blockCertificate));
        List<BlockCertificateRespDTO> respDTOs = BlockCertificateConvert.INSTANCE.convertToDTOList(resps);
        resps.forEach(resp -> respDTOs.forEach(respDTO -> {
            if(resp.getChainOperateId().equals(respDTO.getChainOperateId())) {
                respDTO.setOperateDetails(OperateDetailConvert.INSTANCE.convertToDTOList(resp.getOperateDetails()));
            }
        }));
        return CommonResult.success(respDTOs);
    }

    @Override
    @Transactional
    public CommonResult<List<BlockCertificateRespDTO>> createBlockCertificateBatch(List<BlockCertificateCreateReqDTO> blockCertificates) {
        List<BlockCertificateCreateReqVO> reqs = BlockCertificateConvert.INSTANCE.convertToCreateReqList(blockCertificates);
        for (int i = 0; i < reqs.size(); i++) {
            reqs.get(i).setDetails(OperateDetailConvert.INSTANCE.convertToSaveReqList(blockCertificates.get(i).getDetails()));
        }
        return CommonResult.success(BlockCertificateConvert.INSTANCE.convertDOToDTOList(blockCertificateService.createBlockCertificate(reqs)));
    }

    @Override
    @Transactional
    public CommonResult<Boolean> updateBlockCertificateBatch(List<BlockCertificateUpdateReqDTO> blockCertificates) {
        List<BlockCertificateUpdateReqVO> updateReqVOs = new ArrayList<>();
        blockCertificates.forEach(block -> {
            BlockCertificateUpdateReqVO updateReqVO = new BlockCertificateUpdateReqVO();
            List<OperateDetailSaveReqVO> operateDetails = new ArrayList<>();
            BeanUtil.copyProperties(block, updateReqVO);
            if(block.getDetails() != null) {
                block.getDetails().forEach(detail -> {
                    OperateDetailSaveReqVO operateDetail = new OperateDetailSaveReqVO();
                    BeanUtil.copyProperties(detail, operateDetail);
                    operateDetails.add(operateDetail);
                });
            }
            updateReqVO.setDetails(operateDetails);
            updateReqVOs.add(updateReqVO);
        });
        blockCertificateService.updateBlockCertificateBatch(updateReqVOs);
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    @Transactional
    public CommonResult<Boolean> deleteBlockCertificateBatch(List<Long> ids) {
        blockCertificateService.deleteBlockCertificateBatch(ids);
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    @Transactional
    public CommonResult<List<BlockCertificateRespDTO>> createAndUpChainBatch(BlockCertificateUpChainReqDTO blockCertificate) {
        BlockCertificateUpChainReqVO createReqVO = new BlockCertificateUpChainReqVO();
        BeanUtil.copyProperties(blockCertificate, createReqVO);
        List<UpChainObjReqVO> operateObjs = new ArrayList<>();
        blockCertificate.getOperateObjs().forEach(item -> {
            UpChainObjReqVO upChainObjReqVO = new UpChainObjReqVO();
            BeanUtil.copyProperties(item, upChainObjReqVO);
            operateObjs.add(upChainObjReqVO);
        });
        createReqVO.setOperateObjs(operateObjs);
        List<BlockCertificateRespVO> respVOS = blockCertificateService.createAndUpChainBatch(createReqVO);
        List<BlockCertificateRespDTO> results = new ArrayList<>();
        respVOS.forEach(item -> {
            BlockCertificateRespDTO respDTO = new BlockCertificateRespDTO();
            BeanUtil.copyProperties(item, respDTO);
            results.add(respDTO);
        });
        return CommonResult.success(results);
    }

    @Override
    @Transactional
    public CommonResult<Boolean> updateStatusBatch(List<UpdateStatusReqDTO> updateReqDTOS) {
        List<UpdateStatusReqVO> updateReqVOS = new ArrayList<>();
        updateReqDTOS.forEach(item -> {
            UpdateStatusReqVO updateReqVO = new UpdateStatusReqVO();
            BeanUtil.copyProperties(item, updateReqVO);
            updateReqVOS.add(updateReqVO);
        });

        blockCertificateService.updateStatusBatch(updateReqVOS);
        return CommonResult.success(Boolean.TRUE);
    }

}
