package com.ruoyi.project.training.service.impl;

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

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.training.domain.TTrainingbccDevice;
import com.ruoyi.project.training.mapper.TTrainingbccDeviceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.training.mapper.TTrainingbccMapper;
import com.ruoyi.project.training.domain.TTrainingbcc;
import com.ruoyi.project.training.service.ITTrainingbccService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 装置培训跟踪bService业务层处理
 *
 * @author ssy
 * @date 2024-06-19
 */
@Service
public class TTrainingbccServiceImpl implements ITTrainingbccService
{
    @Resource
    private TTrainingbccMapper tTrainingbccMapper;
    @Resource
    private TTrainingbccDeviceMapper tTrainingbccDeviceMapper;

    protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
    /**
     * 查询装置培训跟踪b
     *
     * @param id 装置培训跟踪bID
     * @return 装置培训跟踪b
     */
    @Override
    public TTrainingbcc selectTTrainingbccById(Long id)
    {
        return tTrainingbccMapper.selectTTrainingbccById(id);
    }

    /**
     * 查询装置培训跟踪b列表
     *
     * @param tTrainingbcc 装置培训跟踪b
     * @return 装置培训跟踪b
     */
    @Override
    public List<TTrainingbcc> selectTTrainingbccList(TTrainingbcc tTrainingbcc)
    {
        return tTrainingbccMapper.selectTTrainingbccList(tTrainingbcc);
    }

    /**
     * 新增装置培训跟踪b
     *
     * @param tTrainingbcc 装置培训跟踪b
     * @return 结果
     */
    @Override
    public int insertTTrainingbcc(TTrainingbcc tTrainingbcc)
    {
        return tTrainingbccMapper.insertTTrainingbcc(tTrainingbcc);
    }

    /**
     * 修改装置培训跟踪b
     *
     * @param tTrainingbcc 装置培训跟踪b
     * @return 结果
     */
    @Override
    public int updateTTrainingbcc(TTrainingbcc tTrainingbcc)
    {
        return tTrainingbccMapper.updateTTrainingbcc(tTrainingbcc);
    }

    /**
     * 批量删除装置培训跟踪b
     *
     * @param ids 需要删除的装置培训跟踪bID
     * @return 结果
     */
    @Override
    public int deleteTTrainingbccByIds(Long[] ids)
    {
        return tTrainingbccMapper.deleteTTrainingbccByIds(ids);
    }

    /**
     * 删除装置培训跟踪b信息
     *
     * @param id 装置培训跟踪bID
     * @return 结果
     */
    @Override
    public int deleteTTrainingbccById(Long id)
    {
        return tTrainingbccMapper.deleteTTrainingbccById(id);
    }

    @Override
    @Transactional
    @RepeatSubmit
    public int updateStaffIds(TTrainingbcc tTrainingbcc) {
        TTrainingbccDevice param = new TTrainingbccDevice();
        param.setRegularId(tTrainingbcc.getId());
        List<TTrainingbccDevice> list = tTrainingbccDeviceMapper.selectTTrainingbccDeviceList(param);
        List<String> nowStaff = list.stream()
                .map(TTrainingbccDevice::getStaffId)
                .collect(Collectors.toList());
        logger.info(JSON.toJSONString(tTrainingbcc));
        List<String> newStaff = Arrays.asList(tTrainingbcc.getStaffIds().split(",").clone());

        // 找出a有b没有的元素
        List<String> needDel = new ArrayList<>();
        for (String element : nowStaff) {
            if (!newStaff.contains(element)) {
                needDel.add(element);
            }
        }

        // 找出b有a没有的元素
        List<String> needAdd = new ArrayList<>();
        for (String element : newStaff) {
            if (!nowStaff.contains(element)) {
                needAdd.add(element);
            }
        }

        //删除
        for (String staff: needDel
             ) {
            if (StringUtils.isNotEmpty(staff))  {
                TTrainingbccDevice device = new TTrainingbccDevice();
                device.setRegularId(tTrainingbcc.getId());
                device.setStaffId(staff);
                tTrainingbccDeviceMapper.deleteTTrainingbccDeviceByStaff(device);
            }

        }
        //新增
        TTrainingbcc trainingbcc = tTrainingbccMapper.selectTTrainingbccById(tTrainingbcc.getId());
        for (String staff :needAdd
             ) {
            if (StringUtils.isNotEmpty(staff))  {
                TTrainingbccDevice device = new TTrainingbccDevice();
                device.setStaffId(staff);
                device.setRegularId(tTrainingbcc.getId());
                device.setStartDate(trainingbcc.getCourseStartdate());
                logger.info(JSON.toJSONString(device));
                tTrainingbccDeviceMapper.insertTTrainingbccDevice(device);
            }

        }


        return 1;
    }
}
