package com.jeeplus.modules.API.works.manageCalendar;

import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.modules.iim.entity.MyCalendar;
import com.jeeplus.modules.oa.service.ManageCalendarService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.utils.ErrorCode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.*;
import java.util.logging.Logger;

/**
 * Created by Meng on 2017/6/20.
 */
@Controller
@RequestMapping(value = "${frontPath}/work/manageCalendar")
public class manageCalendarController {

    @Autowired
    ManageCalendarService manageCalendarService;

    /**
     * 本月所有拥有报告的日期
     *
     * @return
     * @throws java.text.ParseException
     */
    @ResponseBody
    @RequestMapping(value = "getManageCalendarByMonth", method = RequestMethod.GET)
    public AjaxJson getManageCalendarByMonth(HttpServletRequest request) throws ParseException {
        AjaxJson json = new AjaxJson();
        String userId = request.getParameter("userId");
        String officeId = request.getParameter("officeId");
        String date = request.getParameter("date");
        User user;
        if(UserUtils.getUser()!=null){
            user =UserUtils.getUser();
        }else {
            user =UserUtils.get(userId);
        }
        if(!UserUtils.isManager()){
            json.setMsg("当前用户在本公司非管理员，您无权查看管理日志");
            json.setErrorCode(ErrorCode.code_1010);
            json.setSuccess(false);
            return json;
        }
        List list =  manageCalendarService.getManageCalendarByMonth(date);
        json.put("data",list);
        json.setErrorCode(ErrorCode.code_1004);
        return json;
    }

    /**
     * 指定日期所有报告
     *
     * @return
     * @throws java.text.ParseException
     */
    @ResponseBody
    @RequestMapping(value = "getManageCalendarByDate", method = RequestMethod.GET)
    public AjaxJson getManageCalendarByDate(HttpServletRequest request) throws ParseException {
        AjaxJson json = new AjaxJson();
        String userId = request.getParameter("userId");

        String date = request.getParameter("date");
        User user = UserUtils.getUser();
        if(user == null || user.getId() ==null){
            user = UserUtils.get(userId);
        }else{
        }
        try{
            if(!UserUtils.isManager()){
                json.setMsg("当前用户在本公司非管理员，您无权查看管理日志");
                json.setErrorCode(ErrorCode.code_1010);
                json.setSuccess(false);
                return json;
            }
        }catch (Exception e){
            json.setMsg("当前用户在本公司非管理员，您无权查看管理日志");
            json.setErrorCode(ErrorCode.code_1010);
            json.setSuccess(false);
            return json;
        }
        List list =  manageCalendarService.getReportUserAppList(date);
        json.put("data",list);
        json.setErrorCode(ErrorCode.code_1004);
        return json;
    }

