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.

168 lines
4.3 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. const prisma = require("../utils/prisma");
  2. /**
  3. * @typedef {Object} DeptUser
  4. * @property {number} id
  5. * @property {number} deptId
  6. * @property {number} userId
  7. * @property {Date} createdAt
  8. * @property {Date} updatedAt
  9. */
  10. const DeptUsers = {
  11. writable: ["deptId", "userId"],
  12. validations: {
  13. deptId: (newValue) => {
  14. const num = Number(newValue);
  15. if (isNaN(num)) {
  16. throw new Error("Dept ID must be a number");
  17. }
  18. return num;
  19. },
  20. userId: (newValue) => {
  21. const num = Number(newValue);
  22. if (isNaN(num)) {
  23. throw new Error("User ID must be a number");
  24. }
  25. return num;
  26. },
  27. },
  28. castColumnValue: function (key, value) {
  29. switch (key) {
  30. case "deptId":
  31. case "userId":
  32. return Number(value);
  33. default:
  34. return value;
  35. }
  36. },
  37. /**
  38. * 创建组织机构用户关联
  39. * @param {Object} data - 组织机构用户数据
  40. * @returns {Promise<{ deptUser: DeptUser | null, error: string | null }>}
  41. */
  42. create: async function (data) {
  43. console.log("55555555555555555", data);
  44. try {
  45. const validatedData = {};
  46. for (const key of this.writable) {
  47. if (data[key] !== undefined) {
  48. if (this.validations[key]) {
  49. validatedData[key] = this.validations[key](data[key]);
  50. } else {
  51. validatedData[key] = this.castColumnValue(key, data[key]);
  52. }
  53. }
  54. }
  55. const deptUser = await prisma.dept_users.create({
  56. data: {
  57. ...validatedData,
  58. createdAt: new Date(),
  59. updatedAt: new Date(),
  60. },
  61. });
  62. return { deptUser, error: null };
  63. } catch (error) {
  64. console.error("FAILED TO CREATE DEPT USER.", error.message);
  65. return { deptUser: null, error: error.message };
  66. }
  67. },
  68. /**
  69. * 更新组织机构用户关联
  70. * @param {number} id - 关联 ID
  71. * @param {Object} updates - 更新的字段
  72. * @returns {Promise<{ success: boolean, error: string | null, deptUser: DeptUser | null }>}
  73. */
  74. update: async function (id, updates = {}) {
  75. try {
  76. if (!id) throw new Error("No ID provided for update");
  77. const currentDeptUser = await prisma.dept_users.findUnique({
  78. where: { id },
  79. });
  80. if (!currentDeptUser) throw new Error("Dept user not found");
  81. const validatedUpdates = {};
  82. for (const key of this.writable) {
  83. if (updates[key] !== undefined) {
  84. if (this.validations[key]) {
  85. validatedUpdates[key] = this.validations[key](updates[key]);
  86. } else {
  87. validatedUpdates[key] = this.castColumnValue(key, updates[key]);
  88. }
  89. }
  90. }
  91. validatedUpdates.updatedAt = new Date();
  92. const updatedDeptUser = await prisma.dept_users.update({
  93. where: { id },
  94. data: validatedUpdates,
  95. });
  96. return { success: true, error: null, deptUser: updatedDeptUser };
  97. } catch (error) {
  98. console.error(error.message);
  99. return { success: false, error: error.message, deptUser: null };
  100. }
  101. },
  102. /**
  103. * 获取组织机构用户关联
  104. * @param {Object} clause - 查询条件
  105. * @returns {Promise<{ deptUser: DeptUser | null }>}
  106. */
  107. get: async function (clause = {}) {
  108. try {
  109. const deptUser = await prisma.dept_users.findFirst({
  110. where: clause,
  111. });
  112. return deptUser ? { deptUser } : null;
  113. } catch (error) {
  114. console.error(error.message);
  115. return null;
  116. }
  117. },
  118. /**
  119. * 删除组织机构用户关联
  120. * @param {Object} clause - 删除条件
  121. * @returns {Promise<boolean>}
  122. */
  123. delete: async function (clause = {}) {
  124. try {
  125. const affectedRows = await prisma.dept_users.deleteMany({
  126. where: clause,
  127. });
  128. return affectedRows.count > 0;
  129. } catch (error) {
  130. console.error(error.message);
  131. return false;
  132. }
  133. },
  134. /**
  135. * 查询组织机构用户关联列表
  136. * @param {Object} clause - 查询条件
  137. * @param {number} limit - 限制数量
  138. * @returns {Promise<DeptUser[]>}
  139. */
  140. where: async function (clause = {}, limit = null) {
  141. try {
  142. const deptUsers = await prisma.dept_users.findMany({
  143. where: clause,
  144. take: limit !== null ? limit : undefined,
  145. });
  146. return deptUsers;
  147. } catch (error) {
  148. console.error(error.message);
  149. return [];
  150. }
  151. },
  152. };
  153. module.exports = { DeptUsers };