package com.foreverwin.mesnac.dispatch.service.impl;

import com.foreverwin.mesnac.dispatch.dto.SfcDispatchEmployeeDto;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.mesnac.dispatch.model.SfcDispatchEmployee;
import com.foreverwin.mesnac.dispatch.mapper.SfcDispatchEmployeeMapper;
import com.foreverwin.mesnac.dispatch.service.SfcDispatchEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Leon.L
 * @since 2021-09-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SfcDispatchEmployeeServiceImpl extends ServiceImpl<SfcDispatchEmployeeMapper, SfcDispatchEmployee> implements SfcDispatchEmployeeService {


    @Autowired
    private SfcDispatchEmployeeMapper sfcDispatchEmployeeMapper;

    @Override
    public void saveSfcDispatchEmployee(List<SfcDispatchEmployeeDto> sfcDispatchEmployeeList) {
        if (sfcDispatchEmployeeList == null || sfcDispatchEmployeeList.size() <= 0) {
            return;
        }

        String user = CommonMethods.getUser();
        LocalDateTime nowDate = LocalDateTime.now();
        List<SfcDispatchEmployee> list = new ArrayList<>();
        for (int i = 0; i < sfcDispatchEmployeeList.size(); i++) {
            SfcDispatchEmployeeDto disDto = sfcDispatchEmployeeList.get(i);
            String [] array = disDto.getDispatchEmployee();
            if (array == null || array.length <= 0) {
                continue;
            }

            for (int j = 0; j < array.length; j++) {
                SfcDispatchEmployee sfcDispatchEmployee = new SfcDispatchEmployee();
                sfcDispatchEmployee.setHandle(UUID.randomUUID().toString());
                sfcDispatchEmployee.setSite(disDto.getSite());
                sfcDispatchEmployee.setSfcDispatchBo(disDto.getSfcDispatchBo());
                sfcDispatchEmployee.setEmployee(array[j]);
                sfcDispatchEmployee.setCreateUser(user);
                sfcDispatchEmployee.setCreatedDateTime(nowDate);
                list.add(sfcDispatchEmployee);
            }
        }

        //删除派工人员
        sfcDispatchEmployeeMapper.deleteSfcDispatchEmployee(sfcDispatchEmployeeList);

        //保存派工人员
        if (list != null && list.size() > 0) {
            this.saveBatch(list);
        }
    }
}