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

    /**
     * 工人角色列表查询
     *
     * @return
     */
    public String query_workerroleList() {
        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_workerrole() throws D3bException, P2Exception {
        String ttypedetailid = content.getString("ttypedetailid");//新增为0
        String frole = content.getString("frole", "ttypedetail.fvalue", "工人角色");
        Boolean fisused = content.getBoolean("fisused");

        Rows rows = dbConnect.runSqlQuery("select * from ttypedetail where siteid='" + siteid + "' and ftype='工人角色' and fvalue='" + frole + "'");
        if (!rows.isEmpty() && !rows.get(0).getString("ttypedetailid").equals(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", frole, 11L);
        ttypedetail.setValue("fisused", fisused, 11L);
        ttypedetailSet.save();

        return getSucReturnObject().toString();
    }

    /**
     * 工人角色删除
     *
     * @return
     */
    public String delete_workerrole() {
        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();
        }
    }
}
