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

    /**
     * 标准工作列表查询
     *
     * @return
     */
    public String query_standardworkList() {
        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_standardwork() throws D3bException, P2Exception {
        String tworktemplateid = content.getString("tworktemplateid");//新增为0
        String fworkname = content.getString("fworkname", "tworktemplate.fworkname", "工作名称");
        Boolean fisused = content.getBoolean("fisused");
        String ftype = content.getString("ftype");
        int fdocqty = content.getIntValue("fdocqty");
        String fnotes = content.getString("fnotes", "tworktemplate.fnotes", "备注");

        PaoSetRemote tworktemplateSet = getP2ServerSystemPaoSet("tworktemplate", "siteid='" + siteid + "' and tworktemplateid='" + tworktemplateid + "'");
        PaoRemote tworktemplate = null;
        if (tworktemplateSet.isEmpty()) {
            tworktemplate = tworktemplateSet.addAtEnd();
            tworktemplate.setValue("siteid", siteid, 11L);//企业ID
            tworktemplate.setValue("createby", username, 11L);
            tworktemplate.setValue("createdate", sysdate, 11L);
        } else {
            tworktemplate = tworktemplateSet.getPao(0);
        }
        tworktemplate.setValue("fworkname", fworkname, 11L);
        tworktemplate.setValue("fisused", fisused, 11L);
        tworktemplate.setValue("ftype", ftype, 11L);
        tworktemplate.setValue("fdocqty", fdocqty, 11L);
        tworktemplate.setValue("fnotes", fnotes, 11L);
        tworktemplate.setValue("changeby", username, 11L);
        tworktemplate.setValue("changedate", sysdate, 11L);

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

    /**
     * 标准工作删除
     *
     * @return
     */
    public String delete_standard() {
        SQLFactory sqlFactory = new SQLFactory(this, "标准工作删除");
        sqlFactory.addParameter("siteid", siteid);
        sqlFactory.addParameter("tworktemplateid", content.getString("tworktemplateid"));
        String result = dbConnect.runSqlUpdate(sqlFactory.getSQL());
        if ("true".equals(result)) {
            return getSucReturnObject().toString();
        } else {
            return getErrReturnObject().setErrMsg(result).toString();
        }
    }
}
