知识分享 - 如何为单据设置员工、部门、操作员默认值
金蝶云社区-JohnnyDing
JohnnyDing
14人赞赏了该文章 1.6万次浏览 未经作者许可,禁止转载编辑于2016年01月27日 17:23:27
summary-icon摘要由AI智能服务提供

本文档描述了在一个基于K/3 Cloud BOS平台的单据新增功能中,如何将登录用户的员工、员工任岗、部门、业务员等信息自动填充到单据头字段作为默认值的实现方法。由于平台未直接提供相应函数,文档提出通过开发插件解决。插件在界面初始化时加载用户相关信息,并在创建新单据时,将这些信息作为默认值填写到相应的字段上。具体实现包括多个SQL查询来关联用户与各类员工信息,并在单据创建时通过调用Model的SetItemValueByID方法设置字段值。

需求背景:
单据新增时,需要把登录用户对应的员工、员工任岗、部门、业务员等信息,填写到单据头字段中作为默认值。

困难点:
1. K/3 Cloud BOS平台,并没有封装取登录用户对应员工等信息的函数,无从配置默认值
2. 用户与员工、部门、业务员之间的对应关系比较复杂

解决方案:
需要开发插件实现此需求,在界面初始化时,加载用户对应的员工等信息,然后在创建新单时,填写到字段上,作为默认值。

案例说明:
本案例,在单据头上,分别增加了如下几个基础资料字段
1. 员工 F_JD_EmpId
2. 员工任岗 F_JD_StaffId
3. 部门 F_JD_DeptId
4. 采购员 F_JD_BuyerId
5. 仓管员 F_JD_StockerId
6. 销售员 F_JD_SalerId

在插件中,需要把当前登录用户对应的信息,填写到各字段上做为默认值

完整插件代码:
//********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;



// 引用 Kingdee.BOS.dll
using Kingdee.BOS;
using Kingdee.BOS.Util;
// 引用 Kingdee.BOS.Core.dll
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.FieldElement;
// 引用 Kingdee.BOS.ServiceHelper.dll
using Kingdee.BOS.ServiceHelper;
// 引用 Kingdee.BOS.DataEntity.dll
using Kingdee.BOS.Orm.DataEntity;