    /**
     * 指定日期所有用户
     *
     * @return
     * @throws java.text.ParseException
     */
    @ResponseBody
    @RequestMapping(value = "getUsersByDate", method = RequestMethod.GET)
    public AjaxJson getUsersByDate(HttpServletRequest request) throws ParseException {
        AjaxJson json = new AjaxJson();
        String userId = request.getParameter("userId");
        String count = request.getParameter("count");
        String date = request.getParameter("date");
        User user = UserUtils.getUser();
        if(user == null || user.getId() ==null){
            user = UserUtils.get(userId);
        }else{
        }
        try{
            if(!UserUtils.isManager()){
                json.setMsg("当前用户在本公司非管理员，您无权查看管理日志");
                json.setErrorCode(ErrorCode.code_1010);
                json.setSuccess(false);
                return json;
            }
        }catch (Exception e){
            json.setMsg("当前用户在本公司非管理员，您无权查看管理日志");
            json.setErrorCode(ErrorCode.code_1010);
            json.setSuccess(false);
            return json;
        }

        List list =  manageCalendarService.getReportUserAppList(date);
        if(count.equals("1")){
            Map map = (Map)list.get(0);
            Map map1 = (Map)map.get("workReports");
            List<Map> mapList1 =  (List)map1.get("workReport");
            List list1 = new ArrayList();
            json.put("data",mapList1);
        }else if(count.equals("2")){
            Map map = (Map)list.get(1);
            Map map1 = (Map)map.get("workOutSignIns");
            List<Map> mapList1 =  (List)map1.get("workOutSignIn");
            json.put("data",mapList1);
        }else if(count.equals("3")){
            Map map = (Map)list.get(2);
            List list1 = new ArrayList();
            Map map1 = (Map)map.get("attendances");
            List<Map> mapList1 =  (List)map1.get("late");
            List<Map> mapList2 =  (List)map1.get("onDuty");
            List<Map> mapList3 =  (List)map1.get("leaveEarly");
            List<Map> mapList4 =  (List)map1.get("offDut");
            if(mapList1!=null && mapList1.size()!=0){
                for (Map m:mapList1){
                    list1.add(m);
                }
            }
            if(mapList2!=null && mapList2.size()!=0){
                for (Map m:mapList2){
                    list1.add(m);
                }
            }
            if(mapList3!=null && mapList3.size()!=0){
                for (Map m:mapList3){
                    list1.add(m);
                }
            }
            if(mapList4!=null && mapList4.size()!=0){
                for (Map m:mapList4){
                    list1.add(m);
                }
            }
            json.put("data",list1);
        }else if(count.equals("4")){
            Map map = (Map)list.get(3);
            List list1 = new ArrayList();
            Map map1 = (Map)map.get("audits");
            List<Map> mapList1 =  (List)map1.get("workSealFrom");
            List<Map> mapList2 =  (List)map1.get("leave");
            List<Map> mapList3 =  (List)map1.get("oaEvection");
            List<Map> mapList4 =  (List)map1.get("workOvertimeForm");
            if(mapList1!=null && mapList1.size()!=0){
                for (Map m:mapList1){
                    list1.add(m);
                }
            }
            if(mapList2!=null && mapList2.size()!=0){
                for (Map m:mapList2){
                    list1.add(m);
                }
            }
            if(mapList3!=null && mapList3.size()!=0){
                for (Map m:mapList3){
                    list1.add(m);
                }
            }
            if(mapList4!=null && mapList4.size()!=0){
                for (Map m:mapList4){
                    list1.add(m);
                }
            }
            json.put("data",list1);
        }else if(count.equals("31")){
            Map map = (Map)list.get(2);
            Map map1 = (Map)map.get("attendances");
            List<Map> mapList =  (List)map1.get("late");
            json.put("data",mapList);
        }else if(count.equals("32")){
            Map map = (Map)list.get(2);
            Map map1 = (Map)map.get("attendances");
            List<Map> mapList =  (List)map1.get("onDuty");
            json.put("data",mapList);
        }else if(count.equals("33")){
            Map map = (Map)list.get(2);
            Map map1 = (Map)map.get("attendances");
            List<Map> mapList = (List)map1.get("leaveEarly");
            json.put("data",mapList);
        }else if(count.equals("34")){
            Map map = (Map)list.get(2);
            Map map1 = (Map)map.get("attendances");
            List<Map> mapList = (List)map1.get("offDut");
            json.put("data",mapList);
        }else if(count.equals("41")){
            Map map = (Map)list.get(3);
            Map map1 = (Map)map.get("audits");
            List<Map> mapList = (List)map1.get("workSealFrom");
            json.put("data",mapList);
        }else if(count.equals("42")){
            Map map = (Map)list.get(3);
            Map map1 = (Map)map.get("audits");
            List<Map> mapList = (List)map1.get("leave");
            json.put("data",mapList);
        }else if(count.equals("43")){
            Map map = (Map)list.get(3);
            Map map1 = (Map)map.get("audits");
            List<Map> mapList = (List)map1.get("oaEvection");
            json.put("data",mapList);
        }else if(count.equals("44")){
            Map map = (Map)list.get(3);
            Map map1 = (Map)map.get("audits");
            List<Map> mapList = (List)map1.get("workOvertimeForm");
            json.put("data",mapList);
        }else if(count.equals("45")){
            Map map = (Map)list.get(3);
            Map map1 = (Map)map.get("audits");
            List<Map> mapList = (List)map1.get("workGoOut");
            json.put("data",mapList);
        }
        json.setErrorCode(ErrorCode.code_1004);
        return json;
    }

}
