258 lines
8.1 KiB
Go
258 lines
8.1 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"hospital-admin-api/api/dao"
|
|
"hospital-admin-api/api/model"
|
|
"hospital-admin-api/extend/ca"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type UserCaCertService struct {
|
|
}
|
|
|
|
// GetUserCloudCert 申请云证书-个人
|
|
func (r *UserCaCertService) GetUserCloudCert(tx *gorm.DB, userId int64) (bool, error) {
|
|
userCaCertDao := dao.UserCaCert{}
|
|
|
|
// 检测是否存在云证书
|
|
maps := make(map[string]interface{})
|
|
maps["user_id"] = userId
|
|
maps["type"] = 2
|
|
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
|
if userCaCert != nil {
|
|
return false, errors.New("医生存在正常使用的云证书,请注销后重新申请")
|
|
}
|
|
|
|
// 获取用户数据
|
|
userDao := dao.UserDao{}
|
|
user, err := userDao.GetUserById(userId)
|
|
if err != nil || user == nil {
|
|
return false, errors.New("用户数据错误")
|
|
}
|
|
|
|
// 定义所需变量
|
|
var departmentCustomId int64 // 自定义科室id
|
|
var cardName string // 身份证名称
|
|
var cardNum string // 身份证号码
|
|
|
|
// 医生
|
|
if user.UserType == 2 {
|
|
// 获取医生数据
|
|
userDoctorDao := dao.UserDoctorDao{}
|
|
userDoctor, err := userDoctorDao.GetUserDoctorByUserId(userId)
|
|
if err != nil || userDoctor == nil {
|
|
return false, errors.New("医生数据错误")
|
|
}
|
|
|
|
// 获取医生详情数据
|
|
userDoctorInfoDao := dao.UserDoctorInfoDao{}
|
|
userDoctorInfo, err := userDoctorInfoDao.GetUserDoctorInfoByDoctorId(userDoctor.DoctorId)
|
|
if err != nil || userDoctorInfo == nil {
|
|
return false, errors.New("医生详情数据错误")
|
|
}
|
|
|
|
if userDoctor.IdenAuthStatus != 1 {
|
|
return false, errors.New("请先通过身份认证")
|
|
}
|
|
|
|
if userDoctor.MultiPointStatus != 1 {
|
|
return false, errors.New("请先完成多点执业认证")
|
|
}
|
|
|
|
departmentCustomId = userDoctor.DepartmentCustomId // 自定义科室id
|
|
cardName = userDoctorInfo.CardName // 身份证名称
|
|
cardNum = userDoctorInfo.CardNum // 身份证号码
|
|
}
|
|
|
|
// 获取自定义科室数据
|
|
hospitalDepartmentCustomDao := dao.HospitalDepartmentCustomDao{}
|
|
hospitalDepartmentCustom, err := hospitalDepartmentCustomDao.GetHospitalDepartmentCustomById(departmentCustomId)
|
|
if err != nil || hospitalDepartmentCustom == nil {
|
|
return false, errors.New("科室错误")
|
|
}
|
|
|
|
// 获取标准科室数据
|
|
hospitalDepartmentDao := dao.HospitalDepartment{}
|
|
hospitalDepartment, err := hospitalDepartmentDao.GetHospitalDepartmentById(hospitalDepartmentCustom.DepartmentId)
|
|
if err != nil || hospitalDepartment == nil {
|
|
return false, errors.New("科室错误")
|
|
}
|
|
|
|
// 申请云证书
|
|
cloudCertRequestData := &ca.AddCloudCertRequest{
|
|
EntityId: fmt.Sprintf("%d", userId),
|
|
EntityType: "Personal",
|
|
PersonalPhone: user.Mobile,
|
|
PersonalName: cardName,
|
|
PersonalIdNumber: cardNum,
|
|
OrgName: "",
|
|
OrgNumber: "",
|
|
Pin: fmt.Sprintf("%d", userId),
|
|
OrgDept: hospitalDepartment.DepartmentName, // // 卫生证书:医院部门
|
|
Province: "四川省",
|
|
Locality: "成都市",
|
|
AuthType: "实人认证",
|
|
AuthTime: strconv.FormatInt(time.Now().Unix(), 10),
|
|
AuthResult: "认证通过",
|
|
AuthNoticeType: "数字证书申请告知",
|
|
}
|
|
|
|
cloudCertResponse, err := ca.AddCloudCert(cloudCertRequestData)
|
|
if err != nil || cloudCertResponse == nil {
|
|
return false, errors.New(err.Error())
|
|
}
|
|
|
|
// 新增ca监管证书表
|
|
userCaCert = &model.UserCaCert{
|
|
UserId: &userId,
|
|
IsSystem: 0,
|
|
IsLatest: 1,
|
|
Type: 2,
|
|
CertBase64: cloudCertResponse.CertBase64,
|
|
CertChainP7: cloudCertResponse.CertP7,
|
|
CertSerialNumber: cloudCertResponse.CertSerialnumber,
|
|
CaPin: fmt.Sprintf("%d", userId),
|
|
IsSignConfig: 0,
|
|
SignConfig: "",
|
|
CertApplicationTime: model.LocalTime(time.Now()),
|
|
CertExpireTime: model.LocalTime(time.Now().AddDate(0, 0, 180)), // 180天以后的时间
|
|
}
|
|
|
|
userCaCert, err = userCaCertDao.AddUserCaCert(tx, userCaCert)
|
|
if err != nil || userCaCert == nil {
|
|
return false, errors.New(err.Error())
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// EditUserCloudCert 修改云证书-个人
|
|
func (r *UserCaCertService) EditUserCloudCert(tx *gorm.DB, userId int64) (bool, error) {
|
|
userCaCertDao := dao.UserCaCert{}
|
|
|
|
// 检测是否存在云证书
|
|
maps := make(map[string]interface{})
|
|
maps["user_id"] = userId
|
|
maps["type"] = 2
|
|
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
|
if userCaCert == nil {
|
|
return false, errors.New("用户未申请云证书,无法修改")
|
|
}
|
|
|
|
if userCaCert.IsLatest == 1 {
|
|
return false, errors.New("用户云证书为最新证书,无法修改")
|
|
}
|
|
|
|
// 获取用户数据
|
|
userDao := dao.UserDao{}
|
|
user, err := userDao.GetUserById(userId)
|
|
if err != nil || user == nil {
|
|
return false, errors.New("用户数据错误")
|
|
}
|
|
|
|
// 定义所需变量
|
|
var departmentCustomId int64 // 自定义科室id
|
|
var cardName string // 身份证名称
|
|
var cardNum string // 身份证号码
|
|
|
|
// 医生
|
|
if user.UserType == 2 {
|
|
// 获取医生数据
|
|
userDoctorDao := dao.UserDoctorDao{}
|
|
userDoctor, err := userDoctorDao.GetUserDoctorByUserId(userId)
|
|
if err != nil || userDoctor == nil {
|
|
return false, errors.New("医生数据错误")
|
|
}
|
|
|
|
// 获取医生详情数据
|
|
userDoctorInfoDao := dao.UserDoctorInfoDao{}
|
|
userDoctorInfo, err := userDoctorInfoDao.GetUserDoctorInfoByDoctorId(userDoctor.DoctorId)
|
|
if err != nil || userDoctorInfo == nil {
|
|
return false, errors.New("医生详情数据错误")
|
|
}
|
|
|
|
if userDoctor.IdenAuthStatus != 1 {
|
|
return false, errors.New("请先通过身份认证")
|
|
}
|
|
|
|
if userDoctor.MultiPointStatus != 1 {
|
|
return false, errors.New("请先完成多点执业认证")
|
|
}
|
|
|
|
// 检测是否存在正在审核中的处方
|
|
orderPrescriptionDao := dao.OrderPrescriptionDao{}
|
|
|
|
maps := make(map[string]interface{})
|
|
maps["doctor_id"] = userDoctor.DoctorId
|
|
maps["prescription_status"] = 1
|
|
orderPrescription, err := orderPrescriptionDao.GetList(maps)
|
|
if err != nil {
|
|
return false, errors.New("更新云证书失败")
|
|
}
|
|
|
|
if len(orderPrescription) > 0 {
|
|
return false, errors.New("存在审核中的处方,请勿更新云证书")
|
|
}
|
|
|
|
departmentCustomId = userDoctor.DepartmentCustomId // 自定义科室id
|
|
cardName = userDoctorInfo.CardName // 身份证名称
|
|
cardNum = userDoctorInfo.CardNum // 身份证号码
|
|
}
|
|
|
|
// 获取自定义科室数据
|
|
hospitalDepartmentCustomDao := dao.HospitalDepartmentCustomDao{}
|
|
hospitalDepartmentCustom, err := hospitalDepartmentCustomDao.GetHospitalDepartmentCustomById(departmentCustomId)
|
|
if err != nil || hospitalDepartmentCustom == nil {
|
|
return false, errors.New("科室错误")
|
|
}
|
|
|
|
// 获取标准科室数据
|
|
hospitalDepartmentDao := dao.HospitalDepartment{}
|
|
hospitalDepartment, err := hospitalDepartmentDao.GetHospitalDepartmentById(hospitalDepartmentCustom.DepartmentId)
|
|
if err != nil || hospitalDepartment == nil {
|
|
return false, errors.New("科室错误")
|
|
}
|
|
|
|
// 修改云证书
|
|
cloudCertRequestData := &ca.EditCloudCertRequestData{
|
|
EntityId: fmt.Sprintf("%d", userId),
|
|
EntityType: "Personal",
|
|
PersonalPhone: user.Mobile,
|
|
PersonalName: cardName,
|
|
PersonalIdNumber: cardNum,
|
|
OrgName: "",
|
|
OrgNumber: "",
|
|
Pin: fmt.Sprintf("%d", userId),
|
|
OrgDept: hospitalDepartment.DepartmentName, // // 卫生证书:医院部门
|
|
Province: "四川省",
|
|
Locality: "成都市",
|
|
AuthType: "实人认证",
|
|
AuthTime: strconv.FormatInt(time.Now().Unix(), 10),
|
|
AuthResult: "认证通过",
|
|
AuthNoticeType: "数字证书变更告知",
|
|
}
|
|
|
|
cloudCertResponse, err := ca.EditCloudCert(cloudCertRequestData)
|
|
if err != nil || cloudCertResponse == nil {
|
|
tx.Rollback()
|
|
return false, errors.New(err.Error())
|
|
}
|
|
|
|
// 修改ca监管证书表
|
|
data := make(map[string]interface{})
|
|
data["cert_base64"] = cloudCertResponse.CertBase64
|
|
data["cert_chain_p7"] = cloudCertResponse.CertP7
|
|
data["cert_serial_number"] = cloudCertResponse.CertSerialnumber
|
|
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return false, errors.New("审核失败")
|
|
}
|
|
|
|
return true, nil
|
|
}
|