package com.zjty.tynotes.attendance.service.impl;

import com.zjty.tynotes.attendance.dao.ApprovalInformationDao;
import com.zjty.tynotes.attendance.dao.AttendanceDetailsDao;
import com.zjty.tynotes.attendance.dao.UserOverWorkDao;
import com.zjty.tynotes.attendance.entity.ApprovalInformation;
import com.zjty.tynotes.attendance.entity.AttendanceDetails;
import com.zjty.tynotes.attendance.entity.UserOverWork;
import com.zjty.tynotes.attendance.service.ApprovalInformationService;
import com.zjty.tynotes.pas.dao.ConfigDao;
import com.zjty.tynotes.pas.dao.DayDao;
import com.zjty.tynotes.pas.dao.DepartmentDao;
import com.zjty.tynotes.pas.entity.Config;
import com.zjty.tynotes.pas.entity.Day;
import com.zjty.tynotes.pas.entity.Department;
import com.zjty.tynotes.pas.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotEmpty;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author gwj
 * @create 2020/3/24 15:23
 */
@Service
public class ApprovalInformationServiceImpl implements ApprovalInformationService {
    @Autowired
    private ApprovalInformationDao approvalInformationDao;
    @Autowired
    private AttendanceDetailsDao attendanceDetailsDao;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private UserOverWorkDao userOverWorkDao;
    @Autowired
    private DayDao dayDao;
    @Autowired
    private ConfigDao configDao;

    @Override
    public ApprovalInformation addApproval(ApprovalInformation approvalInformation) {

        if(approvalInformation.getApprovalType().equals("调休")){
            String userId = approvalInformation.getUserId();
            AttendanceDetails attendanceDetails = attendanceDetailsDao.findByUserId(userId);
            Integer workOverHours = attendanceDetails.getWorkOverHours();
            Date leaveEndTime = approvalInformation.getLeaveEndTime();
            Date leaveStartTime = approvalInformation.getLeaveStartTime();
            long l = leaveEndTime.getTime() - leaveStartTime.getTime();
            long daytime = 24*60*60*1000;
            long day;
            if(l%daytime==0){
                day = l/daytime;
            }else{
                day = l/daytime + 1;
            }
            if(workOverHours<=(day*8)){
                return null;
            }
            if(day>2){
                approvalInformation.setApprovalLevel("1");
            }else{
                approvalInformation.setApprovalLevel("2");
            }

        }
        approvalInformation.setStatus("待审批");
        return approvalInformationDao.save(approvalInformation);
    }

    @Override
    public boolean deleteApproval(String id) {
        Optional<ApprovalInformation> optional = approvalInformationDao.findById(id);
        if(optional.isPresent()){
            ApprovalInformation approvalInformation = optional.get();
            String status = approvalInformation.getStatus();
            if(("0").equals(status)){
                approvalInformationDao.deleteById(id);
                return true;
            }
        }
        return false;
    }

    @Override
    @Transactional
    public ApprovalInformation auditApproval(String userId,String approvalStatus,ApprovalInformation approvalInformation) {
        String departmentId = approvalInformation.getDepartmentId();
        String departmentLevel = iUserService.getDepartmentLevel(departmentId);
        if(("1").equals(departmentLevel)||("2").equals(departmentLevel)){
            approvalInformation.setApprovalUserIdOne(userId);
            if(approvalInformation.getApprovalStatus()!=null && approvalInformation.getApprovalStatus().equals("0")){
                if(StringUtils.isNotBlank(approvalInformation.getApprovalType())&&approvalInformation.getApprovalType().equals("调休")){
                    UserOverWork userOverWork = userOverWorkDao.findByUserId(approvalInformation.getUserId());

                }
                approvalInformation.setStatus("1");
                approvalInformation.setApprovalOne("0");
            }else if(approvalInformation.getApprovalStatus()!=null && approvalInformation.getApprovalStatus().equals("1")){
                approvalInformation.setStatus("2");
                approvalInformation.setApprovalOne("1");
            }
        }else {
            if(approvalInformation.getApprovalTwo()==null){
                approvalInformation.setApprovalUserIdTwo(userId);
                if(approvalInformation.getApprovalStatus()!=null && approvalInformation.getApprovalStatus().equals("0")){
                    approvalInformation.setApprovalTwo("0");
                }else if(approvalInformation.getApprovalStatus()!=null && approvalInformation.getApprovalStatus().equals("1")){
                    approvalInformation.setStatus("2");
                    approvalInformation.setApprovalTwo("1");
                }
            }else if(approvalInformation.getApprovalTwo()!=null && approvalInformation.getApprovalOne()==null){
                String parentDepartmentLevel = iUserService.getParentDepartmentLevel(departmentId);
                if(("1").equals(parentDepartmentLevel)||("2").equals(parentDepartmentLevel)){
                    approvalInformation.setApprovalUserIdOne(userId);
                    if(approvalInformation.getApprovalStatus()!=null && approvalInformation.getApprovalStatus().equals("0")){
                        approvalInformation.setStatus("1");
                        approvalInformation.setApprovalOne("0");
                    }else if(approvalInformation.getApprovalStatus()!=null && approvalInformation.getApprovalStatus().equals("1")){
                        approvalInformation.setStatus("2");
                        approvalInformation.setApprovalOne("1");
                    }
                }
            }
        }
        return approvalInformation;
    }


