package com.cnd3b.restcontroller.enterprise.webclient.system;

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

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

    /**
     * 推广素材标签查询
     *
     * @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 insert_modify_tag() throws D3bException, P2Exception {
        long ttypedetailid = content.getLongValue("ttypedetailid");//新增为0
        String flable = content.getString("flable", "ttypedetail.fvalue", "标签");//标签值
        boolean fisused = content.getBooleanValue("fisused");

        Rows rows = dbConnect.runSqlQuery("select * from ttypedetail where siteid='" + siteid + "' and ftype='推广素材标签' and fvalue='" + flable + "' ");
        if (!rows.isEmpty() && rows.get(0).getLong("ttypedetailid") != ttypedetailid) {
            return getErrReturnObject().setErrMsg("推广素材标签不可重复").toString();
        }

        PaoSetRemote ttypedetailSet = getP2ServerSystemPaoSet("ttypedetail", "siteid='" + siteid + "' and ttypedetailid='" + ttypedetailid + "'");
        PaoRemote ttypedetail = null;
        if (ttypedetailSet.isEmpty()) {
            ttypedetail = ttypedetailSet.addAtEnd();
            ttypedetail.setValue("siteid", siteid, 11L);//企业ID
            ttypedetail.setValue("ftype", "推广素材标签", 11L);
        } else {
            ttypedetail = ttypedetailSet.getPao(0);
        }
        ttypedetail.setValue("fvalue", flable, 11L);
        ttypedetail.setValue("fisused", fisused, 11L);

        ttypedetailSet.save();

        return getSucReturnObject().toString();
    }

    /**
     * 推广素材标签删除
     *
     * @return
     */
    public String delete_tag() {
        SQLFactory sqlFactory = new SQLFactory(this, "推广素材标签删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("ttypedetailid", content.getLongValue("ttypedetailid"));
        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }
}
