package com.cnd3b.restcontroller.publicmethod.archives_ad;

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

import java.util.Random;

public class archives_ad extends Controller {
    /**
     * 构造函数
     *
     * @param content
     */
    public archives_ad(JSONObject content) {
        super(content);
    }

    /**
     * 推广素材分享页面信息查询
     *
     * @return
     */
    public String query_archiveforshare() throws P2Exception {
        String tarchives_adid = content.getString("tarchives_adid");
        String siteid = content.getString("siteid");

        if (userid == 0 || siteid == null || "".equals(siteid) || "null".equalsIgnoreCase(siteid) || tarchives_adid == null || "".equals(tarchives_adid) || "null".equalsIgnoreCase(tarchives_adid)) {
            return getErrReturnObject().setErrMsg("无效的分享链接").toString();
        }

        String fchannel = "";
        if (content.containsKey("fchannel")) {
            fchannel = content.getString("fchannel");
        }

        SQLFactory sqlFactory = new SQLFactory(this, "分享推广素材信息查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tarchives_adid", tarchives_adid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            SQLFactory docfactory = new SQLFactory(this, "分享推广素材附件查询");
            docfactory.addParameter("siteid", siteid);
            docfactory.addParameter("tarchives_adid", tarchives_adid);
            Rows docrows = dbConnect.runSqlQuery(docfactory.getSQL());
            row.put("docinfos", docrows);

            SQLFactory userfactory = new SQLFactory(this, "分享推广人员信息查询");
            userfactory.addParameter("siteid", siteid);
            userfactory.addParameter("tenterprise_userid", userid);
            Rows userrows = dbConnect.runSqlQuery(userfactory.getSQL());
            row.put("userinfo", userrows);
        }
        if (content.containsKey("wechat_code") && !"".equals(content.getString("wechat_code"))) {
            addArchiveShareForm(siteid, tarchives_adid, content.getString("wechat_code"), fchannel);
        }
        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 推广素材分享客户信息记录
     *
     * @return
     */
    private String addArchiveShareForm(String siteid, String tarchives_adid, String wechat_code, String fchannel) throws P2Exception {
        WechatDock_gzh wechatDockGzh = new WechatDock_gzh();
        WechatUser user = wechatDockGzh.getCustMsg(wechat_code);
        String wechat_openid = user.getOpenid();
        if (user.getOpenid() != null) {
            PaoSetRemote tarchives_ad_shareformSet = getP2ServerSystemPaoSet("tarchives_ad_shareform", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and fchannel='" + fchannel + "' and tarchives_adid='" + tarchives_adid + "' and wechat_openid='" + wechat_openid + "'");
            long tarchives_ad_shaid = 0L;
            PaoRemote tarchives_ad_shareform = null;
            if (tarchives_ad_shareformSet.isEmpty()) {
                tarchives_ad_shareform = tarchives_ad_shareformSet.addAtEnd();
                tarchives_ad_shareform.setValue("siteid", siteid, 11L);//企业ID
                tarchives_ad_shareform.setValue("tenterprise_userid", userid, 11L);//分享用户ID
                tarchives_ad_shareform.setValue("tarchives_adid", tarchives_adid, 11L);//推广素材ID
                tarchives_ad_shareform.setValue("fchannel", fchannel, 11L);//分享渠道
                tarchives_ad_shareform.setValue("createdate", getDateTime(), 11L);//分享时间
                tarchives_ad_shareform.setValue("fvisitors", 1, 11L);
                tarchives_ad_shareform.setValue("wechat_openid", wechat_openid, 11L);

                tarchives_ad_shareform.setValue("fprovince", user.getProvince(), 11L);
                tarchives_ad_shareform.setValue("fcity", user.getCity(), 11L);
                tarchives_ad_shareform.setValue("fcounty", user.getCountry(), 11L);
                tarchives_ad_shareform.setValue("fname", user.getNickname(), 11L);
                tarchives_ad_shareform.setValue("fphonenumber", "", 11L);
                tarchives_ad_shareform.setValue("changedate", getDateTime(), 11L);
                tarchives_ad_shareform.setValue("fvisitors", 1, 11L);

                tarchives_ad_shareform.setValue("freadtimes", (new Random().nextInt(5) + 1) * 60);
                tarchives_ad_shaid = tarchives_ad_shareform.getUniqueIDValue();
            } else {
                tarchives_ad_shareform = tarchives_ad_shareformSet.getPao(0);

                tarchives_ad_shareform.setValue("changedate", getDateTime(), 11L);
                tarchives_ad_shareform.setValue("fvisitors", tarchives_ad_shareform.getInt("fvisitors") + 1, 11L);
                int freadtimes = tarchives_ad_shareform.getInt("freadtimes");
                tarchives_ad_shareform.setValue("freadtimes", freadtimes + (new Random().nextInt(5) + 1) * 60);
            }
            tarchives_ad_shareformSet.save();

            if (tarchives_ad_shaid > 0) {
                SQLFactory messgae = new SQLFactory(this, "推广素材分享用户阅读消息提醒");
                messgae.addParameter("siteid", siteid);
                messgae.addParameter("tenterprise_userid", userid);
                messgae.addParameter("tarchives_adid", tarchives_adid);
                messgae.addParameter("tarchives_ad_shaid", tarchives_ad_shaid);
                dbConnect.runSqlUpdate(messgae.getSQL());
            }
        }
        return getSucReturnObject().toString();
    }
}
