package com.cnd3b.restcontroller.system.message;

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;

public class Message extends Controller {

    public Message(JSONObject content) {
        super(content);
    }

    public String queryMessage() {
        SQLFactory factory = null;
        if (content.containsKey("ftype") && !"".equals(content.getString("ftype"))) {
            factory = new SQLFactory(this, "消息查询分类", pageSize, pageNumber, "fisread,createdate desc");
            factory.addParameter("ftype", content.getString("ftype"));
        } else {
            factory = new SQLFactory(this, "消息查询全部", pageSize, pageNumber, "fisread,createdate desc");
        }
        factory.addParameter("siteid", siteid);
        factory.addParameter("tenterprise_userid", userid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows).setFinalDo(DataPool.defaultdatalife, 1);
    }

    public String readMessage() {
        SQLFactory factory = new SQLFactory(this, "消息阅读");
        factory.addParameter("siteid", siteid);
        factory.addParameter("tenterprise_userid", userid);
        factory.addParameter("tmessageid", content.getString("tmessageid"));
        String status = dbConnect.runSqlUpdate(factory.getSQL());
        if ("true".equals(status)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(status).toString();
        }
    }
    /**
     * 消息发送
     *
     * @param userid   用户ID，传0表示全部用户
     * @param ftype    消息类型，素材｜系统
     * @param fmessage 消息内容
     */
    public String createMessage(Long userid, String ftype, String fmessage) {
        SQLFactory factory = new SQLFactory(this, "消息插入");
        factory.addParameter("siteid", siteid);
        factory.addParameter("ftype", ftype);
        factory.addParameter("fmessage", fmessage);
        factory.addParameter("tenterprise_userid", userid);
        return dbConnect.runSqlUpdate(factory.getSQL());
    }
}
