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 archivesclass_sc extends Controller {
    public archivesclass_sc(JSONObject content) {
        super(content);
    }

    /**
     * 商学院课件分类查询
     *
     * @return
     */
    public String query_archivesclassList() {
        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_archivesclass() throws D3bException, P2Exception {
        long ttypedetailid = content.getLongValue("ttypedetailid");//新增为0
        String fclass1 = content.getString("fclass1", "ttypedetail.ftypemx", "一级分类");//一级分类
        String fclass2 = content.getString("fclass2", "ttypedetail.fvalue", "二级分类");//二级分类
        boolean fisused = content.getBooleanValue("fisused");
        int sequence = content.getIntValue("sequence");

        Rows rows = dbConnect.runSqlQuery("select * from ttypedetail where siteid='" + siteid + "' and ftype='商学院课件分类' and ftypemx='" + fclass1 + "' and fvalue='" + fclass2 + "' ");
        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("ftypemx", fclass1, 11L);
        ttypedetail.setValue("fvalue", fclass2, 11L);
        ttypedetail.setValue("fisused", fisused, 11L);
        ttypedetail.setValue("sequence", sequence, 11L);

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

    /**
     * 商学院课件分类删除
     *
     * @return
     */
    public String delete_archivesclass() {
        SQLFactory sqlFactory = new SQLFactory(this, "商学院课件分类删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("ttypedetailid", content.getString("ttypedetailid"));
        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }
}
