cellsysBase/cellsysGroup.js
2025-03-17 16:16:55 +08:00

214 lines
6.5 KiB
JavaScript

import {
Query,
QueryTask,
QueryType,
EditTask,
EditType,
Operator,
CellsysType,
} from './cellsysUtil.js';
import CellsysMember from './cellsysMember.js';
class CellsysGroup {
constructor(params) {
this.cellsysType = CellsysType.Group;
if (!params) {
params = {};
}
this.id = params.id;
this.name = params.name;
this.description = params.description;
this.status = params.status ? params.status : 1;
this.orgId = params.org_id;
this.orgName = params.org_name;
this.appId = params.app_id;
this.style = params.style || {};
this.userId = params.user_id;
this.count = params.count;
this.isPublish = params.is_publish;
this.publishRange = 1; //群组发布没有分组织和群组,所以发布范围固定是组织
this.leader = params.leader;
this.createTime = params.create_time;
this.updateTime = params.update_time;
let creator = params.creator;
this.creator = {
memberId: null,
name: null,
};
if (creator) {
this.creator = {
memberId: creator.member_id,
name: creator.name,
};
}
let updater = params.updater;
this.updater = {
memberId: null,
name: null,
};
if (updater) {
this.updater = {
memberId: updater.member_id,
name: updater.name,
};
}
this.tags = [];
if (params.tag_name) {
this.tags = params.tag_name.map((name) => {
return { name: name };
});
}
}
setId(id) {
this.id = id;
}
setName(name) {
this.name = name;
}
setDescription(description) {
this.description = description;
}
/**
* 查询群组内的成员
*/
queryMembers(pageInfo, filters, httpConfig) {
let query = new Query();
query.addFilter('group_id', Operator.Equals, this.id);
if (pageInfo) {
query.setCurrPage(pageInfo.currPage);
query.setPageSize(pageInfo.pageSize);
}
if (filters && filters.length > 0) {
filters.forEach((item) => {
query.addFilter(item['name'], item['operator'], item['value']);
});
}
query.setOrder({ member_id: 'asc' });
if (httpConfig) {
query.setHttpConfig(httpConfig);
}
let queryTask = new QueryTask(QueryType.Member, pageInfo ? true : false);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
if (pageInfo) {
if (res.data) {
res.data = res.data.map((item) => {
return new CellsysMember(item);
});
resolve(res);
}
} else {
let resArr = res.map((item) => {
return new CellsysMember(item);
});
resolve(resArr);
}
})
.catch((err) => {
reject(err);
});
});
}
/**
* 新增成员
*/
createMember(memberObj) {
if (!memberObj.memberType) {
return Promise.reject(
new Error('不能添加没有成员类型的用户,请先为该用户分配成员类型'),
);
}
let editTask = new EditTask(EditType.Member_Create);
editTask.addParam('_group_id', this.id);
editTask.addParam('_user_id', memberObj.userId);
// editTask.addParam("_type", memberObj.memberType)
editTask.addParam('_is_leader', memberObj.isLeader);
editTask.addParam('_description', memberObj.description);
return editTask.execute();
}
/**
* 批量新增成员
*/
createMembers(members) {
let editTask = new EditTask(EditType.Member_Batch_Create);
editTask.addParam('_group_id', this.id);
if (members && members.length > 0) {
let _membersParam = [];
members.forEach((memberObj) => {
_membersParam.push({
_realname: memberObj.realName,
_mobile: memberObj.mobile,
_gender: String(memberObj.gender),
_email: memberObj.email,
_member_type_name: memberObj.typeName
? memberObj.typeName
: memberObj.memberType,
_is_leader: memberObj.isLeader,
_description: memberObj.description,
});
});
editTask.addParam('_members', _membersParam);
}
return editTask.execute();
}
/**
* 修改成员信息
*/
updateMember(memberObj) {
let editTask = new EditTask(EditType.Member_Update);
editTask.addParam('_id', memberObj.memberId);
editTask.addParam('_type', memberObj.memberType);
editTask.addParam('_is_leader', memberObj.isLeader);
editTask.addParam('_description', memberObj.description);
return editTask.execute();
}
/**
* 删除群组内成员
*/
removeMember(memberObj) {
let editTask = new EditTask(EditType.Member_Delete);
editTask.addParam('_user_id', memberObj.userId);
editTask.addParam('_group_id', this.id);
return editTask.execute();
}
//查询群组用户定位
queryMembersLocal(params) {
let query = new Query();
query.addFilter('group_id', Operator.Equals, this.id);
let queryType = QueryType.UserLocal;
query.setOrder({ datetime: 'desc' });
if (params && params.httpConfig) {
query.setHttpConfig(params.httpConfig);
}
let queryTask = new QueryTask(queryType, false);
return new Promise((resolve, reject) => {
queryTask
.execute(query)
.then((res) => {
let resArr = res.map((item) => {
return new CellsysMember(item);
});
resolve(resArr);
})
.catch((err) => {
reject(err);
});
});
}
}
export default CellsysGroup;