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

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeeplus.modules.utils.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.workoutsignin.dao.WorkOutSignInDao;
import com.jeeplus.modules.workoutsignin.entity.WorkOutSignIn;
import com.jeeplus.modules.workoutsignin.service.WorkOutSignInService;

@Controller
@RequestMapping(value = "${frontPath}/work/workOutSignIn")
public class WorkOutSignInInterfaceController extends BaseController {
	
	@Autowired
	private WorkOutSignInService workOutSignInService;

	@Autowired
	private WorkOutSignInDao workOutSignInDao;

	/**
	 * 外勤签到
	 */
	@ResponseBody
	@RequestMapping(value = "saveSignIn", method = RequestMethod.POST)
	public AjaxJson saveSignIn(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		AjaxJson j = new AjaxJson();
		HashMap<String,String> requestMap = findRequestMap(request);
		String location = requestMap.get("location");
		String locationInfo = requestMap.get("locationInfo");
		String longitude = requestMap.get("longitude");
		String latitude = requestMap.get("latitude");
		User user = UserUtils.getUser();
		WorkOutSignIn workOutSignIn = new WorkOutSignIn();
		workOutSignIn.setUser(user);
		workOutSignIn.setLocationName(location);
		workOutSignIn.setLocationInfo(locationInfo);
		workOutSignIn.setLatitude(latitude);
		workOutSignIn.setLongitude(longitude);
		workOutSignIn.setCompanyId(UserUtils.getSelectCompany().getId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		workOutSignInService.save(workOutSignIn);
		j.setMsg("签到成功");
        j.setErrorCode(ErrorCode.code_1004);
		return j;
	}

	/**
	 * 历史足迹
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getSignInHistory", method = RequestMethod.GET)
	public AjaxJson getSignInHistory(HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String pageNo = request.getParameter("pageNo");
            User user = UserUtils.getUser();
            WorkOutSignIn workOutSignIn = new WorkOutSignIn();
            workOutSignIn.setUser(user);
            workOutSignIn.setCompanyId(UserUtils.getSelectCompany().getId());
            Page<WorkOutSignIn> page = workOutSignInService.findPage(new Page<WorkOutSignIn>(request, response), workOutSignIn);
            List<WorkOutSignIn> list = page.getList();
            List<Map<String, Object>> mapList = Lists.newArrayList();
            for (WorkOutSignIn work : list) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("location", work.getLocationName());
                map.put("locationInfo", work.getLocationInfo());
                map.put("time", sdf.format(work.getCreateDate()));
                mapList.add(map);
            }
            if (pageNo == null || pageNo.equals("")) {
                j.put("data", Lists.newArrayList());
                j.setMsg("获取历史足迹失败");
                j.setSuccess(false);
                j.setErrorCode(ErrorCode.code_2005);
            } else {
                if (mapList == null) {
                    j.put("data", Lists.newArrayList());
                    j.setMsg("获取历史足迹成功");
                    j.setSuccess(true);
                    j.setErrorCode(ErrorCode.code_1004);
                } else {
                    if (Integer.parseInt(pageNo) <= page.getTotalPage()) {
                        j.setMsg("获取历史足迹成功");
                        j.put("data", mapList);
                        j.setErrorCode(ErrorCode.code_1004);
                    } else {
                        j.setMsg("获取历史足迹成功");
                        j.put("data", Lists.newArrayList());
                        j.setErrorCode(ErrorCode.code_1004);
                    }
                }
            }
        }catch (Exception e){
            j.put("data", Lists.newArrayList());
            j.setMsg("获取历史足迹失败");
            j.setSuccess(false);
            j.setErrorCode(ErrorCode.code_2004);
        }
		return j;
	}

}
