package com.zhongdao.jlr.business.action;


import com.util.RequestUtil;
import com.zhongdao.jlr.business.service.BaseService;
import com.zhongdao.jlr.pojo.BaseMenu;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 用户登录控制器
 */

@Controller
@RequestMapping("/base")
public class BaseAction {

    @Resource(name = "baseServiceImpl")
    BaseService baseService;

    /***
     * 用户登录
     * @param request
     * @param response
     */
    @RequestMapping(value = "/login.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void login(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Map result = new HashMap();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            result.put("msg", "必填参数不能为空");
            RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
            return;
        }
        Map map = baseService.login(request, result, username, password);
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(map));
    }

    /***
     * 展示权限列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/home.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String home(HttpServletRequest request, HttpServletResponse response) {
        //获取菜单信息
        List<BaseMenu> list=baseService.getMenuInfo();
        //获取父级菜单信息
        List<BaseMenu> lists=baseService.getParentMenuInfo();
        //共享数据
        request.setAttribute("firstPage", 1);
        request.getSession().setAttribute("menu", list);
        request.getSession().setAttribute("parentMenu", lists);
        return "/page/home";
    }

    /***
     * 用户注销
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/loginout.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String loginout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return "/login";
    }

    /**
     * 地图定位
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/amap.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String amap(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String lngStr = request.getParameter("lngStr");
        //将参数类型转换为double类型
        Double lng = Double.valueOf(TextUtils.isBlank(lngStr) ? 0 : Double.valueOf(lngStr));
        String StrLat = request.getParameter("lat");
        Double lat = Double.valueOf(TextUtils.isBlank(StrLat) ? 0 : Double.valueOf(StrLat));
        //共享数据
        request.setAttribute("lng", lng);
        request.setAttribute("lat", lat);
        return "/page/amap/amap";
    }
}
