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

import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.JPushClientUtil;
import com.jeeplus.modules.iim.dao.MyCalendarDao;
import com.jeeplus.modules.iim.entity.MyCalendar;
import com.jeeplus.modules.pushinfo.entity.Pushinfo;
import com.jeeplus.modules.pushinfo.service.PushinfoService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日历Service
 * 
 * @author liugf
 * @version 2016-04-19
 */
@Service
@Transactional(readOnly = true)
public class MyCalendarService extends CrudService<MyCalendarDao, MyCalendar> {
	
	@Autowired
	private MyCalendarDao myCalendarDao;
    @Autowired
    private PushinfoService pushinfoService;

	public MyCalendar get(String id) {
		return super.get(id);
	}

	public List<MyCalendar> getByUserIdCount(MyCalendar myCalendar) {
		return dao.getByUserIdCount(myCalendar);
	}

	public List<MyCalendar> findList(MyCalendar myCalendar) {
		return super.findList(myCalendar);
	}

	public Page<MyCalendar> findPage(Page<MyCalendar> page, MyCalendar myCalendar) {
		return super.findPage(page, myCalendar);
	}

	public List<MyCalendar> findListByDate(MyCalendar myCalendar){
		return myCalendarDao.findListByDate(myCalendar);
		
	}
	
	@Transactional(readOnly = false)
	public void save(MyCalendar myCalendar) {
		super.save(myCalendar);
	}
	//pc端
    //保存修改日历并发推送
	@Transactional(readOnly = false)
    public void saveCalender(MyCalendar myCalendar){
        super.save(myCalendar);
        Map<String,String> map = new HashMap<>();
        map.put("id",myCalendar.getId());
        map.put("type","5001");
        String title = myCalendar.getTitle();
        String content = "您有一个新的日程即将开始";
        String remarks = myCalendar.getStart()+" -- "+myCalendar.getEnd()+"  "+title;
        String alias = myCalendar.getUser().getId();
        String time = myCalendar.getNotice()+":00";
        JPushClientUtil.sendNotificationToAliasByTime(title,content,map,alias,time);
        Pushinfo pushinfo = new Pushinfo();
        pushinfo.setCurrentUser(UserUtils.getUser());
        pushinfo.setRemarks(remarks);
        pushinfo.setUserId(UserUtils.getUser().getId());
        pushinfo.setType("5001");
        pushinfo.setPushId(myCalendar.getId());
        pushinfo.setTitle("日程");
        pushinfo.setContent(content);
        pushinfo.setPushUserId(UserUtils.getUser().getId());
        pushinfo.setParentType("singleNowCalendar");
        pushinfo.setMobile("ios,android");
        pushinfo.setCompanyId(myCalendar.getCompanyId());
        pushinfoService.save(pushinfo);
    }


	@Transactional(readOnly = false)
	public void delete(MyCalendar myCalendar) {
		super.delete(myCalendar);
	}

	//APP端
	@Transactional(readOnly = false)
	public MyCalendar saveCalendar(HashMap<String, String> requestMap) throws Exception{
//		String userId = requestMap.get("userId");
        String calendarId = URLDecoder.decode(requestMap.get("calendarId")==null?"":requestMap.get("calendarId"), "UTF-8");
        String title = URLDecoder.decode(requestMap.get("title")==null?"":requestMap.get("title"), "UTF-8");
        String location = URLDecoder.decode(requestMap.get("location")==null?"":requestMap.get("location"), "UTF-8");
        String startTime = URLDecoder.decode(requestMap.get("startTime")==null?"":requestMap.get("startTime"), "UTF-8");
        String endTime = URLDecoder.decode(requestMap.get("endTime")==null?"":requestMap.get("endTime"), "UTF-8");
        String noticeTime = URLDecoder.decode(requestMap.get("noticeTime")==null?"":requestMap.get("noticeTime"), "UTF-8");
        String companyId = UserUtils.getSelectCompany().getId();
        MyCalendar myCalendar = new MyCalendar();
		try {
            User user = UserUtils.getUser();
            if(calendarId != null && !calendarId.equals("")){
                myCalendar =super.get(calendarId);
            }
            myCalendar.setUser(user);
            myCalendar.setTitle(title);
            myCalendar.setLocation(location);
            myCalendar.setStart(startTime);
            myCalendar.setEnd(endTime);
            myCalendar.setNotice(noticeTime);
            myCalendar.setCompanyId(companyId);
            myCalendar.setOfficeId(UserUtils.getSelectOffice().getId());
            super.save(myCalendar);
            Map extras = new HashMap();
            String content="您有一个日程即将开始";
            extras.put("id", myCalendar.getId());
            extras.put("type","5001");
            String remarks = startTime + "--" + endTime + " "+title;
            //UserUtils.getSelectCompany();
            String aliase = user.getId();
            String time = noticeTime+":00";
            JPushClientUtil.sendNotificationToAliasByTime(title, content, extras, aliase, time);
            Pushinfo pushinfo = new Pushinfo();
            pushinfo.setCurrentUser(UserUtils.getUser());
            pushinfo.setRemarks(remarks);
            pushinfo.setUserId(UserUtils.getUser().getId());
            pushinfo.setType("5001");
            pushinfo.setPushId(myCalendar.getId());
            pushinfo.setTitle("日程");
            pushinfo.setContent(content);
            pushinfo.setPushUserId(UserUtils.getUser().getId());
            pushinfo.setParentType("singleNowCalendar");
            pushinfo.setMobile("ios,android");
            pushinfo.setCompanyId(companyId);
            pushinfoService.save(pushinfo);
            return myCalendar;
        }catch (Exception e){
            return myCalendar;
        }

	}

}