package com.elephant.framework.galaxy.aroad.module.attendance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.attendance.dto.AttendanceDto;
import com.elephant.framework.galaxy.aroad.module.attendance.dto.MemberDto;
import com.elephant.framework.galaxy.aroad.module.attendance.dto.SaveAttendanceDto;
import com.elephant.framework.galaxy.aroad.module.attendance.entity.Attendance;
import com.elephant.framework.galaxy.aroad.module.attendance.entity.AttendanceMembers;
import com.elephant.framework.galaxy.aroad.module.attendance.mapper.AttendanceMapper;
import com.elephant.framework.galaxy.aroad.module.attendance.service.AttendanceMembersService;
import com.elephant.framework.galaxy.aroad.module.attendance.service.AttendanceService;
import com.elephant.framework.galaxy.aroad.module.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 排班表
 *
 * @author songwenjie
 * @date 2019-04-11 10:09:46
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {

    @Autowired
    private AttendanceMembersService membersService;

    @Autowired
    private UserService userService;





    /**
     * @Description: 查询部门下的排班计划
     * @Param: [deptId]
     * @return: java.util.List<com.elephant.framework.galaxy.aroad.module.attendance.dto.AttendanceDto>
     * @Author: songwenjie
     * @Date: 2019/4/16
     */
    public List<AttendanceDto> getAttendance(int deptId){
        return baseMapper.getAttendance(deptId);
    }

    /** 
    * @Description: 添加排班（系统选择值班人员）
    * @Param: [attendanceDto] 
    * @return: void 
    * @Author: songwenjie 
    * @Date: 2019/4/18 
    */ 
    @Transactional
    public boolean autoSave(SaveAttendanceDto<Integer> attendanceDto){
        Attendance attendance = Attendance.builder()
                .shiftId(attendanceDto.getShiftId())
                .time(attendanceDto.getTime())
                .unitId(attendanceDto.getUnitId())
                .build();

        List<MemberDto> users = userService.getUsers(attendanceDto.getMembers());
        if(users == null || users.size() == 0 ){
            return false;
        }
        Integer attendanceId = baseMapper.getAttendanceId(attendance); //查询此部门此班次是否有排班
        if(attendanceId == null){
            baseMapper.saveAttendance(attendance);
        }
        List<AttendanceMembers> members = new ArrayList<>();
        users.forEach(u->{
            members.add(
                    AttendanceMembers.builder()
                    .attendanceId(attendanceId != null ?attendanceId : attendance.getAttendanceId())
                    .name(u.getName())
                    .phone(u.getPhone())
                    .build()
            );
        });
        return membersService.saveOrUpdateBatch(members);
    }


    /** 
    * @Description:  添加排班（手动输入值班人员）
    * @Param: [attendanceDto] 
    * @return: boolean 
    * @Author: songwenjie 
    * @Date: 2019/4/18 
    */ 
    public boolean manualSave(SaveAttendanceDto<MemberDto> attendanceDto){
        Attendance attendance = Attendance.builder()
                .shiftId(attendanceDto.getShiftId())
                .time(attendanceDto.getTime())
                .unitId(attendanceDto.getUnitId())
                .build();

        List<MemberDto> users = attendanceDto.getMembers();
        if(users == null || users.size() == 0 ){
            return false;
        }
        Integer attendanceId = baseMapper.getAttendanceId(attendance); //查询此部门此班次是否有排班
        if(attendanceId == null){
            baseMapper.saveAttendance(attendance);
        }
        List<AttendanceMembers> members = new ArrayList<>();
        users.forEach(u->{
            members.add(
                    AttendanceMembers.builder()
                            .attendanceId(attendanceId != null ?attendanceId : attendance.getAttendanceId())
                            .name(u.getName())
                            .phone(u.getPhone())
                            .build()
            );
        });
        return membersService.saveBatch(members);
    }
}
