package com.jjt.emp.service.impl;

import com.jjt.common.utils.StringUtils;
import com.jjt.emp.domain.TwinEmp;
import com.jjt.emp.domain.TwinEmpDetail;
import com.jjt.emp.mapper.TwinEmpMapper;
import com.jjt.emp.service.ITwinEmpService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 员工排班Service业务层处理
 *
 * @author wukai
 * @date 2025-01-18
 */
@Service
public class TwinEmpServiceImpl implements ITwinEmpService {
    @Resource
    private TwinEmpMapper twinEmpMapper;

    /**
     * 查询员工排班
     *
     * @param empId 员工排班主键
     * @return 员工排班
     */
    @Override
    public TwinEmp selectTwinEmpByEmpId(Long empId) {
        TwinEmp twinEmp = twinEmpMapper.selectTwinEmpByEmpId(empId);
        List<TwinEmpDetail> listA = twinEmp.getTwinEmpDetailList().stream().filter(d -> "A".equals(d.getEmpTeam())).collect(Collectors.toList());
        List<TwinEmpDetail> listB = twinEmp.getTwinEmpDetailList().stream().filter(d -> "B".equals(d.getEmpTeam())).collect(Collectors.toList());
        twinEmp.setTwinEmpDetailListA(listA);
        twinEmp.setTwinEmpDetailListB(listB);
        return twinEmp;
    }

    /**
     * 查询员工排班列表
     *
     * @param twinEmp 员工排班
     * @return 员工排班
     */
    @Override
    public List<TwinEmp> selectTwinEmpList(TwinEmp twinEmp) {
        return twinEmpMapper.selectTwinEmpList(twinEmp);
    }

    /**
     * 新增员工排班
     *
     * @param twinEmp 员工排班
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTwinEmp(TwinEmp twinEmp) {
        int rows = twinEmpMapper.insertTwinEmp(twinEmp);
        insertTwinEmpDetail(twinEmp);
        return rows;
    }

    /**
     * 修改员工排班
     *
     * @param twinEmp 员工排班
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTwinEmp(TwinEmp twinEmp) {
        twinEmpMapper.deleteTwinEmpDetailByEmpId(twinEmp.getEmpId())
        ;
        insertTwinEmpDetail(twinEmp);
        return twinEmpMapper.updateTwinEmp(twinEmp);
    }

    /**
     * 批量删除员工排班
     *
     * @param empIds 需要删除的员工排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTwinEmpByEmpIds(Long[] empIds) {
        twinEmpMapper.deleteTwinEmpDetailByEmpIds(empIds);
        return twinEmpMapper.deleteTwinEmpByEmpIds(empIds);
    }

    /**
     * 删除员工排班信息
     *
     * @param empId 员工排班主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTwinEmpByEmpId(Long empId) {
        twinEmpMapper.deleteTwinEmpDetailByEmpId(empId);
        return twinEmpMapper.deleteTwinEmpByEmpId(empId);
    }

    /**
     * 新增员工排班明细信息
     *
     * @param twinEmp 员工排班对象
     */
    public void insertTwinEmpDetail(TwinEmp twinEmp) {
        List<TwinEmpDetail> twinEmpDetailList = new ArrayList<>();
        twinEmp.getTwinEmpDetailListA().forEach(d -> {
            twinEmpDetailList.add(d);
        });
        twinEmp.getTwinEmpDetailListB().forEach(d -> {
            twinEmpDetailList.add(d);
        });
        Long empId = twinEmp.getEmpId();
        if (StringUtils.isNotNull(twinEmpDetailList)) {
            List<TwinEmpDetail> list = new ArrayList<TwinEmpDetail>();
            for (TwinEmpDetail twinEmpDetail : twinEmpDetailList) {
                twinEmpDetail.setEmpId(empId);
                list.add(twinEmpDetail);
            }
            if (list.size() > 0) {
                twinEmpMapper.batchTwinEmpDetail(list);
            }
        }
    }

    /**
     * 按日期查询排班
     *
     * @param date 日期
     * @return 排班结果
     */
    @Override
    public TwinEmp selectTwinEmpByDate(Date date) {
        TwinEmp twinEmp = new TwinEmp();
        twinEmp.setEmpDate(date);
        List<TwinEmp> list = selectTwinEmpList(twinEmp);

        if (list.size() == 0) {
            //如果未找到，则取最后一条记录
            list = selectTwinEmpList(new TwinEmp());
            Long empId = list.get(0).getEmpId();

            TwinEmp emp = selectTwinEmpByEmpId(empId);
            emp.setEmpId(null);
            emp.setEmpDate(date);
            insertTwinEmp(emp);
            twinEmp = emp;
        } else {
            Long empId = list.get(0).getEmpId();
            twinEmp = selectTwinEmpByEmpId(empId);
        }
        return twinEmp;
    }
}
