package cn.iocoder.yudao.module.system.api.loan;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.loan.dto.LoanDTO;
import cn.iocoder.yudao.module.system.controller.admin.loan.vo.LoanSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.loan.LoanDO;
import cn.iocoder.yudao.module.system.service.loan.LoanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
public class LoanApiImpl implements LoanApi {

    @Resource
    private LoanService loanService;

    @Override
    public CommonResult<Boolean> createLoan(LoanDTO dto) {
        LoanSaveReqVO createDO = BeanUtils.toBean(dto, LoanSaveReqVO.class);
        loanService.createLoan(createDO);
        return success(true);
    }

    @Override
    public CommonResult<LoanDTO> getByUserId(Long userId, Integer loanType) {
        LoanDO loanDO = loanService.getByUserId(userId, loanType);
        return success(BeanUtils.toBean(loanDO, LoanDTO.class));
    }

    @Override
    public CommonResult<List<LoanDTO>> getListByUserId(Collection<Long> userId, Integer loanType) {
        List<LoanDO> loanDOList = loanService.getListByUserId(userId, loanType);
        return success(BeanUtils.toBean(loanDOList, LoanDTO.class));
    }

    @Override
    public CommonResult<List<LoanDTO>> getListByMonth(Collection<Long> userId, String month) {
        List<LoanDO> loanDOList = loanService.getListByMonth(userId, month);
        return success(BeanUtils.toBean(loanDOList, LoanDTO.class));
    }
}
