package com.happy.action;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.Admin;
import com.happy.Model.AdminManager;
import com.happy.Model.AdminPower;
import com.happy.Until.GetHttpParam;
import com.happy.Until.PwdDefind;
import com.happy.Until.ResUtil;
import com.happy.dto.IPage;
import com.happy.service.AdminService;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

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

public class adminAction extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest request;
    public HttpServletResponse response;

    @Resource
    public AdminService adminService;

    public String adminName;
    public String password;
    public Integer id;
    public int page; // 当前页
    public int rows;// 每页显示的行数rows

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public String trueStr(){
        JSONObject resultJson = new JSONObject();
        resultJson.put("message", "操作成功");
        resultJson.put("code", 200);
        resultJson.put("data", "0");
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：登录
     * @return
     */
    public String loginAdmin() {
        JSONObject resultJson = new JSONObject();
        StringBuilder getOneSqlx = new StringBuilder("");
        getOneSqlx.append(" and admin_name = '").append(adminName).append("'");
        Admin login = adminService.getOen(getOneSqlx.toString());

        StringBuilder getOneSqlx1 = new StringBuilder("");
        getOneSqlx1.append(" and admin_name = '").append(adminName).append("'")
                .append(" and password = '").append(password).append("'");
        Admin login1 = adminService.getOen(getOneSqlx1.toString());
        if (login != null) {
            if(login1 == null){
                resultJson.put("message", "用户名或密码错误");
                resultJson.put("code", 205);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            //login.setPassword("********");
            resultJson.put("message", "登录成功");
            resultJson.put("code", 200);
            resultJson.put("data", login);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "该账户不存在");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：新增管理端管理员账号
     * @return
     */
    public String insertAdmin() {
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Admin admin = null;
        try {
            admin = gson.fromJson(json.toString(), new TypeToken<Admin>() {}.getType());
            if (admin == null) {
                resultJson.put("message", "数据为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if (admin.getAdminName()==null || admin.getUserName()==null || admin.getLevel()==null || admin.getPhone()==null){
                resultJson.put("message", "各参数不能为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if (!PwdDefind.vertify(admin.getPassword())){
                resultJson.put("message", "密码至少包含：大小写英文字母、数字、特殊符号,密码长度大于8位,小于20位");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            StringBuilder getOneSqlx = new StringBuilder("");
            getOneSqlx.append(" and admin_name = '").append(admin.getAdminName()).append("'");
            Admin listc = adminService.getOen(getOneSqlx.toString());
            if (listc != null) {
                resultJson.put("message", "该用户已存在");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            int m = adminService.insertAdmin(admin);
            if (m > 0) {
                resultJson.put("message", "添加成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            } else {
                resultJson.put("message", "添加失败");
                resultJson.put("code", 502);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：修改管理端管理员账号
     * @return
     */
    public String updateAdmin() {
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Admin admin = null;
        try {
            admin = gson.fromJson(json.toString(), new TypeToken<Admin>() {}.getType());
            if (admin == null) {
                resultJson.put("message", "数据为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            if(admin.getId()==null){
                resultJson.put("message", "修改Id不能为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
//            if (admin.getAdminName()==null || admin.getUserName()==null || admin.getLevel()==null || admin.getPhone()==null){
//                resultJson.put("message", "各参数不能为空");
//                resultJson.put("code", 500);
//                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
//                return null;
//            }
            if(admin.getPassword()!=null && !"".equals(admin.getPassword())){
                if (!PwdDefind.vertify(admin.getPassword())){
                    resultJson.put("message", "密码至少包含：大小写英文字母、数字、特殊符号,密码长度大于8位,小于20位");
                    resultJson.put("code", 500);
                    ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                    return null;
                }
            }
            StringBuilder getOneSqlx = new StringBuilder("");
            getOneSqlx.append(" and admin_name = '").append(admin.getAdminName()).append("'")
                    .append(" and id != '").append(admin.getId()).append("'");
            Admin listc = adminService.getOen(getOneSqlx.toString());
            if (listc != null) {
                resultJson.put("message", "该用户已存在");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            int m = adminService.updateAdmin(admin);
            if (m > 0) {
                resultJson.put("message", "修改成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            } else {
                resultJson.put("message", "修改失败");
                resultJson.put("code", 502);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：删除
     * @return
     */
    public String delAdmin(){
        JSONObject resultJson = new JSONObject();
        if (id == null) {
            resultJson.put("message", "请传入id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        int m = adminService.delAdmin(id);
        if (m > 0) {
            resultJson.put("message", "删除成功");
            resultJson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：重置密码
     * @return
     */
    public String removePwd(){
        JSONObject resultJson = new JSONObject();
        if (id == null) {
            resultJson.put("message", "请传入id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Admin admin = adminService.getById(id);
        if (admin != null && !"".equals(admin)) {
            admin.setPassword("Jams_123456");
            int m = adminService.updateAdmin(admin);
            if(m > 0){
                resultJson.put("message", "重置成功");
                resultJson.put("code", 200);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }else{
                resultJson.put("message", "重置失败");
                resultJson.put("code", 502);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }


    /**
     * 描述：分页查询
     * @return
     */
    public String queryPage(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (adminName!=null){
            s1.append(" and (admin_name like '%").append(adminName).append("%'")
                .append(" or phone like '%").append(adminName).append("%'")
                .append(" or user_name like '%").append(adminName).append("%')");
        }
        IPage<Admin> adminIPage = adminService.queryPage(s1.toString(),page,rows);
        if(adminIPage!=null && !"".equals(adminIPage)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", adminIPage);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }else{
            resultJson.put("message", "查询分页失败");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        return null;
    }

    /**
     * 描述：根据Id查询详细
     * @return
     */
    public String getById(){
        JSONObject resultJson = new JSONObject();
        if (id == null) {
            resultJson.put("message", "请传入id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        Admin admin = adminService.getById(id);
        if (admin != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", admin);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }else{
            resultJson.put("message", "未查到用户信息");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

    /**
     * 描述：查询菜单
     * @return
     */
    public String getIndexList(){
        JSONObject resultjson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (id==null){
            resultjson.put("message", "请传入用户Id");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        Admin admin = adminService.getById(id);
        if (admin==null){
            resultjson.put("message", "未查到用户信息");
            resultjson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        String ids = admin.getAdminPower();
        s1.append(" and id in (").append(ids).append(") ");
        List<AdminPower> lpower = adminService.queryTree(s1.toString());
        if (lpower!=null){
            resultjson.put("message", "返回成功");
            resultjson.put("code", 200);
            resultjson.put("data",lpower);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
            return null;
        }
        resultjson.put("message", "数据为空");
        resultjson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
        return null;
    }

}
