zs 1 år sedan
förälder
incheckning
1bdae53b0d
1 ändrade filer med 153 tillägg och 0 borttagningar
  1. 153 0
      src/service/util/user.util.ts

+ 153 - 0
src/service/util/user.util.ts

@@ -0,0 +1,153 @@
+import { Provide } from '@midwayjs/decorator';
+import { InjectEntityModel } from '@midwayjs/typegoose';
+import { ReturnModelType } from '@typegoose/typegoose';
+import { FrameworkErrorEnum, ServiceError } from 'free-midway-component';
+import { Admin } from '../../entity/user/admin.entity';
+import { Personal } from '../../entity/user/personal.entity';
+import { Company } from '../../entity/user/company.entity';
+import { Expert } from '../../entity/user/expert.entity';
+import { Types } from 'mongoose';
+const ObjectId = Types.ObjectId;
+import {
+  CheckUpdateCardAndPid,
+  checkPhoneAndPid,
+} from '../../interface/util/user.util.interface';
+
+@Provide()
+export class UserUtilService {
+  @InjectEntityModel(Admin)
+  adminModel: ReturnModelType<typeof Admin>;
+
+  @InjectEntityModel(Personal)
+  personalModel: ReturnModelType<typeof Personal>;
+
+  @InjectEntityModel(Company)
+  companyModel: ReturnModelType<typeof Company>;
+
+  @InjectEntityModel(Expert)
+  expertModel: ReturnModelType<typeof Expert>;
+
+  /**
+   * 检查创建时,手机号和上级id
+   * @param data 参数
+   */
+  async checkPhoneAndPid(data: checkPhoneAndPid) {
+    if (data.phone) {
+      // 检查手机号
+      // 管理员
+      const adminan = await this.adminModel.count({ phone: data.phone });
+      if (adminan > 0) {
+        throw new ServiceError(
+          '已有管理员/机构管理员/业务管理员 使用该手机号',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+      // 专家
+      const expertan = await this.expertModel.count({
+        phone: data.phone,
+      });
+      if (expertan > 0) {
+        throw new ServiceError(
+          '已有专家用户使用该手机号',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+      // 企业
+      const companyan = await this.companyModel.count({
+        phone: data.phone,
+      });
+      if (companyan > 0) {
+        throw new ServiceError(
+          '已有企业用户使用该手机号',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+      if (data.code) {
+        // 用户
+        const personalan = await this.personalModel.count({
+          phone: data.phone,
+          code: data.code,
+        });
+        if (personalan > 0) {
+          throw new ServiceError(
+            `姓名:${data.name} ; 手机号: ${data.phone} ;已有机构所属的个人用户使用该手机号`,
+            FrameworkErrorEnum.BAD_BODY
+          );
+        }
+      }
+    }
+    // 检查上级id
+    if (data.pid) {
+      // 管理员
+      const adminid = await this.adminModel.count({
+        _id: new ObjectId(data.pid),
+      });
+      if (adminid <= 0)
+        throw new ServiceError(
+          '未找到上级的信息',
+          FrameworkErrorEnum.NOT_FOUND_DATA
+        );
+    } else {
+      throw new ServiceError(
+        '未找到上级的信息',
+        FrameworkErrorEnum.NOT_FOUND_DATA
+      );
+    }
+  }
+  /**
+   * 检查创建时,手机号和上级id
+   * @param id 当前要修改的数据
+   * @param data 参数
+   */
+  async checkUpdateCardAndPid(id, data: CheckUpdateCardAndPid) {
+    const { phone, code, name } = data;
+    if (phone && code) {
+      // 用户
+      const personalan = await this.personalModel.count({
+        _id: { $ne: id },
+        phone,
+        code,
+      });
+      if (personalan > 0)
+        throw new ServiceError(
+          `姓名:${name} ; 手机号: ${phone} ;已有机构所属的个人用户使用该手机号`,
+          FrameworkErrorEnum.BAD_BODY
+        );
+    }
+    if (phone) {
+      // 管理员
+      const adminan = await this.adminModel.count({
+        _id: { $ne: id },
+        phone,
+      });
+      if (adminan > 0) {
+        throw new ServiceError(
+          '已有管理员/机构管理员/业务管理员 使用该手机号',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+      // 专家
+      const expertan = await this.expertModel.count({
+        _id: { $ne: id },
+        phone,
+      });
+      if (expertan > 0) {
+        throw new ServiceError(
+          '已有专家用户使用该手机号',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+      // 企业
+      const companyan = await this.companyModel.count({
+        _id: { $ne: id },
+        phone,
+      });
+      if (companyan > 0) {
+        throw new ServiceError(
+          '已有企业用户使用该手机号',
+          FrameworkErrorEnum.BAD_BODY
+        );
+      }
+    }
+  }
+}