/// <summary>
    /// 获取待审批的单据
    /// </summary>
    /// <param name="docCode">单据号</param>
    /// <returns></returns>
    public static JObject GetFieldsOfApproveBranch(string docCode)
    {
        JObject res    = new JObject();
        string  ErrMsg = "";
        DataSet ds     = NewBranchRegistrationSrv.GetApprovalRecordAndApprovalFields(docCode, ref ErrMsg);

        if (ds == null)
        {
            res.Add("ErrCode", 2);
            res.Add("ErrMsg", ErrMsg);
        }
        else if (ds.Tables[1].Rows.Count == 0)
        {
            res.Add("ErrCode", 3);
            res.Add("ErrMsg", "抱歉,您无审批内容");
        }
        else
        {
            JArray fieldList = GetFieldList(ds.Tables[1], ds.Tables[2], ds.Tables[3], ds.Tables[4], ds.Tables[5], ds.Tables[6], ds.Tables[7]);
            res.Add("fieldList", fieldList);
            //已审批人的记录
            JArray approverList = DataTableToJArray(ds.Tables[0]);
            res.Add("approverList", approverList);

            res.Add("onlyRead", "false");
            res.Add("ErrCode", 0);
            res.Add("ErrMsg", "操作成功");
        }
        return(res);
    }
    /// <summary>
    /// 新增网点备案时获取字段
    /// </summary>
    /// <param name="userId">人员ID</param>
    /// <returns></returns>
    public static JObject GetFieldsOfAddBranch(string userId)
    {
        JObject res    = new JObject();
        string  ErrMsg = "";
        DataSet ds     = NewBranchRegistrationSrv.IsRegionalManager(userId, ref ErrMsg);

        if (ds == null)
        {
            res.Add("ErrCode", 2);
            res.Add("ErrMsg", ErrMsg);
        }
        else if (ds.Tables[0].Rows.Count == 0)
        {
            res.Add("ErrCode", 3);
            res.Add("ErrMsg", "抱歉,您无权限增加网点");
        }
        else
        {
            DataTable empty        = new DataTable();
            JArray    fieldList    = GetFieldList(ds.Tables[1], ds.Tables[2], ds.Tables[3], ds.Tables[4], ds.Tables[5], ds.Tables[6], empty);
            JArray    approverList = new JArray();
            res.Add("fieldList", fieldList);
            res.Add("onlyRead", "false");
            res.Add("approverList", approverList);
            res.Add("ErrCode", 0);
            res.Add("ErrMsg", "操作成功");
        }
        return(res);
    }
    /// <summary>
    /// 提交单据
    /// </summary>
    /// <param name="fieldList">单据所有字段相关数据</param>
    /// <param name="userId">提交人UserId</param>
    /// <param name="n">遇到错误时提交次数(两个或以上人员同时提交单据时会报错)</param>
    /// <returns></returns>
    private static JObject LeafDepartmentSubmit(JArray fieldList, string userId, int n)
    {
        JObject res = new JObject();

        if (n > 0)
        {
            string  docCode = "0";
            string  ErrMsg  = "";
            DataSet ds      = NewBranchRegistrationSrv.GetMaxDocCode(ref ErrMsg);
            if (ds == null)
            {
                res.Add("ErrCode", 2);
                res.Add("ErrMsg", ErrMsg);
            }
            else
            {
                List <string> sqlList = new List <string>();

                docCode = CreateDocCode(ds.Tables[0].Rows[0][0].ToString());

                JObject DocJObject = new JObject();
                DocJObject.Add("Code", docCode);           //单据号
                DocJObject.Add("Level", "1");              //下一审批级别
                DocJObject.Add("SubmitterUserId", userId); //提交人userId
                DocJObject.Add("InsertOrUpdate", "0");     //0表示新增
                DocJObject.Add("State", "审批中");
                DocJObject.Add("CreateTime", DateTime.Now);

                string departmentId = SearchDepartment(fieldList);

                JArray approverList         = GetNextApprover(ds.Tables[1], 1, departmentId, 0);
                string approverUserId       = JArrayToString(approverList, "userId");       //审批人userId
                string approverWechatUserId = JArrayToString(approverList, "wechatUserId"); //审批人wechatUserId

                DocJObject.Add("ApproverUserId", approverUserId);

                sqlList.Add(SqlHelper.GetInsertString(DocJObject, "cost_sharing_record"));
                sqlList.Add(GetDetailInsertSQL(fieldList, "0", userId, docCode));

                SqlExceRes msg = new SqlExceRes(SqlHelper.Exce(sqlList.ToArray()));
                if (msg.Result == 0)
                {
                    //发信息给提交者还有下级审批人
                    res.Add("ErrCode", 0);
                    res.Add("ErrMsg", "操作成功");
                }
                else//错误,等待0.2秒重新提交
                {
                    Thread.Sleep(200);
                    res = LeafDepartmentSubmit(fieldList, userId, n - 1);
                }
            }
        }
        else
        {
            res.Add("ErrCode", 3);
            res.Add("ErrMsg", "网络超时,请重新提交!");
        }
        return(res);
    }
    /// <summary>
    /// 审批/提交记录获取
    /// </summary>
    /// <param name="userId">人员ID</param>
    /// <param name="docCode">单据号</param>
    /// <returns></returns>
    public static JObject GetFieldsOfRecordBranch(string userId, string docCode)
    {
        JObject res    = new JObject();
        string  ErrMsg = "";
        DataSet ds     = NewBranchRegistrationSrv.GetApprovalRecord(userId, docCode, ref ErrMsg);

        if (ds == null)
        {
            res.Add("ErrCode", 2);
            res.Add("ErrMsg", ErrMsg);
        }
        else if (ds.Tables[1].Rows.Count == 0)
        {
            res.Add("ErrCode", 3);
            res.Add("ErrMsg", "抱歉,无已审批内容,如有疑问,请及时联系管理员");
        }
        else
        {
            JArray fieldList = GetRecordFieldList(ds.Tables[1], ds.Tables[2], ds.Tables[3], ds.Tables[4], ds.Tables[5]);
            res.Add("fieldList", fieldList);
            //已审批人的记录
            JArray approverList = DataTableToJArray(ds.Tables[0]);
            res.Add("approverList", approverList);

            res.Add("onlyRead", "true");
            res.Add("ErrCode", 0);
            res.Add("ErrMsg", "操作成功");
        }
        return(res);
    }
    /// <summary>
    /// 审批
    /// </summary>
    /// <param name="fieldList">被审批的字段</param>
    /// <param name="userId">审批人userId</param>
    /// <param name="docCode">单据号</param>
    /// <returns></returns>
    private static JObject DepartmentApprove(JArray fieldList, string userId, string docCode)
    {
        JObject res    = new JObject();
        string  ErrMsg = "";
        DataSet ds     = NewBranchRegistrationSrv.GetDoc(docCode, ref ErrMsg);

        if (ds == null)
        {
            res.Add("ErrCode", 2);
            res.Add("ErrMsg", ErrMsg);
        }
        else
        {
            List <string> sqlList = new List <string>();

            string departmentId = SearchDepartment(fieldList); //从前端传回的字段中里找‘部门’字段
            if (departmentId == null)                          //如果前端里没有‘部门’字段,从数据库中获取
            {
                departmentId = SearchDepartment(ds.Tables[0]);
            }

            int level = Convert.ToInt16(ds.Tables[2].Rows[0]["Level"]);
            int n     = 0;
            if (level == 3)//如果level=3需要往上找2级,所以n=1,具体代码见GetNextApprover方法
            {
                n = 1;
            }
            JArray approverList = GetNextApprover(ds.Tables[1], level + 1, departmentId, n);

            string state                = "已审批";
            string approverUserId       = "";                   //审批人userId
            string approverWechatUserId = "";                   //审批人wechatUserId

            if (approverList != null || approverList.Count > 0) //若审批未结束
            {
                approverUserId       = JArrayToString(approverList, "userId");
                approverWechatUserId = JArrayToString(approverList, "wechatUserId");
                state = "审批中";
            }

            JObject DocJObject = new JObject();
            DocJObject.Add("ApproverUserId", approverUserId);
            DocJObject.Add("Level", level + 1);
            DocJObject.Add("State", state);
            string condition = "where Code= '" + docCode + "'";

            sqlList.Add(SqlHelper.GetUpdateString(DocJObject, "cost_sharing_record", condition));
            sqlList.Add(GetDetailInsertSQL(fieldList, level.ToString(), userId, docCode));
            SqlHelper.Exce(sqlList.ToArray());

            if (approverWechatUserId == "")//审批结束
            {
                DataSet DocDS = NewBranchRegistrationSrv.GetDetail(docCode);
                SqlHelper.Exce(GetInsertSQL(DocDS.Tables[0]));//插入到new_cost_sharing表中
                //发送消息给单据提交人
            }
            else//审批进行中
            {
                //发送消息给单据提交人和下级审批人
            }
            res.Add("ErrCode", 0);
            res.Add("ErrMsg", "操作成功");
        }
        return(res);
    }