package com.jjt.wkEmp.service.impl;

import com.jjt.biz.vo.KnittingEmpVO;
import com.jjt.wkEmp.domain.TwinWkEmpRota;
import com.jjt.wkEmp.mapper.TwinWkEmpRotaMapper;
import com.jjt.wkEmp.service.ITwinWkEmpRotaService;
import org.springframework.stereotype.Service;

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

/**
 * 经编车间员工排班Service业务层处理
 *
 * @author wukai
 * @date 2025-04-28
 */
@Service
public class TwinWkEmpRotaServiceImpl implements ITwinWkEmpRotaService {
    @Resource
    private TwinWkEmpRotaMapper twinWkEmpRotaMapper;

    /**
     * 查询经编车间员工排班
     *
     * @param shiftId 经编车间员工排班主键
     * @return 经编车间员工排班
     */
    @Override
    public TwinWkEmpRota selectTwinWkEmpRotaByShiftId(Long shiftId) {
        return twinWkEmpRotaMapper.selectTwinWkEmpRotaByShiftId(shiftId);
    }

    /**
     * 查询经编车间员工排班列表
     *
     * @param twinWkEmpRota 经编车间员工排班
     * @return 经编车间员工排班
     */
    @Override
    public List<TwinWkEmpRota> selectTwinWkEmpRotaList(TwinWkEmpRota twinWkEmpRota) {
        return twinWkEmpRotaMapper.selectTwinWkEmpRotaList(twinWkEmpRota);
    }

    /**
     * 按时间查询列表
     *
     * @param date 时间
     * @return 结果
     */
    @Override
    public List<TwinWkEmpRota> selectTwinWkEmpRotaListByDate(Date date) {
        TwinWkEmpRota twinWkEmpRota = new TwinWkEmpRota();
        twinWkEmpRota.setEmpDate(date);
        return selectTwinWkEmpRotaList(twinWkEmpRota);
    }


    /**
     * 新增经编车间员工排班
     *
     * @param twinWkEmpRota 经编车间员工排班
     * @return 结果
     */
    @Override
    public int insertTwinWkEmpRota(TwinWkEmpRota twinWkEmpRota) {
        twinWkEmpRota.setCreatedTime(new Date());
        return twinWkEmpRotaMapper.insertTwinWkEmpRota(twinWkEmpRota);
    }

    /**
     * 修改经编车间员工排班
     *
     * @param twinWkEmpRota 经编车间员工排班
     * @return 结果
     */
    @Override
    public int updateTwinWkEmpRota(TwinWkEmpRota twinWkEmpRota) {
        twinWkEmpRota.setUpdatedTime(new Date());
        return twinWkEmpRotaMapper.updateTwinWkEmpRota(twinWkEmpRota);
    }

    /**
     * 批量删除经编车间员工排班
     *
     * @param shiftIds 需要删除的经编车间员工排班主键
     * @return 结果
     */
    @Override
    public int deleteTwinWkEmpRotaByShiftIds(Long[] shiftIds) {
        return twinWkEmpRotaMapper.deleteTwinWkEmpRotaByShiftIds(shiftIds);
    }

    /**
     * 删除经编车间员工排班信息
     *
     * @param shiftId 经编车间员工排班主键
     * @return 结果
     */
    @Override
    public int deleteTwinWkEmpRotaByShiftId(Long shiftId) {
        return twinWkEmpRotaMapper.deleteTwinWkEmpRotaByShiftId(shiftId);
    }

    /**
     * 数据同步
     *
     * @param data 接口数据
     */
    @Override
    public void sync(KnittingEmpVO data) {
        TwinWkEmpRota rota = selectByEmp(data.getDate(), data.getId());
        rota.setEmpId(data.getId());
        rota.setEmpName(data.getName());
        rota.setEmpTeam(data.getTeam());
        rota.setInTime(data.getIn());
        rota.setOutTime(data.getOut());
        Set<Integer> deviceSet = new TreeSet<>();
        for (String s : data.getDevices()) {
            String id = s.replace("J", "");
            deviceSet.add(Integer.parseInt(id));
        }
        String devices = deviceSet.stream().map(String::valueOf).collect(Collectors.joining(","));
        rota.setDevices(devices);
        if (rota.getShiftId() != null) {
            updateTwinWkEmpRota(rota);
        } else {
            insertTwinWkEmpRota(rota);
        }
    }


    /**
     * 按时间和员工号查询记录
     */

    private TwinWkEmpRota selectByEmp(Date date, String empId) {
        TwinWkEmpRota search = new TwinWkEmpRota();
        search.setEmpDate(date);
        search.setEmpId(empId);

        List<TwinWkEmpRota> list = selectTwinWkEmpRotaList(search);
        TwinWkEmpRota rota;
        if (list.size() > 0) {
            rota = list.get(0);
        } else {
            rota = search;
        }
        return rota;
    }
}
