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

import com.yd.common.enums.CommonEnum;
import com.yd.common.result.Result;
import com.yd.common.utils.RandomStringGenerator;
import com.yd.csf.api.service.ApiSalaryRemittanceService;
import com.yd.csf.feign.dto.salaryremittance.ApiSalaryRemittanceFzDTO;
import com.yd.csf.service.model.SalaryRemittance;
import com.yd.csf.service.service.ISalaryRemittanceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class ApiSalaryRemittanceServiceImpl implements ApiSalaryRemittanceService {

    @Autowired
    private ISalaryRemittanceService iSalaryRemittanceService;

    /**
     * 批量保存薪资汇款明细
     * @param salaryBizId
     * @param fzDTO
     * @return
     */
    @Override
    public Result batchSave(String salaryBizId, ApiSalaryRemittanceFzDTO fzDTO) {
        //先删，后批量新增
        iSalaryRemittanceService.delBySalaryBizId(salaryBizId);

        //批量新增
        if (CollectionUtils.isNotEmpty(fzDTO.getSalaryRemittanceDTOList())) {
            List<SalaryRemittance> saveList = fzDTO.getSalaryRemittanceDTOList()
                    .stream().map(dto -> {
                        SalaryRemittance salaryRemittance = new SalaryRemittance();
                        BeanUtils.copyProperties(dto,salaryRemittance);
                        salaryRemittance.setSalaryBizId(salaryBizId);
                        salaryRemittance.setSalaryRemittanceBizId(RandomStringGenerator.generateBizId16(CommonEnum.UID_TYPE_SALARY_REMITTANCE.getCode()));
                        return salaryRemittance;
                    }).collect(Collectors.toList());
            iSalaryRemittanceService.saveOrUpdateBatch(saveList);
        }
        return Result.success();
    }
}
