package com.cnd3b.restcontroller.enterprise.salerclient.usercenter.usermsg;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
import com.cnd3b.common.D3bException;
import com.cnd3b.common.data.Row;
import com.cnd3b.common.data.Rows;
import com.cnd3b.common.data.SQLFactory;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.Date;

public class usermsg extends Controller {
    public usermsg(JSONObject content) {
        super(content);
    }

    /**
     * 个人信息详情查询
     *
     * @return
     */
    public String query_usermsg() {
        SQLFactory sqlFactory = new SQLFactory(this, "用户信息查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("userid", userid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            SQLFactory sqlFactory2 = new SQLFactory(this, "用户版本授权情况查询");
            sqlFactory2.addParameter("siteid", siteid);
            sqlFactory2.addParameter("userid", userid);
            Rows rows2 = dbConnect.runSqlQuery(sqlFactory2.getSQL());
            if (rows2.isEmpty()) {
                row.put("fappmodel", "");
                row.put("fauthenddate", "");
            } else {
                row.put("fappmodel", rows2.get(0).getString("fappmodel"));
                row.put("fauthenddate", rows2.get(0).getString("fauthenddate"));
            }
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 个人信息修改
     *
     * @return
     */
    public String update_usermsg() throws D3bException, P2Exception {
        String fname = content.getString("fname", "tenterprise_users.fname", "姓名");//姓名
        String fsex = content.getString("fsex");//性别
        Date fbirthdate = content.getDate("fbirthdate");//出生日期
        String femail = content.getString("femail", "tenterprise_users.femail", "邮箱");//邮箱
        String fwechatno = content.getString("fwechatno", "tenterprise_users.fwechatno", "微信");//微信号

        PaoSetRemote tenterprise_usersSet = getP2ServerSystemPaoSet("tenterprise_users", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "'");
        if (!tenterprise_usersSet.isEmpty()) {
            PaoRemote pao = tenterprise_usersSet.getPao(0);
            pao.setValue("fname", fname, 11L);
            pao.setValue("fsex", fsex, 11L);
            pao.setValue("fbirthdate", fbirthdate, 11L);
            pao.setValue("femail", femail, 11L);
            pao.setValue("fwechatno", fwechatno, 11L);
            pao.setValue("changeby", username, 11L);
            pao.setValue("changedate", getDateTime(), 11L);
            tenterprise_usersSet.save();
            tenterprise_usersSet.close();
        } else {
            return getErrReturnObject().setErrMsg("找不到账号信息").toString();
        }
        return query_usermsg();
    }
}
