package cn.iocoder.yudao.module.flow.api.outbound;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.flow.api.outbound.dto.OutboundGoodsDTO;
import cn.iocoder.yudao.module.flow.api.outbound.dto.OutboundRespDTO;
import cn.iocoder.yudao.module.flow.controller.admin.outbound.vo.OutboundDTO;
import cn.iocoder.yudao.module.flow.convert.outbound.OutboundConvert;
import cn.iocoder.yudao.module.flow.dal.dataobject.outbound.OutboundDO;
import cn.iocoder.yudao.module.flow.dal.mysql.outbound.OutboundMapper;
import cn.iocoder.yudao.module.flow.enums.ErrorCodeConstants;
import cn.iocoder.yudao.module.flow.service.outbound.OutboundService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ApiConstants.VERSION;

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

    @Resource
    private OutboundService OutboundService;
    @Resource
    private OutboundMapper outboundMapper;

    @Override
    public CommonResult<OutboundRespDTO> getOutboundByOutBoundId(String outBoundId) {
        OutboundDTO outboundDTO = OutboundService.getOutboundGoodsInfoOutBoundId(outBoundId);
        if(outboundDTO == null || outboundDTO.getOutboundGoodsList().isEmpty()) {
            throw exception(ErrorCodeConstants.OUTBOUND_NOT_EXISTS);
        }
        OutboundRespDTO respDTO = new OutboundRespDTO();
        BeanUtil.copyProperties(outboundDTO, respDTO);
        List<OutboundGoodsDTO> list = BeanUtil.copyToList(outboundDTO.getOutboundGoodsList(), OutboundGoodsDTO.class);
        respDTO.setOutboundGoodsList(list);
        return CommonResult.success(respDTO);
    }

    @Override
    public CommonResult<List<OutboundRespDTO>> getOutboundListByVendorId(String vendorId) {
        return CommonResult.success(OutboundConvert.INSTANCE.convertToRespDTOList(outboundMapper.selectList(OutboundDO::getVendorId,vendorId)));
    }

}