    @Override
    public List<ApprovalInformation> findAllApproval(String userId) {
        return approvalInformationDao.findAllByUserId(userId);
    }

    @Override
    public List<ApprovalInformation> findAllAuditApproval(String userId) {
        List<String> statusList = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        statusList.add("1");
        statusList.add("2");
        List<ApprovalInformation> approvalInformations = approvalInformationDao.findAllByStatusIn(statusList);
        List<ApprovalInformation> approvalInformationList = new ArrayList<>();
        approvalInformations.forEach(approvalInformation -> {
            if(userId.equals(approvalInformation.getApprovalUserIdOne())){
                approvalInformationList.add(approvalInformation);
            }else if(userId.equals(approvalInformation.getApprovalUserIdTwo())){
                approvalInformationList.add(approvalInformation);
            }
        });
        strings.add("0");
        List<ApprovalInformation> approvalInformationList1 = approvalInformationDao.findAllByStatusIn(strings);
        approvalInformationList1.forEach(approvalInformation -> {
            String departmentId = approvalInformation.getDepartmentId();
            String departmentLevel = iUserService.getDepartmentLevel(departmentId);
            if(("1").equals(departmentLevel)||("2").equals(departmentLevel)){
                boolean flag = iUserService.findUsersByDepartmentAndAuthority(userId,departmentId);
                if(flag){
                    approvalInformationList.add(approvalInformation);
                }
            }else{
                if(approvalInformation.getApprovalTwo()==null&&approvalInformation.getApprovalOne()==null){
                    boolean flag = iUserService.findUsersByDepartmentAndAuthority(userId,departmentId);
                    if(flag){
                        approvalInformationList.add(approvalInformation);
                    }
                }else if (approvalInformation.getApprovalTwo()!=null && approvalInformation.getApprovalOne()==null){
                    Optional<Department> optional = departmentDao.findById(departmentId);
                    if(optional.isPresent()){
                        Department department = optional.get();
                        String parentId = department.getParentId();
                        boolean flag = iUserService.findUsersByDepartmentAndAuthority(userId,parentId);
                        if(flag){
                            approvalInformationList.add(approvalInformation);
                        }
                    }
                }
            }

        });
        return approvalInformationList;
    }

    /**
     * 计算调休时长
     */
    private Long computeTime(Date startTime,Date endTime){
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Config config = configDao.findAll().get(0);
//        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd "+config.getWorkTime());
//        SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd "+config.getAfterWorkTime());
//        String format = sdf.format(startTime);
//        try {
//            Calendar calendar = Calendar.getInstance();
//            Date parse1 = sdf2.parse(format);//该天的上班时间
//            Date parse2 = sdf3.parse(format);
//            Date parse = sdf.parse(format);
//            Day day = dayDao.findByDate(parse);
//            String status = day.getStatus();
//            long hours = 0;
//
//            while(true){
//                if(parse1.before(startTime)){
//                    if(endTime.before(parse2)){
//                        Long time = endTime.getTime() - startTime.getTime();
//                        long hour = 1000*60*60;
//                        if(time%hour==0){
//                            hours = time/hour;
//                        }else{
//                            hours = time/hour + 1;
//                        }
//                    }else{
//
//                    }
//                }
//            }
//
//
//
//
//            //判断该天是否是工作日
//            if(status.equals("0")) {
//                if(parse1.before(startTime)){
//                    if(endTime.before(parse2)){
//                        Long time = endTime.getTime() - startTime.getTime();
//                        long hour = 1000*60*60;
//                        if(time%hour==0){
//                            hours = time/hour;
//                        }else{
//                            hours = time/hour + 1;
//                        }
//                    }else{
//                        calendar.setTime(parse1);
//                        calendar.add(Calendar.DATE,1);
//                        Date time = calendar.getTime();
//                        Day day1 = dayDao.findByDate(time);
//                        if(day1.getStatus().equals("0")){
//
//                        }
//                    }
//                }
//            }else{
//                calendar.setTime(parse1);
//                calendar.add(Calendar.DATE,1);
//                Date time = calendar.getTime();
//                calendar.setTime(parse2);
//                calendar.add(Calendar.DATE,1);
//                Date time1 = calendar.getTime();
//                Day day1 = dayDao.findByDate(time);
//                if(time.before(endTime)){
//                    if(endTime.before(parse2)){
//
//                    }
//                }
//            }
//
////            if(status.equals("0")){//判断该天是否是工作日
////                if(parse1.before(startTime)&&endTime.before(parse2)){
////                    if(endTime.before(parse2)){
////                        Long time = endTime.getTime() - startTime.getTime();
////                        long hour = 1000*60*60;
////                        long hours = 0;
////                        if(time%hour==0){
////                            hours = time/hour;
////                        }else{
////                            hours = time/hour + 1;
////                        }
////                    }else{
////                        calendar.setTime(parse1);
////                        calendar.add(Calendar.DATE,1);//往后一天的上班时间
////                        Date time1 = calendar.getTime();
////                        if(endTime.before(time1)){
////                            Long time = parse2.getTime() - startTime.getTime();
////                            long hour = 1000*60*60;
////                            long hours = 0;
////                            if(time%hour==0){
////                                hours = time/hour;
////                            }else{
////                                hours = time/hour + 1;
////                            }
////                        }
////                    }
////
////                }
////            }
//
//
//
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        return null;
    }


}
