/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.oa.service;

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.MenuStatusEnum;
import com.jeeplus.modules.oa.dao.OaAttendanceDao;
import com.jeeplus.modules.oa.entity.OaAttendance;
import com.jeeplus.modules.oa.entity.OaAttendanceRule;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.work.entity.report.WorkReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 考勤表Service
 * @author 孟祥越
 * @version 2017-05-11
 */
@Service
@Transactional(readOnly = true)
public class OaAttendanceService extends CrudService<OaAttendanceDao, OaAttendance> {

	@Autowired
	private OaAttendanceDao oaAttendanceDao;
	@Autowired
	private OaAttendanceRuleService oaAttendanceRuleService;
	
	public OaAttendance get(String id) {
		return super.get(id);
	}
	
	public List<OaAttendance> findList(OaAttendance oaAttendance) {
		List<OaAttendance> list = oaAttendanceDao.findList(oaAttendance);
		return list;
	}
	
	public Page<OaAttendance> findPage(Page<OaAttendance> page, OaAttendance oaAttendance) {
		oaAttendance.setPage(page);
		page.setList(this.findList(oaAttendance));
		return page;
	}
	public Page<OaAttendance> findPageByUser(Page<OaAttendance>page , OaAttendance oaAttendance){
		oaAttendance.setPage(page);
		page.setList(oaAttendanceDao.findListByUser(oaAttendance));
		return page ;
	}
	public Page<OaAttendance> findPageByCompany(Page<OaAttendance> page,OaAttendance oaAttendance){
		oaAttendance.setPage(page);
		page.setList(dao.findByCompany(oaAttendance));
		return page;
	}
	public List<OaAttendance> findByCompany(OaAttendance oaAttendance) {
		return dao.findByCompany(oaAttendance);
	}
	
