package com.zhongdao.jlr.business.action;

import com.util.RequestUtil;
import com.util.Vo;
import com.zhongdao.jlr.business.service.UserInfoService;
import com.zhongdao.jlr.pojo.*;
import jodd.util.BCrypt;
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("/user")
public class UserAction {
    @Resource(name = "userInfoService")
    UserInfoService service;

    /***
     * 获取角色信息,跳转用户管理页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String JumpTocaseInfo(HttpServletRequest request, HttpServletResponse response) {
        //获取角色信息
        List<JLRRole> baseRoles=service.getRoleInfo();
        //获取经销商信息
        List<EnterpriseJlr> enterpriseJlrs=service.getEnterpriseInfo();
        //共享数据
        request.setAttribute("roles",baseRoles);
        request.setAttribute("enterprise",enterpriseJlrs);
        return "/page/user";
    }

    /***
     * 获取,跳转用户账号页面
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/myself.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public String JumpMyself(HttpServletRequest request, HttpServletResponse response) {
        return "/page/modifyPassword";
    }

    /***
     * 获取用户信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getUserInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void caseInfoList(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String name = request.getParameter("name");// 案件编号
        String phone = request.getParameter("phone");// VIN码
        String role = request.getParameter("role");// VIN码
        String username = request.getParameter("username");// VIN码
        //获取用户信息
        Vo vo = service.getUserInfo(request,name,phone,role,username);
        //将用户信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
    }

    /**
     * 用户信息添加或修改信息修改
     */
    @RequestMapping(value = "/insertUser.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void putMyEnterprise(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JLRUser jlrUser=new JLRUser();
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String name = request.getParameter("name");
        String username = request.getParameter("login_name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String role = request.getParameter("role");
        String enterprise = request.getParameter("enterprise");
        String newPassword = request.getParameter("newPassword");
        String id = request.getParameter("id");
        //将参数放入对象中
        if(!TextUtils.isEmpty(id)){
            jlrUser.setId(Integer.valueOf(id));
        }
        jlrUser.setName(name);
        jlrUser.setLogin_name(username);
        jlrUser.setPhone(phone);
        jlrUser.setEmail(email);
        jlrUser.setPassword(newPassword);
        jlrUser.setRole_id(Integer.valueOf(role));
        jlrUser.setEnterprise_id(Integer.valueOf(enterprise));
        if (jlrUser != null) {
            //添加或更改用户信息
            boolean back = service.updateUserInfo(jlrUser);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }

    /**
     * 根据id查询用户信息
     */
    @RequestMapping(value = "/getUser.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void getById(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map集合,存储返回的结果信息
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        String id = request.getParameter("id");
        if (!TextUtils.isEmpty(id)) {
            //根据id获取用户信息
            JLRUser back = service.getUser(id);
            if (back != null) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
                result.put("data", back);
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将结果信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));
    }

    /**
     * 根据编号删除用户信息
     */
    @RequestMapping(value = "/deleteUser.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void deleteById(HttpServletRequest request, HttpServletResponse response) {
        //声明一个map对象用于存放响应到客户端的数据
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String id = request.getParameter("id");
        if (!TextUtils.isEmpty(id)) {
            //根据id获取用户信息
            JLRUser user = service.getUser(id);
            //根据编号删除经用户信息
            boolean back = service.deleteUser(user);
            if (back) {
                result.put("flag", "1");
                result.put("msg", "执行成功");
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据信息响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/updatePassword.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
            RequestMethod.POST})
    public void updatePassword(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //声明一个map集合,存储返回的结果信息
        Map<String, String> result = new HashMap<String, String>();
        result.put("flag", "0");
        result.put("msg", "执行失败");
        //获取参数
        String password = request.getParameter("password");
        String newPassword = request.getParameter("newPassword");
        String confirmPassword = request.getParameter("confirmPassword");
        String id = request.getParameter("id");
        if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(newPassword) && !TextUtils.isEmpty(confirmPassword)) {
            try {
                //根据id获取用户信息
                JLRUser jlrUser=service.getUser(id);
                if(jlrUser != null){
                    //判断密码是否正确
                    boolean ps= BCrypt.checkpw(password,jlrUser.getPassword());
                    if(ps){
                        //将新密码放入对象中
                        jlrUser.setPassword(newPassword);
                        //修改密码
                        boolean back = service.updateUserInfo(jlrUser);
                        if (back) {
                            result.put("flag", "1");
                            result.put("msg", "执行成功");
                        }
                    }else{
                        throw new Exception("密码错误");
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                result.put("msg",e.getMessage());
            }
        } else {
            result.put("msg", "参数为空");
        }
        //将数据响应到客户端
        RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(result));

    }

}
