package com.cnd3b.restcontroller.system.users;

import com.cnd3b.utility.Encryption;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;
import com.cnd3b.common.parameter.parameter;
import com.cnd3b.utility.Sms;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.Calendar;
import java.util.Random;

public class Users extends Controller {
    /**
     * @param content
     */
    public Users(JSONObject content) {
        super(content);
    }

    public String changePassWord() throws P2Exception {
        String password = content.getString("password");//前端进行md5加密
        String newpassword = content.getString("newpassword");//前端进行md5加密
        if (password.equals(newpassword)) {
            return getErrReturnObject().setErrMsg("新密码和原密码不可相同").toString();
        }

        PaoSetRemote tenterprise_usersSet = getP2ServerSystemPaoSet("tenterprise_users", "tenterprise_userid='" + userid + "' and siteid='" + siteid + "' and fpassword='" + password + "'");
        if (!tenterprise_usersSet.isEmpty()) {
            tenterprise_usersSet.getPao(0).setValue("fpassword", newpassword, 11L);
            tenterprise_usersSet.save();
        } else {
            return getErrReturnObject().setErrMsg("用户名密码错误").toString();
        }
        return getSucReturnObject().toString();
    }

    public String getChangePhonenumberPassword() {
        String phonenumber = content.getString("newphonenumber");
        String password = createPassWord();
        parameter.changephonenumber_passwordmap.put(phonenumber, password);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 5);
        parameter.changephonenumber_date.put(phonenumber, calendar.getTime());

        if (parameter.isdebug()) {
            JSONObject object = new JSONObject();
            object.put("code", 1);
            object.put("msg", "验证码：" + password);
            return object.toString();
        } else {
            JSONObject object = new JSONObject();
            Sms sms = new Sms();
            sms.sendOutMsg(phonenumber, password);
            object.put("code", 1);
            object.put("msg", "验证码已发送，请注意查收！");
            return object.toString();
        }
    }

    private String createPassWord() {
        String allChar = "1234567890";
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        if (parameter.changephonenumber_passwordmap.containsValue(sb.toString())) {
            return createPassWord();
        } else {
            return sb.toString();
        }
    }

    /**
     * 修改手机号
     *
     * @return
     */
    public String changePhonenumber() throws P2Exception {
        String password = content.getString("password");//手机短信验证码
        String phonenumber = content.getString("newphonenumber");//新手机号

        int resultcode;
        if (!parameter.changephonenumber_passwordmap.containsKey(phonenumber)) {
            resultcode = 1;//没有获取验证码
        } else if (parameter.changephonenumber_date.get(phonenumber).before(Calendar.getInstance().getTime())) {
            resultcode = 2;//验证码已失效
        } else {
            //系统验证码
            String syspassword = parameter.changephonenumber_passwordmap.get(phonenumber);
            if (password.equals(new Encryption().Encode_MD5(syspassword))) {
                resultcode = 0;//验证码正确
            } else {
                resultcode = 3;//验证码错误
            }
        }
        if (resultcode == 0) {
            PaoSetRemote tenterprise_usersSet = getP2ServerSystemPaoSet("tenterprise_users", "tenterprise_userid='" + userid + "' and siteid='" + siteid + "'");
            if (!tenterprise_usersSet.isEmpty()) {
                tenterprise_usersSet.getPao(0).setValue("fphonenumber", phonenumber, 11L);
                tenterprise_usersSet.save();
            } else {
                return getErrReturnObject().setErrMsg("找不到当前账号信息").toString();
            }
            return getSucReturnObject().toString();
        } else {
            String msg = "";
            if (resultcode == 1) {
                msg = "请先获取所填手机号验证码！";
            } else if (resultcode == 2) {
                msg = "验证码已失效，请重新获取！";
            } else if (resultcode == 3) {
                msg = "无效的验证码！";
            }
            JSONObject object = new JSONObject();
            object.put("code", 0);
            object.put("msg", msg);
            return object.toString();
        }
    }


    /**
     * 下级用户查询，只返回下属一级
     *
     * @return
     */
    public String getSubUserList() {
        SQLFactory sqlFactory = new SQLFactory(this, "下级账号查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("fparentid", userid);
        Rows rows = getSubUserRows();
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

}