	@Transactional(readOnly = false)
	public String save(OaAttendance oaAttendance,String type) {
		try {
			Office company = oaAttendance.getCompany();
			if (company == null) {
				return "打卡失败";
			}
			OaAttendance oaAtt = new OaAttendance();
			oaAtt.setOffice(UserUtils.getSelectOffice());
			OaAttendanceRule oaAttendanceRule = oaAttendanceRuleService.get(oaAttendance.getRule().getId());
			if (type.equals("1")) {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
				String endTime = oaAttendanceRule.getEndTime();
				Calendar c1 = new GregorianCalendar();
				Calendar c2 = Calendar.getInstance();
				Date end = null;
				try {
					end = sdf.parse(endTime);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				oaAtt = this.getOaAttendanceByDate(oaAttendance, 0);
				if (oaAtt != null) {
					return "上班已打卡";
				}
				c1.setTime(end);
				c1.set(Calendar.YEAR, c2.get(Calendar.YEAR));
				c1.set(Calendar.MONTH, c2.get(Calendar.MONTH));
				c1.set(Calendar.DAY_OF_MONTH, c2.get(Calendar.DAY_OF_MONTH));
				System.out.println(c2.getTime());
				System.out.println(c1.getTime());
				if (c2.getTime().compareTo(c1.getTime()) > 0) {
					System.out.println("已超过上班打卡时间");
					return "已超过上班打卡时间";
				} else {
					oaAttendance.setStartTime(new Date());
					oaAttendance.setSignDate(new Date());
					oaAtt = oaAttendance;
				}
			}
			if (type.equals("2")) {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
				String startTime = oaAttendanceRule.getStartTime();
				Calendar c1 = new GregorianCalendar();//规定次日上班时间
				Calendar c2 = Calendar.getInstance();//当前时间
				Date start = null;
				try {
					start = sdf.parse(startTime);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				c1.setTime(start);
				c1.set(Calendar.YEAR, c2.get(Calendar.YEAR));
				c1.set(Calendar.MONTH, c2.get(Calendar.MONTH));
				c1.set(Calendar.DAY_OF_MONTH, c2.get(Calendar.DAY_OF_MONTH));
				oaAtt = this.getOaAttendanceByDate(oaAttendance, 0);
				if (oaAtt != null) {
					if (oaAtt.getEndTime() != null && !oaAtt.getEndTime().equals("")) {
						return "下班已打卡";
					}
					c1.add(Calendar.DATE, +1);
				/*if(c2.compareTo(c1)>0){
					System.out.println("已超过下班打卡时间");
					return "已超过下班打卡时间";
				}else{
					oaAtt.setEndTime(new Date());
				}*/
					oaAtt.setEndTime(new Date());
				} else {
					oaAttendance.setEndTime(new Date());
					oaAttendance.setSignDate(new Date());
					oaAtt = oaAttendance;
				/*oaAtt = this.getOaAttendanceByDate(oaAttendance, -1);
				if(oaAtt!=null){
                    if(c2.compareTo(c1)<0){
                        if(oaAtt.getEndTime()!=null||oaAtt.getEndTime().equals("")){
                            return "下班已打卡";
                        }else{
                            oaAtt.setEndTime(new Date());
                        }
                    }
					*//*if(c2.compareTo(c1)>0){
						System.out.println("已超过下班打卡时间");
						return "已超过下班打卡时间";
					}*//*
					oaAtt.setEndTime(new Date());
				}else{
					c1.setTime(start);
					c1.set(Calendar.YEAR, c2.get(Calendar.YEAR));
					c1.set(Calendar.MONTH, c2.get(Calendar.MONTH));
					c1.set(Calendar.DAY_OF_MONTH, c2.get(Calendar.DAY_OF_MONTH)+1);
					*//*if(c2.compareTo(c1)>0){
						System.out.println("已超过下班打卡时间");
						return "已超过下班打卡时间";
					}else{
						oaAttendance.setEndTime(new Date());
						oaAttendance.setSignDate(new Date());
						oaAtt = oaAttendance;
					}*//*
					oaAttendance.setEndTime(new Date());
					oaAttendance.setSignDate(new Date());
					oaAtt = oaAttendance;
				}*/
				}

			}
			oaAtt.setOffice(UserUtils.getSelectOffice());


			super.save(oaAtt);
			return "ok";
		}catch (Exception e){
			return "";
		}
	}

	public OaAttendance getOaAttendanceByDate(OaAttendance oaAttendance,int days) {
		List<OaAttendance> oaAttList = new ArrayList<OaAttendance>();
		Date date=new Date();//取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,days);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
		date=calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String str=sdf.format(date);
        Date date1 = null;
        try {
            date1=sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        oaAttendance.setSignDate(date1);
		oaAttList = this.findList(oaAttendance);		
		if(oaAttList != null && oaAttList.size()>0){
			return oaAttList.get(0);
		}else{
			return null;
		}
	}
	
	
	@Transactional(readOnly = false)
	public void delete(OaAttendance oaAttendance) {
		super.delete(oaAttendance);
	}
	
	public Page<OaAttendanceRule> findPageByrule(Page<OaAttendanceRule> page, OaAttendanceRule rule) {
		rule.setPage(page);
		page.setList(dao.findListByrule(rule));
		return page;
	}
	
	public Page<Office> findPageByCompany(Page<Office> page, Office company) {
		company.setPage(page);
		page.setList(dao.findListByCompany(company));
		return page;
	}

    public List getCreateDateList(String companyId, String date) {
		Map<String,String> map = new HashMap<String, String>();
		map.put("companyId",companyId);
		map.put("date",date);
		return oaAttendanceDao.getCreateDateList(map);
    }

    public List<OaAttendance> findByCompany2(OaAttendance oaAttendance, String date) {
		Map<String,Object> map = new HashMap<>();
		map.put("attendance",oaAttendance);
		map.put("date",date);
		return oaAttendanceDao.findByCompany2(map);
    }


	public List<String> getCompanyList() {
		return oaAttendanceDao.getCompanyList();
	}

	public List<OaAttendance> getOaAttendanceList(String companyId,Date signDate) {
		return oaAttendanceDao.getOaAttendanceList(companyId,signDate);
	}

	public Date parseDate(){
		Date date=new Date();//取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE,-1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
		date=calendar.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String str=sdf.format(date);
		Date signDate = null;
		try {
			signDate = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return signDate;
	}

	public OaAttendance getOaAttendanceByCompanyIdAndUserIdAndSignDate(String companyId, String userId) {
		Date signDate = parseDate();
		Map<String , Object> map = new HashMap<>();
		map.put("companyId",companyId);
		map.put("userId",userId);
		map.put("signDate",signDate);
		return oaAttendanceDao.getOaAttendanceByCompanyIdAndUserIdAndSignDate(map);
	}

	public List getCreateDateList1(String time) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("userId",UserUtils.getUser().getId());
		map.put("time",time);
		return oaAttendanceDao.getCreateDateList1(map);
	}
	public List<OaAttendance> findByCompany1(String time){
		Map<String,String> map = new HashMap<String,String>();
		map.put("userId",UserUtils.getUser().getId());
		map.put("time",time);
		return oaAttendanceDao.findByCompany1(map);
	}
	public List getCreateDateListAll(String time) {
		Map<String,String> map = new HashMap<String,String>();
		OaAttendance w =new OaAttendance();
		map.put("sqlStr",dataScopeFilter(w.getCurrentUser(), "o", "u", "s", MenuStatusEnum.OA_ATTENDANCE.getValue()));
		map.put("time",time);
		return oaAttendanceDao.getCreateDateListAll(map);
	}
	public List<OaAttendance> findByCompanyAll(String time){
		Map<String,String> map = new HashMap<String,String>();
		OaAttendance w =new OaAttendance();
		map.put("sqlStr",dataScopeFilter(w.getCurrentUser(), "o", "u", "s", MenuStatusEnum.OA_ATTENDANCE.getValue()));
		map.put("time",time);
		return oaAttendanceDao.findByCompanyAll(map);
	}

	public List<OaAttendance> findByManage(Map<String,Object> map) {
		return oaAttendanceDao.findByManage(map);
	}
	public List<OaAttendance> findByCompanySelf(Map<String,Object> map) {
		return oaAttendanceDao.findByCompanySelf(map);
	}
}