package com.cnd3b.restcontroller.enterprise.salerclient.mainpage.archives;

import com.alibaba.fastjson.JSONArray;
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.RowsMap;
import com.cnd3b.common.data.SQLFactory;
import com.cnd3b.common.data.db.DataPool;
import p2.pao.PaoRemote;
import p2.pao.PaoSetRemote;
import p2.util.P2Exception;

import java.util.Calendar;

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

    /**
     * 推广素材列表查询
     *
     * @return
     */
    public String query_archivesList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.checkdate", "t1.fvisitors", "t1.fshares"};
        String sort = getSort(sortfield, "t1.checkdate desc");

        /**
         * 过滤条件设置
         */
        StringBuffer where = new StringBuffer(" 1=1 ");
        if (content.containsKey("where")) {
            JSONObject whereObject = content.getJSONObject("where");
            if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
                where.append(" and(");
                where.append("t1.ftitle like'%").append(whereObject.getString("condition")).append("%' ");
                where.append("or t1.fnotes like'%").append(whereObject.getString("condition")).append("%' ");
                where.append(")");
            }
            if (whereObject.containsKey("fclass1") && !"".equals(whereObject.getString("fclass1"))) {
                where.append(" and fclass1=").append("'").append(whereObject.getString("fclass1")).append("'");
            }
            if (whereObject.containsKey("fclass2") && !"".equals(whereObject.getString("fclass2"))) {
                where.append(" and fclass2=").append("'").append(whereObject.getString("fclass2")).append("'");
            }
            if (whereObject.containsKey("fmycollection") && whereObject.getBooleanValue("fmycollection")) {
                where.append(" and t2.tarchives_ad_colid is not null");
            }
        }
        SQLFactory factory = new SQLFactory(this, "推广素材列表查询", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tenterprise_userid", userid);
        factory.addParameter_SQL("where", where);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());

        String[] tarchives_adids = rows.toArray("tarchives_adid");

        SQLFactory contentfactory = new SQLFactory(this, "推广素材列表查询_富文本内容查询");
        contentfactory.addParameter("siteid", siteid);
        contentfactory.addParameter_in("tarchives_adid", tarchives_adids);
        RowsMap contentrowsmap = dbConnect.runSqlQuery(factory.getSQL()).toRowsMap("tarchives_adid");
        for (Row row : rows) {
            String tarchives_adid = row.getString("tarchives_adid");
            if (contentrowsmap.containsKey(tarchives_adid)) {
                row.put("fcontent", contentrowsmap.get(tarchives_adid).get(0).getString("fcontent"));
            } else {
                row.put("fcontent", "");
            }
        }


        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }

    /**
     * 推广素材主界面查询
     *
     * @return
     */
    public String query_archivesMain() throws P2Exception {
        long tarchives_adid = content.getLongValue("tarchives_adid");
        SQLFactory sqlFactory = new SQLFactory(this, "推广素材主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        sqlFactory.addParameter("tarchives_adid", tarchives_adid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        for (Row row : rows) {
            String[] flable = dbConnect.runSqlQuery("select flable from tarchives_ad_tag where siteid='" + siteid + "' and tarchives_adid='" + tarchives_adid + "'").toArray("flable");
            row.put("tag", flable);
            Rows fcontent = dbConnect.runSqlQuery("select fcontent from tarchives_ad where siteid='" + siteid + "' and tarchives_adid='" + tarchives_adid + "'");
            row.put("fcontent", fcontent.get(0).getString("fcontent"));
        }
        dbConnect.runSqlUpdate("update tarchives_ad set fvisitors=isnull(fvisitors,0)+1 where siteid='" + siteid + "' and tarchives_adid=" + content.getString("tarchives_adid"));

        PaoSetRemote tarchives_ad_userdataSet = getP2ServerSystemPaoSet("tarchives_ad_userdata");
        PaoRemote tarchives_ad_userdata = tarchives_ad_userdataSet.addAtEnd();
        tarchives_ad_userdata.setValue("siteid", siteid, 11L);
        tarchives_ad_userdata.setValue("tarchives_adid", tarchives_adid, 11L);
        tarchives_ad_userdata.setValue("tenterprise_userid", userid, 11L);
        tarchives_ad_userdata.setValue("createby", username, 11L);
        Calendar calendar = Calendar.getInstance();
        tarchives_ad_userdata.setValue("createdate", calendar.getTime(), 11L);
        calendar.add(Calendar.MINUTE, 5);
        tarchives_ad_userdata.setValue("closedate", calendar.getTime(), 11L);
        tarchives_ad_userdataSet.save();

        return getSucReturnObject().setData(rows).toString();
    }

    /**
     * 推广素材标签查询
     *
     * @return
     */
    public String query_tagList() {
        SQLFactory sql = new SQLFactory(this, "推广素材系统标签查询");
        sql.addParameter("siteid", siteid);
        Rows rows = dbConnect.runSqlQuery(sql.getSQL());
        return getSucReturnObject().setData(rows).saveToDataPool().toString();
    }

    /**
     * 推广素材收藏
     *
     * @return
     */
    public String archivesCollection() throws P2Exception {
        long tarchives_adid = content.getLongValue("tarchives_adid");//推广素材ID
        boolean iscollection = content.getBooleanValue("iscollection");//是否收藏，1收藏，0取消收藏

        PaoSetRemote tarchives_ad_collectionSet = getP2ServerSystemPaoSet("tarchives_ad_collection", "siteid='" + siteid + "' and tenterprise_userid='" + userid + "' and tarchives_adid='" + tarchives_adid + "'");
        PaoRemote tarchives_ad_collection = null;
        if (tarchives_ad_collectionSet.isEmpty() && iscollection) {
            tarchives_ad_collection = tarchives_ad_collectionSet.addAtEnd();
            tarchives_ad_collection.setValue("siteid", siteid, 11L);//企业ID
            tarchives_ad_collection.setValue("tenterprise_userid", userid, 11L);//用户ID
            tarchives_ad_collection.setValue("fcollectiondate", sysdate, 11L);//录入时间
            tarchives_ad_collection.setValue("tarchives_adid", tarchives_adid, 11L);
        } else if (!tarchives_ad_collectionSet.isEmpty() && !iscollection) {
            tarchives_ad_collectionSet.deleteAll();
        }
        tarchives_ad_collectionSet.save();
        tarchives_ad_collectionSet.close();
        return getSucReturnObject().toString();
    }

    /**
     * 推广素材类型查询
     *
     * @return
     */
    public String getArchivesClass() {
        SQLFactory sqlFactory = new SQLFactory(this, "推广素材分类查询");
        sqlFactory.addParameter("siteid", siteid);
        RowsMap rowsmap = dbConnect.runSqlQuery(sqlFactory.getSQL()).toRowsMap("ftypemx");
        JSONArray array = new JSONArray();
        for (String ftypemx : rowsmap.keySet()) {
            JSONObject object = new JSONObject();
            object.put("fclass1", ftypemx);
            object.put("fclass2", rowsmap.get(ftypemx).toArray("fvalue"));
            array.add(object);
        }
        return getSucReturnObject().setData(array).saveToDataPool().toString();
    }


    /**
     * 推广素材分享
     *
     * @return
     */
    public String archiveShare() throws P2Exception {
        long tarchives_adid = content.getLongValue("tarchives_adid");//推广素材ID
        String fchannel = content.getString("fchannel");//分享渠道

        dbConnect.runSqlUpdate("update tarchives_ad set fshares=isnull(fshares,0)+1 where siteid='" + siteid + "' and tarchives_adid=" + tarchives_adid);

        PaoSetRemote tarchives_ad_shareSet = getP2ServerSystemPaoSet("tarchives_ad_share");
        PaoRemote tarchives_ad_share = tarchives_ad_shareSet.addAtEnd();
        tarchives_ad_share.setValue("siteid", siteid, 11L);//企业ID
        tarchives_ad_share.setValue("tenterprise_userid", userid, 11L);//用户ID
        tarchives_ad_share.setValue("tarchives_adid", tarchives_adid, 11L);//推广素材ID
        tarchives_ad_share.setValue("fchannel", fchannel, 11L);//分享渠道
        tarchives_ad_share.setValue("createby", username, 11L);//分享人
        tarchives_ad_share.setValue("createdate", sysdate, 11L);//分享时间

        tarchives_ad_shareSet.save();
        return getSucReturnObject().toString();
    }

    /**
     * 拉新名单
     *
     * @return
     */
    public String shareStatistics_custlist() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.changedate desc"};
        String sort = getSort(sortfield, "t1.changedate desc");

        SQLFactory factory = new SQLFactory(this, "推广素材分享统计_拉新名单", pageSize, pageNumber, sort);
        factory.addParameter("siteid", siteid);
        factory.addParameter("tenterprise_userid", userid);
        Rows rows = dbConnect.runSqlQuery(factory.getSQL());
        return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
    }
}
