package com.cnd3b.restcontroller.singleagent.appclient.archives;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.Controller;
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 extends Controller {
    public archives(JSONObject content) {
        super(content);
    }

    /**
     * 营销物料列表查询
     *
     * @return
     */
    public String query_archivesList() {
        /**
         *排序条件设置
         */
        String[] sortfield = {"t1.checkdate", "t1.fvisitors"};
        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_collecid is not null");
            }
            if (whereObject.containsKey("fmyarchives") && whereObject.getBooleanValue("fmyarchives")) {
                where.append(" and t1.tenterprise_userid='").append(userid).append("' ");
            } else {
                where.append(" and t1.fstatus='发布'");
            }
        }
        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());

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

    /**
     * 营销物料主界面查询
     *
     * @return
     */
    public String query_archivesMain() throws P2Exception {
        long tarchivesid = content.getLongValue("tarchivesid");
        SQLFactory sqlFactory = new SQLFactory(this, "营销物料主界面查询");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tenterprise_userid", userid);
        sqlFactory.addParameter("tarchivesid", tarchivesid);
        Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
        dbConnect.runSqlUpdate("update tarchives set fvisitors=isnull(fvisitors,0)+1 where siteid='" + siteid + "' and tarchivesid=" + content.getString("tarchivesid"));

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

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

    /**
     * 营销物料收藏
     *
     * @return
     */
    public String archivesCollection() throws P2Exception {
        long tarchivesid = content.getLongValue("tarchivesid");//营销物料ID
        boolean iscollection = content.getBooleanValue("iscollection");//是否收藏，1收藏，0取消收藏

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

        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();
    }

}