namespace JDSample.FormPlugIn.Bill
{
///


/// 演示如何设置字段默认值
///

[Description("设置字段默认值")]
public class S160127SetDefaultValueEdit : AbstractBillPlugIn
{
///
/// 登录用户对应的员工内码
///

private long _empId = 0;


///
/// 登录用户对应的员工任岗内码
///

private long _staffId = 0;



///
/// 登录用户对应的部门内码
///

private long _deptId = 0;



///
/// 登录用户对应的采购员内码
///

private long _buyerId = 0;



///
/// 登录用户对应的仓管员内码
///

private long _stockerId = 0;



///
/// 登录用户对应的销售员内码
///

private long _salerId = 0;



///
/// 界面初始化时触发此事件:提前读取登录用户信息
///

///
public override void OnInitialize(InitializeEventArgs e)
{
SqlParam userIdParam = new SqlParam("@FUserId", KDDbType.Int64, this.Context.UserId);
List paramList = new List() { userIdParam };

// 读取登录用户对应的员工(BD_Empinfo)
// 用户 -> 人员 -> 员工
string sqlGetEmpId = @"
select E.FID, E_L.FNAME, P.FPERSONID, U.FUSERID, U.FNAME
from T_SEC_USER U
inner join t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
inner join T_HR_EmpInfo E ON (P.FPERSONID = E.FPERSONID)
left join T_HR_EmpInfo_L E_L ON (E.FID = E_L.FID AND E_L.FLOCALEID = 2052)
where U.FUserId = @FUserId";

using(IDataReader reader = DBServiceHelper.ExecuteReader(this.Context, sqlGetEmpId, paramList))
{
while(reader.Read())
{
this._empId = Convert.ToInt64(reader[0]);
break;
}
}



// 根据用户,找员工任岗 BD_NEWSTAFF
// 用户 -> 人员 -> 员工任岗
string sqlGetStaffId = @"
select S.FSTAFFID, S_L.FNAME, P.FPERSONID, U.FUSERID, U.FNAME
from T_SEC_USER U
inner join t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
inner join T_BD_STAFF S ON (P.FPERSONID = S.FPERSONID)
left join T_BD_STAFF_L S_L ON (S.FSTAFFID = S_L.FSTAFFID AND S_L.FLOCALEID = 2052)
where U.FUserId = @FUserId";

using (IDataReader reader = DBServiceHelper.ExecuteReader(this.Context, sqlGetStaffId, paramList))
{
while (reader.Read())
{
this._staffId = Convert.ToInt64(reader[0]);
break;
}
}



// 读取登录用户对应的部门 BD_Department
// 用户 -> 人员 -> 员工任岗 - 部门
string sqlGetDeptId = @"
select D.FDEPTID, D_L.FNAME , S.FSTAFFID, P.FPERSONID, U.FUSERID, U.FNAME
from T_SEC_USER U
inner join t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
inner join T_BD_STAFF S ON (P.FPERSONID = S.FPERSONID)
inner join T_BD_DEPARTMENT D ON (S.FDEPTID = D.FDEPTID)
left join T_BD_DEPARTMENT_L D_L ON (D.FDEPTID= D_L.FDEPTID AND D_L.FLOCALEID = 2052)
where U.FUserId = @FUserId";

using (IDataReader reader = DBServiceHelper.ExecuteReader(this.Context, sqlGetDeptId, paramList))
{
while (reader.Read())
{
this._deptId = Convert.ToInt64(reader[0]);
break;
}
}



// 读取登录用户对应的采购员 BD_BUYER
// 用户 -> 人员 -> 员工任岗 -> 操作员
string sqlGetBuyerId = @"
select O.FENTRYID, S.FSTAFFID, S_L.FNAME, P.FPERSONID, U.FUSERID, U.FNAME
from T_SEC_USER U
inner join t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
inner join T_BD_STAFF S ON (P.FPERSONID = S.FPERSONID)
left join T_BD_STAFF_L S_L ON (S.FSTAFFID = S_L.FSTAFFID AND S_L.FLOCALEID = 2052)
inner join T_BD_OPERATORENTRY O ON (O.FSTAFFID = S.FSTAFFID AND O.FOPERATORTYPE = 'CGY')
where U.FUserId = @FUserId";

using (IDataReader reader = DBServiceHelper.ExecuteReader(this.Context, sqlGetBuyerId, paramList))
{
while (reader.Read())
{
this._buyerId = Convert.ToInt64(reader[0]);
break;
}
}



// 读取登录用户对应的仓管员 BD_WAREHOUSEWORKERS
// 用户 -> 人员 -> 员工任岗 - 操作员

string sqlGetStockerId = @"
select O.FENTRYID, S.FSTAFFID, S_L.FNAME, P.FPERSONID, U.FUSERID, U.FNAME
from T_SEC_USER U
inner join t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
inner join T_BD_STAFF S ON (P.FPERSONID = S.FPERSONID)
left join T_BD_STAFF_L S_L ON (S.FSTAFFID = S_L.FSTAFFID AND S_L.FLOCALEID = 2052)
inner join T_BD_OPERATORENTRY O ON (O.FSTAFFID = S.FSTAFFID AND O.FOPERATORTYPE = 'WHY')
where U.FUserId = @FUserId";

using (IDataReader reader = DBServiceHelper.ExecuteReader(this.Context, sqlGetStockerId, paramList))
{
while (reader.Read())
{
this._stockerId = Convert.ToInt64(reader[0]);
break;
}
}



// 读取登录用户对应的销售员 BD_Saler
// 用户 -> 人员 -> 员工任岗 - 操作员

string sqlGetSalerId = @"
select O.FENTRYID, S.FSTAFFID, S_L.FNAME, P.FPERSONID, U.FUSERID, U.FNAME
from T_SEC_USER U
inner join t_BD_Person P ON (u.FLINKOBJECT = P.FPERSONID)
inner join T_BD_STAFF S ON (P.FPERSONID = S.FPERSONID)
left join T_BD_STAFF_L S_L ON (S.FSTAFFID = S_L.FSTAFFID AND S_L.FLOCALEID = 2052)
inner join T_BD_OPERATORENTRY O ON (O.FSTAFFID = S.FSTAFFID AND O.FOPERATORTYPE = 'XSY')
where U.FUserId = @FUserId";

using (IDataReader reader = DBServiceHelper.ExecuteReader(this.Context, sqlGetSalerId, paramList))
{
while (reader.Read())
{
this._salerId = Convert.ToInt64(reader[0]);
break;
}
}


}

///
/// 新增单据时触发此事件:在此事件中,设置各字段默认值
///

///
public override void AfterCreateNewData(EventArgs e)
{
this.Model.SetItemValueByID("F_JD_UserId", this.Context.UserId, 0);
this.Model.SetItemValueByID("F_JD_EmpId", this._empId, 0);
this.Model.SetItemValueByID("F_JD_StaffId", this._staffId, 0);
this.Model.SetItemValueByID("F_JD_DeptId", this._deptId, 0);
this.Model.SetItemValueByID("F_JD_BuyerId", this._buyerId, 0);
this.Model.SetItemValueByID("F_JD_StockerId", this._stockerId, 0);
this.Model.SetItemValueByID("F_JD_SalerId", this._salerId, 0);
}
}
}