You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.9 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. const prisma = require("../utils/prisma");
  2. const { User } = require("../models/user");
  3. const { Dept } = require("../models/dept");
  4. /**
  5. * @typedef {Object} DeptUser
  6. * @property {number} id
  7. * @property {number} deptId
  8. * @property {number} userId
  9. * @property {Date} createdAt
  10. * @property {Date} updatedAt
  11. */
  12. const DeptUsers = {
  13. writable: ["deptId", "userId"],
  14. validations: {
  15. deptId: (newValue) => {
  16. const num = Number(newValue);
  17. if (isNaN(num)) {
  18. throw new Error("Dept ID must be a number");
  19. }
  20. return num;
  21. },
  22. userId: (newValue) => {
  23. const num = Number(newValue);
  24. if (isNaN(num)) {
  25. throw new Error("User ID must be a number");
  26. }
  27. return num;
  28. },
  29. },
  30. castColumnValue: function (key, value) {
  31. switch (key) {
  32. case "deptId":
  33. case "userId":
  34. return Number(value);
  35. default:
  36. return value;
  37. }
  38. },
  39. /**
  40. * 创建组织机构用户关联
  41. * @param {Object} data - 组织机构用户数据
  42. * @returns {Promise<{ deptUser: DeptUser | null, error: string | null }>}
  43. */
  44. create: async function (data) {
  45. try {
  46. const { userId, deptId } = data.data;
  47. console.log("创建组织机构用户关联:", userId);
  48. console.log("创建组织机构用户关联:", deptId);
  49. // 检查 userId 和 deptId 是否存在
  50. const userExists = await User.get({
  51. id: userId
  52. });
  53. if (!userExists) {
  54. throw new Error(`用户 ID ${userId} 不存在`);
  55. }
  56. const deptExists = await Dept.get({
  57. deptId: parseInt(deptId)
  58. });
  59. if (!deptExists) {
  60. throw new Error(`部门 ID ${deptId} 不存在`);
  61. }
  62. // 创建 dept_users 记录
  63. const deptUser = await prisma.dept_users.create({
  64. data: {
  65. // userId: userId,
  66. // deptId: parseInt(deptId),
  67. createdAt: new Date(),
  68. updatedAt: new Date(),
  69. user: {
  70. connect: {
  71. id: userId,
  72. },
  73. },
  74. dept: {
  75. connect: {
  76. deptId: parseInt(deptId),
  77. },
  78. },
  79. },
  80. });
  81. return deptUser;
  82. } catch (error) {
  83. console.error("创建 dept_users 记录时出错:", error.message);
  84. throw error;
  85. }
  86. },
  87. createNew: async function (data,prisma) {
  88. try {
  89. const { userId, deptId } = data.data;
  90. // 检查 userId 和 deptId 是否存在
  91. const userExists = await User.getNew({
  92. id: userId,
  93. },prisma);
  94. if (!userExists) {
  95. throw new Error(`用户 ID ${userId} 不存在`);
  96. }
  97. const deptExists = await Dept.get({
  98. deptId: parseInt(deptId),
  99. });
  100. if (!deptExists) {
  101. throw new Error(`部门 ID ${deptId} 不存在`);
  102. }
  103. // 创建 dept_users 记录
  104. const deptUser = await prisma.dept_users.create({
  105. data: {
  106. // userId: userId,
  107. // deptId: parseInt(deptId),
  108. createdAt: new Date(),
  109. updatedAt: new Date(),
  110. user: {
  111. connect: {
  112. id: userId,
  113. },
  114. },
  115. dept: {
  116. connect: {
  117. deptId: parseInt(deptId),
  118. },
  119. },
  120. },
  121. });
  122. return deptUser;
  123. } catch (error) {
  124. console.error("创建 dept_users 记录时出错:", error.message);
  125. throw error;
  126. }
  127. },
  128. /**
  129. * 更新组织机构用户关联
  130. * @param {number} id - 关联 ID
  131. * @param {Object} updates - 更新的字段
  132. * @returns {Promise<{ success: boolean, error: string | null, deptUser: DeptUser | null }>}
  133. */
  134. update: async function (id, updates = {}) {
  135. try {
  136. if (!id) throw new Error("No ID provided for update");
  137. const currentDeptUser = await prisma.dept_users.findUnique({
  138. where: { id },
  139. });
  140. if (!currentDeptUser) throw new Error("Dept user not found");
  141. const validatedUpdates = {};
  142. for (const key of this.writable) {
  143. if (updates[key] !== undefined) {
  144. if (this.validations[key]) {
  145. validatedUpdates[key] = this.validations[key](updates[key]);
  146. } else {
  147. validatedUpdates[key] = this.castColumnValue(key, updates[key]);
  148. }
  149. }
  150. }
  151. validatedUpdates.updatedAt = new Date();
  152. const updatedDeptUser = await prisma.dept_users.update({
  153. where: { id },
  154. data: validatedUpdates,
  155. });
  156. return { success: true, error: null, deptUser: updatedDeptUser };
  157. } catch (error) {
  158. console.error(error.message);
  159. return { success: false, error: error.message, deptUser: null };
  160. }
  161. },
  162. /**
  163. * 获取组织机构用户关联
  164. * @param {Object} clause - 查询条件
  165. * @returns {Promise<{ deptUser: DeptUser | null }>}
  166. */
  167. get: async function (clause = {}) {
  168. try {
  169. const deptUser = await prisma.dept_users.findFirst({
  170. where: clause,
  171. });
  172. return deptUser ? { deptUser } : null;
  173. } catch (error) {
  174. console.error(error.message);
  175. return null;
  176. }
  177. },
  178. /**
  179. * 删除组织机构用户关联
  180. * @param {Object} clause - 删除条件
  181. * @returns {Promise<boolean>}
  182. */
  183. delete: async function (clause = {}) {
  184. try {
  185. const affectedRows = await prisma.dept_users.deleteMany({
  186. where: clause,
  187. });
  188. return affectedRows.count > 0;
  189. } catch (error) {
  190. console.error(error.message);
  191. return false;
  192. }
  193. },
  194. /**
  195. * 查询组织机构用户关联列表
  196. * @param {Object} clause - 查询条件
  197. * @param {number} limit - 限制数量
  198. * @returns {Promise<DeptUser[]>}
  199. */
  200. where: async function (clause = {}, limit = null) {
  201. try {
  202. const deptUsers = await prisma.dept_users.findMany({
  203. where: clause,
  204. take: limit !== null ? limit : undefined,
  205. });
  206. return deptUsers;
  207. } catch (error) {
  208. console.error(error.message);
  209. return [];
  210. }
  211. },
  212. };
  213. module.exports = { DeptUsers };