package com.yd.csf.api.service.impl;

import com.yd.common.result.Result;
import com.yd.csf.api.service.ApiUserBankService;
import com.yd.csf.feign.dto.userbank.ApiCsfClientUserBankDTO;
import com.yd.csf.service.model.CsfClientUser;
import com.yd.csf.service.model.UserBank;
import com.yd.csf.service.service.IUserBankService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Slf4j
@Service
public class ApiUserBankServiceImpl implements ApiUserBankService {

    @Resource
    private IUserBankService iUserBankService;

    /**
     * 处理用户银行列表信息
     * @param csfClientUser
     * @param bankDTOList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result saveUserBankList(CsfClientUser csfClientUser,
                                   List<ApiCsfClientUserBankDTO> bankDTOList) {
        //根据CSF客户端用户表唯一业务ID逻辑删除
        iUserBankService.del(csfClientUser.getCsfClientUserBizId());

        //新增银行列表信息
        if (CollectionUtils.isEmpty(bankDTOList)) {
            return Result.success();
        }

        List<UserBank> saveList = bankDTOList.stream().map(d -> {
            UserBank userBank = new UserBank();
            BeanUtils.copyProperties(d,userBank);
            return userBank;
        }).collect(Collectors.toList());

        iUserBankService.saveOrUpdateBatch(saveList);

        return Result.success();
    }



}
