904 lines
28 KiB
Go
904 lines
28 KiB
Go
package service
|
||
|
||
import (
|
||
"encoding/base64"
|
||
"encoding/json"
|
||
"errors"
|
||
"fmt"
|
||
"gorm.io/gorm"
|
||
"hospital-admin-api/api/dao"
|
||
"hospital-admin-api/api/dto"
|
||
"hospital-admin-api/api/model"
|
||
"hospital-admin-api/api/requests"
|
||
"hospital-admin-api/config"
|
||
"hospital-admin-api/extend/aliyun"
|
||
"hospital-admin-api/extend/ca"
|
||
"hospital-admin-api/global"
|
||
"hospital-admin-api/utils"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type UserCaCertService struct {
|
||
}
|
||
|
||
// AddUserCloudCert 申请云证书-个人
|
||
func (r *UserCaCertService) AddUserCloudCert(tx *gorm.DB, userId int64) (bool, error) {
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
maps := make(map[string]interface{})
|
||
maps["user_id"] = userId
|
||
maps["type"] = 2
|
||
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
||
if userCaCert != nil {
|
||
if time.Time(userCaCert.CertExpireTime).IsZero() {
|
||
return false, errors.New("云证书过期时间错误")
|
||
}
|
||
|
||
now := time.Now()
|
||
certExpireTime := time.Time(userCaCert.CertExpireTime)
|
||
if now.Before(certExpireTime) {
|
||
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("医生详情数据错误")
|
||
}
|
||
|
||
departmentCustomId = userDoctor.DepartmentCustomId // 自定义科室id
|
||
cardName = userDoctorInfo.CardName // 身份证名称
|
||
cardNum = userDoctorInfo.CardNum // 身份证号码
|
||
} else {
|
||
// 获取药师数据
|
||
userPharmacistDao := dao.UserPharmacistDao{}
|
||
userPharmacist, err := userPharmacistDao.GetUserPharmacistByUserId(userId)
|
||
if err != nil || userPharmacist == nil {
|
||
return false, errors.New("药师数据错误")
|
||
}
|
||
|
||
// 获取药师详情数据
|
||
userPharmacistInfoDao := dao.UserPharmacistInfoDao{}
|
||
userPharmacistInfo, err := userPharmacistInfoDao.GetUserPharmacistInfoByUserId(userId)
|
||
if err != nil || userPharmacistInfo == nil {
|
||
return false, errors.New("药师详情数据错误")
|
||
}
|
||
|
||
departmentCustomId = userPharmacist.DepartmentCustomId // 自定义科室id
|
||
cardName = userPharmacistInfo.CardName // 身份证名称
|
||
cardNum = userPharmacistInfo.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: "91510106MABTJY4K9R",
|
||
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())
|
||
}
|
||
|
||
// 到期时间-测试:180,正式:365
|
||
CertExpireTime := model.LocalTime(time.Now().AddDate(0, 0, 180))
|
||
if config.C.Env == "prod" {
|
||
CertExpireTime = model.LocalTime(time.Now().AddDate(0, 0, 365))
|
||
}
|
||
|
||
if userCaCert == nil {
|
||
// 新增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: CertExpireTime, // 180天以后的时间
|
||
}
|
||
|
||
userCaCert, err = userCaCertDao.AddUserCaCert(tx, userCaCert)
|
||
if err != nil || userCaCert == nil {
|
||
return false, errors.New(err.Error())
|
||
}
|
||
} else {
|
||
// 修改ca监管证书表
|
||
data := make(map[string]interface{})
|
||
data["cert_base64"] = cloudCertResponse.CertBase64
|
||
data["cert_chain_p7"] = cloudCertResponse.CertP7
|
||
data["cert_serial_number"] = cloudCertResponse.CertSerialnumber
|
||
data["cert_application_time"] = model.LocalTime(time.Now())
|
||
data["cert_expire_time"] = CertExpireTime
|
||
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
||
if err != nil {
|
||
return false, errors.New("审核失败")
|
||
}
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// AddHospitalCloudCert 申请云证书-医院
|
||
func (r *UserCaCertService) AddHospitalCloudCert(tx *gorm.DB) (bool, error) {
|
||
var userId int64 = 5345345461
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
maps := make(map[string]interface{})
|
||
maps["ca_pin"] = userId
|
||
maps["type"] = 2
|
||
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
||
if userCaCert != nil {
|
||
if time.Time(userCaCert.CertExpireTime).IsZero() {
|
||
return false, errors.New("医院云证书过期时间错误")
|
||
}
|
||
|
||
now := time.Now()
|
||
certExpireTime := time.Time(userCaCert.CertExpireTime)
|
||
if now.Before(certExpireTime) {
|
||
return false, errors.New("医院云证书还未过期")
|
||
}
|
||
}
|
||
|
||
// 申请云证书
|
||
cloudCertRequestData := &ca.AddCloudCertRequest{
|
||
EntityId: fmt.Sprintf("%d", userId),
|
||
EntityType: "Organizational",
|
||
PersonalPhone: "18601047315",
|
||
PersonalName: "张晓彦",
|
||
PersonalIdNumber: "132527197709270017",
|
||
OrgName: "成都金牛欣欣相照互联网医院有限公司",
|
||
OrgNumber: "91510106MABTJY4K9R",
|
||
Pin: fmt.Sprintf("%d", userId),
|
||
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())
|
||
}
|
||
|
||
// 到期时间-测试:180,正式:365
|
||
CertExpireTime := model.LocalTime(time.Now().AddDate(0, 0, 180))
|
||
if config.C.Env == "prod" {
|
||
CertExpireTime = model.LocalTime(time.Now().AddDate(0, 0, 365))
|
||
}
|
||
|
||
if userCaCert == nil {
|
||
// 新增ca监管证书表
|
||
userCaCert = &model.UserCaCert{
|
||
UserId: &userId,
|
||
IsSystem: 1,
|
||
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())
|
||
}
|
||
} else {
|
||
// 修改ca监管证书表
|
||
data := make(map[string]interface{})
|
||
data["cert_base64"] = cloudCertResponse.CertBase64
|
||
data["cert_chain_p7"] = cloudCertResponse.CertP7
|
||
data["cert_serial_number"] = cloudCertResponse.CertSerialnumber
|
||
data["cert_application_time"] = model.LocalTime(time.Now())
|
||
data["cert_expire_time"] = CertExpireTime
|
||
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
||
if err != nil {
|
||
return false, errors.New("审核失败")
|
||
}
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// EditUserCloudCert 修改云证书-个人
|
||
func (r *UserCaCertService) EditUserCloudCert(tx *gorm.DB, userId int64) (bool, error) {
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
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("医生详情数据错误")
|
||
}
|
||
|
||
// 检测是否存在正在审核中的处方
|
||
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: "91510106MABTJY4K9R",
|
||
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 {
|
||
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 {
|
||
return false, errors.New("审核失败")
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// RenewUserCloudCert 更新云证书-个人-续约
|
||
func (r *UserCaCertService) RenewUserCloudCert(tx *gorm.DB, userId int64) (bool, error) {
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
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 == 0 {
|
||
return false, errors.New("医生云证书非最新,请执行更新")
|
||
}
|
||
|
||
if !userCaCert.CertExpireTime.IsEmpty() {
|
||
timeDifference := time.Time(userCaCert.CertExpireTime).Sub(time.Now())
|
||
|
||
if timeDifference > 60*24*time.Hour {
|
||
return false, errors.New("云证书有效期大于60天,无法更新")
|
||
}
|
||
}
|
||
|
||
// 获取用户数据
|
||
userDao := dao.UserDao{}
|
||
user, err := userDao.GetUserById(userId)
|
||
if err != nil || user == nil {
|
||
return false, errors.New("用户数据错误")
|
||
}
|
||
|
||
// 修改云证书
|
||
cloudCertRequestData := &ca.RenewCloudCertRequest{
|
||
EntityId: fmt.Sprintf("%d", userId),
|
||
Pin: fmt.Sprintf("%d", userId),
|
||
AuthType: "实人认证",
|
||
AuthTime: strconv.FormatInt(time.Now().Unix(), 10),
|
||
AuthResult: "认证通过",
|
||
AuthNoticeType: "数字证书更新告知",
|
||
}
|
||
|
||
cloudCertResponse, err := ca.RenewCloudCert(cloudCertRequestData)
|
||
if err != nil || cloudCertResponse == nil {
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
// 到期时间-测试:180,正式:365
|
||
CertExpireTime := model.LocalTime(time.Now().AddDate(0, 0, 180))
|
||
if config.C.Env == "prod" {
|
||
CertExpireTime = model.LocalTime(time.Now().AddDate(0, 0, 365))
|
||
}
|
||
|
||
// 修改ca监管证书表
|
||
data := make(map[string]interface{})
|
||
data["cert_base64"] = cloudCertResponse.CertBase64
|
||
data["cert_chain_p7"] = cloudCertResponse.CertP7
|
||
data["cert_serial_number"] = cloudCertResponse.CertSerialnumber
|
||
data["cert_application_time"] = model.LocalTime(time.Now())
|
||
data["cert_expire_time"] = CertExpireTime
|
||
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
||
if err != nil {
|
||
return false, errors.New("审核失败")
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// RenewHospitalCloudCert 更新云证书-医院-续约
|
||
func (r *UserCaCertService) RenewHospitalCloudCert(tx *gorm.DB) (bool, error) {
|
||
var userId int64 = 5345345461
|
||
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
maps := make(map[string]interface{})
|
||
maps["ca_pin"] = userId
|
||
maps["type"] = 2
|
||
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
||
if userCaCert == nil {
|
||
return false, errors.New("医院未申请云证书,无法操作")
|
||
}
|
||
|
||
if userCaCert.IsLatest == 0 {
|
||
return false, errors.New("医院云证书非最新,无法操作")
|
||
}
|
||
|
||
if !userCaCert.CertExpireTime.IsEmpty() {
|
||
timeDifference := time.Time(userCaCert.CertExpireTime).Sub(time.Now())
|
||
|
||
if timeDifference > 60*24*time.Hour {
|
||
return false, errors.New("云证书有效期大于60天,无法更新")
|
||
}
|
||
}
|
||
|
||
// 修改云证书
|
||
cloudCertRequestData := &ca.RenewCloudCertRequest{
|
||
EntityId: fmt.Sprintf("%d", userId),
|
||
Pin: fmt.Sprintf("%d", userId),
|
||
AuthType: "实人认证",
|
||
AuthTime: strconv.FormatInt(time.Now().Unix(), 10),
|
||
AuthResult: "认证通过",
|
||
AuthNoticeType: "数字证书更新告知",
|
||
}
|
||
|
||
cloudCertResponse, err := ca.RenewCloudCert(cloudCertRequestData)
|
||
if err != nil || cloudCertResponse == nil {
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
// 到期时间-测试:180,正式:365
|
||
CertExpireTime := model.LocalTime(time.Now().AddDate(0, 0, 180))
|
||
if config.C.Env == "prod" {
|
||
CertExpireTime = model.LocalTime(time.Now().AddDate(0, 0, 365))
|
||
}
|
||
|
||
// 修改ca监管证书表
|
||
data := make(map[string]interface{})
|
||
data["cert_base64"] = cloudCertResponse.CertBase64
|
||
data["cert_chain_p7"] = cloudCertResponse.CertP7
|
||
data["cert_serial_number"] = cloudCertResponse.CertSerialnumber
|
||
data["cert_application_time"] = model.LocalTime(time.Now())
|
||
data["cert_expire_time"] = CertExpireTime
|
||
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
||
if err != nil {
|
||
return false, errors.New("更新失败")
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// RemoveUserCloudCert 注销云证书-个人
|
||
func (r *UserCaCertService) RemoveUserCloudCert(tx *gorm.DB, userId int64) (bool, error) {
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
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("用户数据错误")
|
||
}
|
||
|
||
// 删除签章配置
|
||
deleteUserSignConfigRequestData := &ca.DeleteUserSignConfigRequestData{
|
||
UserId: fmt.Sprintf("%d", userCaCert.UserId),
|
||
ConfigKey: fmt.Sprintf("%d", userCaCert.UserId),
|
||
}
|
||
|
||
_, err = ca.DeleteUserSignConfig(deleteUserSignConfigRequestData)
|
||
if err != nil {
|
||
return false, errors.New("删除用户签章配置失败")
|
||
}
|
||
|
||
// 修改签章配置为未添加
|
||
data := make(map[string]interface{})
|
||
data["is_sign_config"] = 0
|
||
data["sign_config"] = ""
|
||
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
||
if err != nil {
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
// 注销云证书
|
||
cloudCertRequestData := &ca.RemoveCloudCertRequest{
|
||
EntityId: fmt.Sprintf("%d", userId),
|
||
Pin: fmt.Sprintf("%d", userId),
|
||
AuthType: "实人认证",
|
||
AuthTime: strconv.FormatInt(time.Now().Unix(), 10),
|
||
AuthResult: "认证通过",
|
||
AuthNoticeType: "数字证书吊销告知",
|
||
}
|
||
|
||
_, err = ca.RemoveCloudCert(cloudCertRequestData)
|
||
if err != nil {
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
// 修改ca监管证书表-注销
|
||
err = userCaCertDao.DeleteUserCaCertById(tx, userCaCert.CertId)
|
||
if err != nil {
|
||
return false, errors.New("注销失败")
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// AddUserSignConfig 添加用户签章配置
|
||
func (r *UserCaCertService) AddUserSignConfig(tx *gorm.DB, req requests.AddUserSignConfig) (bool, error) {
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
var entityId string // 唯一标识
|
||
var cardNum string // 身份证号/信用代码
|
||
var signImagePath string // 签名图片地址
|
||
|
||
// 医院-固定
|
||
if req.Type == 1 {
|
||
entityId = "5345345461"
|
||
cardNum = "91510106MABTJY4K9R"
|
||
signImagePath = "basic/file/hospital_signature.png"
|
||
}
|
||
|
||
// 医生
|
||
if req.Type == 2 {
|
||
if req.UserId == "" {
|
||
return false, errors.New("缺少用户标识")
|
||
}
|
||
|
||
entityId = req.UserId
|
||
|
||
// 将 id 转换为 int64 类型
|
||
userId, err := strconv.ParseInt(req.UserId, 10, 64)
|
||
if err != nil {
|
||
return false, errors.New("用户标识错误")
|
||
}
|
||
|
||
// 获取医生数据
|
||
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 userDoctorInfo.SignImage == "" {
|
||
return false, errors.New("缺少签名图片,无法申请")
|
||
}
|
||
|
||
cardNum = userDoctorInfo.CardNum
|
||
signImagePath = strings.TrimLeft(userDoctorInfo.SignImage, "/")
|
||
}
|
||
|
||
// 药师
|
||
if req.Type == 3 {
|
||
if req.UserId == "" {
|
||
return false, errors.New("缺少用户标识")
|
||
}
|
||
|
||
entityId = req.UserId
|
||
|
||
// 将 id 转换为 int64 类型
|
||
userId, err := strconv.ParseInt(req.UserId, 10, 64)
|
||
if err != nil {
|
||
return false, errors.New("用户标识错误")
|
||
}
|
||
|
||
// 获取药师详情数据
|
||
userPharmacistInfoDao := dao.UserPharmacistInfoDao{}
|
||
userPharmacistInfo, err := userPharmacistInfoDao.GetUserPharmacistInfoByUserId(userId)
|
||
if err != nil || userPharmacistInfo == nil {
|
||
return false, errors.New("药师详情数据错误")
|
||
}
|
||
|
||
if userPharmacistInfo.SignImage == "" {
|
||
return false, errors.New("缺少签名图片,无法申请")
|
||
}
|
||
|
||
cardNum = userPharmacistInfo.CardNum
|
||
signImagePath = strings.TrimLeft(userPharmacistInfo.SignImage, "/")
|
||
}
|
||
|
||
// 检测是否存在云证书
|
||
maps := make(map[string]interface{})
|
||
maps["ca_pin"] = entityId
|
||
maps["type"] = 2
|
||
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
||
if userCaCert == nil {
|
||
return false, errors.New("医生未申请云证书,请申请后添加签章配置")
|
||
}
|
||
|
||
if userCaCert.IsSignConfig == 1 {
|
||
return false, errors.New("医生已存在签章配置,请勿重复添加")
|
||
}
|
||
|
||
// 下载签章图片
|
||
var style string
|
||
if req.Type == 1 {
|
||
style = "image/resize,w_300,h_300"
|
||
} else {
|
||
style = "image/resize,m_lfit,w_100,h_350"
|
||
}
|
||
|
||
signImage, err := aliyun.GetCusTomObjectToRAM(signImagePath, style)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
signImage = base64.StdEncoding.EncodeToString([]byte(signImage))
|
||
|
||
// 签章配置,JSON
|
||
var signParam map[string]interface{}
|
||
var signParams []map[string]interface{}
|
||
|
||
// 医院
|
||
if req.Type == 1 {
|
||
signParam = map[string]interface{}{
|
||
"llx": "370",
|
||
"lly": "210",
|
||
"urx": "520",
|
||
"ury": "360",
|
||
}
|
||
}
|
||
|
||
// 医生
|
||
if req.Type == 2 {
|
||
signParam = map[string]interface{}{
|
||
"llx": "120",
|
||
"lly": "190",
|
||
"urx": "190",
|
||
"ury": "140",
|
||
}
|
||
}
|
||
|
||
// 药师
|
||
if req.Type == 2 {
|
||
signParam = map[string]interface{}{
|
||
"llx": "350",
|
||
"lly": "190",
|
||
"urx": "440",
|
||
"ury": "140",
|
||
}
|
||
}
|
||
|
||
signParam["pageList"] = []int{1}
|
||
signParam["sealImg"] = signImage
|
||
|
||
signParams = append(signParams, signParam)
|
||
|
||
signParamJson, err := json.Marshal(signParams)
|
||
if err != nil {
|
||
return false, err
|
||
}
|
||
|
||
// 添加签章配置
|
||
cloudCertRequestData := &ca.AddUserSignConfigRequest{
|
||
UserId: entityId,
|
||
ConfigKey: entityId,
|
||
CertSn: cardNum,
|
||
SignParam: string(signParamJson),
|
||
SealImg: signImage,
|
||
}
|
||
|
||
fmt.Println(cloudCertRequestData)
|
||
_, err = ca.AddUserSignConfig(cloudCertRequestData)
|
||
if err != nil {
|
||
return false, errors.New(err.Error())
|
||
}
|
||
|
||
// 修改ca监管证书表
|
||
data := make(map[string]interface{})
|
||
data["is_sign_config"] = 1
|
||
data["sign_config"] = string(signParamJson)
|
||
err = userCaCertDao.EditUserCaCertById(tx, userCaCert.CertId, data)
|
||
if err != nil {
|
||
return false, errors.New("修改签证配置失败")
|
||
}
|
||
|
||
return true, nil
|
||
}
|
||
|
||
// GetHospitalCloudCert 获取医院云证书数据
|
||
func (r *UserCaCertService) GetHospitalCloudCert() (g *dto.UserCaCertDto, err error) {
|
||
var userId int64 = 5345345461
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 检测是否存在云证书
|
||
maps := make(map[string]interface{})
|
||
maps["is_system"] = 1
|
||
maps["ca_pin"] = userId
|
||
maps["type"] = 2
|
||
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
||
g = dto.GetUserCaCertDto(userCaCert)
|
||
|
||
return g, nil
|
||
}
|
||
|
||
// UserCloudCert 补充所有用户云证书数据-纯接口,无调用
|
||
func (r *UserCaCertService) UserCloudCert() (bool, error) {
|
||
tx := global.Db.Begin()
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
tx.Rollback()
|
||
}
|
||
}()
|
||
|
||
// 获取所有用户
|
||
userDao := dao.UserDao{}
|
||
|
||
maps := make(map[string]interface{})
|
||
maps["user_type"] = 2
|
||
maps["user_status"] = 1
|
||
users, err := userDao.GetUserList(maps)
|
||
if len(users) == 0 || err != nil {
|
||
tx.Rollback()
|
||
return false, errors.New("无用户数据")
|
||
}
|
||
|
||
for _, user := range users {
|
||
userCaCertDao := dao.UserCaCertDao{}
|
||
|
||
// 获取医生数据
|
||
userDoctorDao := dao.UserDoctorDao{}
|
||
userDoctor, err := userDoctorDao.GetUserDoctorByUserId(user.UserId)
|
||
if err != nil || userDoctor == nil {
|
||
utils.LogJsonErr("UserCaCertService-UserCloudCert", userDoctor.UserName+"医生数据错误")
|
||
continue
|
||
}
|
||
|
||
// 判断医生状态
|
||
if userDoctor.IdenAuthStatus != 1 {
|
||
utils.LogJsonInfo("UserCaCertService-UserCloudCert", userDoctor.UserName+"医生未完成身份认证,无需申请")
|
||
continue
|
||
}
|
||
|
||
// 检测是否存在云证书
|
||
maps := make(map[string]interface{})
|
||
maps["user_id"] = user.UserId
|
||
maps["type"] = 2
|
||
userCaCert, _ := userCaCertDao.GetUserCaCert(maps)
|
||
if userCaCert != nil {
|
||
utils.LogJsonInfo("UserCaCertService-UserCloudCert", userDoctor.UserName+"医生已申请云证书")
|
||
continue
|
||
}
|
||
|
||
// 获取医生详情数据
|
||
userDoctorInfoDao := dao.UserDoctorInfoDao{}
|
||
userDoctorInfo, err := userDoctorInfoDao.GetUserDoctorInfoByDoctorId(userDoctor.DoctorId)
|
||
if err != nil || userDoctorInfo == nil {
|
||
utils.LogJsonErr("UserCaCertService-UserCloudCert", userDoctor.UserName+"医生详情数据错误")
|
||
continue
|
||
}
|
||
|
||
// 获取自定义科室数据
|
||
hospitalDepartmentCustomDao := dao.HospitalDepartmentCustomDao{}
|
||
hospitalDepartmentCustom, err := hospitalDepartmentCustomDao.GetHospitalDepartmentCustomById(userDoctor.DepartmentCustomId)
|
||
if err != nil || hospitalDepartmentCustom == nil {
|
||
utils.LogJsonErr("UserCaCertService-UserCloudCert", userDoctor.UserName+"自定义科室数据错误")
|
||
continue
|
||
}
|
||
|
||
// 获取标准科室数据
|
||
hospitalDepartmentDao := dao.HospitalDepartment{}
|
||
hospitalDepartment, err := hospitalDepartmentDao.GetHospitalDepartmentById(hospitalDepartmentCustom.DepartmentId)
|
||
if err != nil || hospitalDepartment == nil {
|
||
utils.LogJsonErr("UserCaCertService-UserCloudCert", userDoctor.UserName+"标准科室数据错误")
|
||
continue
|
||
}
|
||
|
||
// 申请云证书
|
||
cloudCertRequestData := &ca.AddCloudCertRequest{
|
||
EntityId: fmt.Sprintf("%d", user.UserId),
|
||
EntityType: "Personal",
|
||
PersonalPhone: user.Mobile,
|
||
PersonalName: userDoctorInfo.CardName,
|
||
PersonalIdNumber: userDoctorInfo.CardNum,
|
||
OrgName: "成都金牛欣欣相照互联网医院有限公司",
|
||
OrgNumber: "91510106MABTJY4K9R",
|
||
Pin: fmt.Sprintf("%d", user.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 {
|
||
utils.LogJsonErr("UserCaCertService-UserCloudCert", err.Error())
|
||
continue
|
||
}
|
||
|
||
// 到期时间-测试:180,正式:365
|
||
CertExpireTime := model.LocalTime(time.Now().AddDate(0, 0, 180))
|
||
if config.C.Env == "prod" {
|
||
CertExpireTime = model.LocalTime(time.Now().AddDate(0, 0, 365))
|
||
}
|
||
|
||
// 新增ca监管证书表
|
||
userCaCert = &model.UserCaCert{
|
||
UserId: &user.UserId,
|
||
IsSystem: 0,
|
||
IsLatest: 1,
|
||
Type: 2,
|
||
CertBase64: cloudCertResponse.CertBase64,
|
||
CertChainP7: cloudCertResponse.CertP7,
|
||
CertSerialNumber: cloudCertResponse.CertSerialnumber,
|
||
CaPin: fmt.Sprintf("%d", user.UserId),
|
||
IsSignConfig: 0,
|
||
SignConfig: "",
|
||
CertApplicationTime: model.LocalTime(time.Now()),
|
||
CertExpireTime: CertExpireTime, // 180天以后的时间
|
||
}
|
||
|
||
userCaCert, err = userCaCertDao.AddUserCaCert(tx, userCaCert)
|
||
if err != nil || userCaCert == nil {
|
||
utils.LogJsonErr("UserCaCertService-UserCloudCert", userDoctor.UserName+"申请成功,存储失败")
|
||
continue
|
||
}
|
||
|
||
utils.LogJsonInfo("UserCaCertService-UserCloudCert", userDoctor.UserName+"申请成功")
|
||
}
|
||
|
||
tx.Commit()
|
||
|
||
return true, nil
|
||
}
|