github.com/SupenBysz/gf-admin-community@v0.7.4/internal/logic/sys_person_lincense/sys_person_license.go (about)

     1  package sys_person_lincense
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"github.com/SupenBysz/gf-admin-community/sys_model"
     7  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_dao"
     8  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_do"
     9  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_entity"
    10  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_enum"
    11  	"github.com/SupenBysz/gf-admin-community/sys_service"
    12  	"github.com/SupenBysz/gf-admin-community/utility/funs"
    13  	"github.com/gogf/gf/v2/database/gdb"
    14  	"github.com/gogf/gf/v2/encoding/gjson"
    15  	"github.com/gogf/gf/v2/errors/gcode"
    16  	"github.com/gogf/gf/v2/errors/gerror"
    17  	"github.com/gogf/gf/v2/os/gtime"
    18  	"github.com/gogf/gf/v2/text/gstr"
    19  	"github.com/gogf/gf/v2/util/gconv"
    20  	"github.com/kysion/base-library/base_model"
    21  	"github.com/kysion/base-library/utility/daoctl"
    22  	"github.com/kysion/base-library/utility/masker"
    23  	"github.com/yitter/idgenerator-go/idgen"
    24  	"time"
    25  )
    26  
    27  // 个人资质相关
    28  type sSysPersonLicense struct {
    29  	conf gdb.CacheOption
    30  }
    31  
    32  func init() {
    33  	sys_service.RegisterSysPersonLicense(NewSysPersonLicense())
    34  }
    35  
    36  func NewSysPersonLicense() *sSysPersonLicense {
    37  	result := &sSysPersonLicense{
    38  		conf: gdb.CacheOption{
    39  			Duration: time.Hour,
    40  			Force:    false,
    41  		},
    42  	}
    43  
    44  	// 订阅审核Hook,审核通过添加个人资质信息
    45  	sys_service.SysAudit().InstallHook(sys_enum.Audit.Action.Approve, sys_enum.Audit.Category.PersonLicenseAudit.Code(), result.AuditChange)
    46  
    47  	// 订阅审核数据获取Hook, 将审核数据渲染成个人资质然后进行输出
    48  	sys_service.SysAudit().InstallHook(sys_enum.Audit.Action.Approve, sys_enum.Audit.Category.PersonLicenseAudit.Code(), result.GetAuditData)
    49  
    50  	return result
    51  }
    52  
    53  // GetAuditData 订阅审核数据获取Hook, 将审核数据渲染成个人资质然后进行输出
    54  func (s *sSysPersonLicense) GetAuditData(ctx context.Context, auditEvent sys_enum.AuditEvent, info *sys_entity.SysAudit) error {
    55  	//  处理审核
    56  	if info == nil {
    57  		return sys_service.SysLogs().ErrorSimple(ctx, nil, "审核数据为空", "Audit")
    58  	}
    59  	if (auditEvent.Code() & sys_enum.Audit.Event.GetAuditData.Code()) == sys_enum.Audit.Event.GetAuditData.Code() {
    60  		if (info.Category & sys_enum.Audit.Category.PersonLicenseAudit.Code()) == sys_enum.Audit.Category.PersonLicenseAudit.Code() {
    61  			auditData := sys_model.AuditPersonLicense{}
    62  
    63  			//解析json字符串
    64  			gjson.DecodeTo(info.AuditData, &auditData)
    65  			if auditData.No == "" { // 说明不是默认结构,业务层自己封装了结构
    66  				return nil
    67  			}
    68  
    69  			// 还未审核的图片从缓存中寻找  0 缓存  1 数据库
    70  
    71  			// 将路径id换成可访问图片的url
    72  			{
    73  				tempIdcardFrontPath := ""
    74  				if gstr.IsNumeric(auditData.IdcardFrontPath) {
    75  					if uploadFile, err := sys_service.File().GetUploadFile(ctx, gconv.Int64(auditData.IdcardFrontPath), auditData.UserId); err == nil && uploadFile != nil {
    76  						tempIdcardFrontPath = uploadFile.Src
    77  					}
    78  				}
    79  
    80  				if tempIdcardFrontPath != "" {
    81  					auditData.IdcardFrontPath = sys_service.File().MakeFileUrlByPath(ctx, tempIdcardFrontPath)
    82  				}
    83  			}
    84  
    85  			{
    86  				tempIdcardBackPath := ""
    87  				if gstr.IsNumeric(auditData.IdcardBackPath) {
    88  					if uploadFile, err := sys_service.File().GetUploadFile(ctx, gconv.Int64(auditData.IdcardBackPath), auditData.UserId); err == nil && uploadFile != nil {
    89  						tempIdcardBackPath = uploadFile.Src
    90  					}
    91  				}
    92  
    93  				if tempIdcardBackPath != "" {
    94  					auditData.IdcardBackPath = sys_service.File().MakeFileUrlByPath(ctx, tempIdcardBackPath)
    95  				}
    96  			}
    97  
    98  			if auditData.No != "" { // 说明是默认结构
    99  				// 重新赋值  将id转为可访问路径
   100  				info.AuditData = gjson.MustEncodeString(auditData)
   101  			} else { // 业务层自己自定义的审核机构,业务层自己解析即可
   102  
   103  			}
   104  
   105  		}
   106  	}
   107  	return nil
   108  }
   109  
   110  // AuditChange 审核成功的处理逻辑 Hook
   111  func (s *sSysPersonLicense) AuditChange(ctx context.Context, auditEvent sys_enum.AuditEvent, info *sys_entity.SysAudit) error {
   112  	//data := sys_service.SysAudit().GetAuditById(ctx, info.Id)
   113  	//if data == nil {
   114  	//	return sys_service.SysLogs().ErrorSimple(ctx, nil, "获取审核信息失败", sys_dao.SysAudit.Table())
   115  	//}
   116  
   117  	//  处理审核
   118  	if (auditEvent.Code() & sys_enum.Audit.Event.ExecAudit.Code()) == sys_enum.Audit.Event.ExecAudit.Code() {
   119  		// 审核通过
   120  		if (info.State & sys_enum.Audit.Action.Approve.Code()) == sys_enum.Audit.Action.Approve.Code() {
   121  			// 创建个人资质
   122  			auditPersonLicense := sys_model.AuditPersonLicense{}
   123  			gjson.DecodeTo(info.AuditData, &auditPersonLicense)
   124  			if auditPersonLicense.No == "" { // 业务层自己处理审核通过的逻辑
   125  				return nil
   126  			}
   127  
   128  			licenseRes, err := sys_service.SysPersonLicense().CreateLicense(ctx, auditPersonLicense)
   129  			if err != nil {
   130  				return sys_service.SysLogs().ErrorSimple(ctx, nil, "审核通过后个人资质创建失败", sys_dao.SysPersonLicense.Table())
   131  			}
   132  
   133  			// 设置个人资质的审核编号
   134  			ret, err := sys_service.SysPersonLicense().SetLicenseAuditNumber(ctx, licenseRes.Id, gconv.String(info.Id))
   135  			if err != nil || ret == false {
   136  				return sys_service.SysLogs().ErrorSimple(ctx, err, "", sys_dao.SysPersonLicense.Table())
   137  			}
   138  		}
   139  	}
   140  
   141  	return nil
   142  }
   143  
   144  // GetLicenseById  根据ID获取个人资质认证|信息
   145  func (s *sSysPersonLicense) GetLicenseById(ctx context.Context, id int64) (*sys_entity.SysPersonLicense, error) {
   146  	data := sys_entity.SysPersonLicense{}
   147  	err := sys_dao.SysPersonLicense.Ctx(ctx).Scan(&data, sys_do.SysPersonLicense{Id: id})
   148  	if err != nil {
   149  		return nil, sys_service.SysLogs().ErrorSimple(ctx, err, "个人资质信息不存在", sys_dao.SysPersonLicense.Table())
   150  	}
   151  
   152  	// 需要将持久化的文件ID替换成可访问的接口URL
   153  	s.buildURL(ctx, &data)
   154  
   155  	return &data, nil
   156  }
   157  
   158  // QueryLicenseList  查询个人资质认证|列表
   159  func (s *sSysPersonLicense) QueryLicenseList(ctx context.Context, search base_model.SearchParams) (*sys_model.PersonLicenseListRes, error) {
   160  	result, err := daoctl.Query[sys_entity.SysPersonLicense](sys_dao.SysPersonLicense.Ctx(ctx), &search, false)
   161  
   162  	if err != nil {
   163  		return &sys_model.PersonLicenseListRes{}, err
   164  	}
   165  
   166  	response := sys_model.PersonLicenseListRes{}
   167  	for _, record := range result.Records {
   168  		// 需要将持久化的文件ID替换成可访问的接口URL
   169  		s.buildURL(ctx, &record)
   170  
   171  		response.Records = append(response.Records, record)
   172  	}
   173  	response.PaginationRes = result.PaginationRes
   174  
   175  	return &response, err
   176  	//return (*sys_model.PersonLicenseListRes)(&result), err
   177  }
   178  
   179  // CreateLicense  新增个人资质|信息
   180  func (s *sSysPersonLicense) CreateLicense(ctx context.Context, info sys_model.AuditPersonLicense) (*sys_entity.SysPersonLicense, error) {
   181  	result := sys_entity.SysPersonLicense{}
   182  	gconv.Struct(info, &result)
   183  
   184  	if info.LicenseId == 0 {
   185  		result.Id = idgen.NextId()
   186  	} else {
   187  		result.Id = info.LicenseId
   188  	}
   189  
   190  	//result.State = 0
   191  	//result.AuthType = 0
   192  	result.CreatedAt = gtime.Now()
   193  
   194  	// TODO 校验
   195  	{
   196  		if info.IdcardFrontPath != "" && info.IdcardBackPath != "" {
   197  			// 指针传递,会在方法内部修改result的值
   198  			_, err := funs.CheckPersonLicenseFiles(ctx, info, &result)
   199  
   200  			if err != nil {
   201  				return nil, err
   202  			}
   203  
   204  		}
   205  	}
   206  
   207  	{
   208  		// 创建资质信息
   209  		_, err := sys_dao.SysPersonLicense.Ctx(ctx).Insert(result)
   210  
   211  		if err != nil {
   212  			return nil, sys_service.SysLogs().ErrorSimple(ctx, err, "新增资质信息失败", sys_dao.SysPersonLicense.Table())
   213  		}
   214  
   215  		if err != nil {
   216  			return nil, err
   217  		}
   218  	}
   219  
   220  	// 需要将持久化的文件ID替换成可访问的接口URL
   221  	s.buildURL(ctx, &result)
   222  
   223  	return &result, nil
   224  }
   225  
   226  // UpdateLicense  更新个人资质认证,如果是已经通过的认证,需要重新认证通过后才生效|信息
   227  func (s *sSysPersonLicense) UpdateLicense(ctx context.Context, info sys_model.AuditPersonLicense, id int64) (*sys_entity.SysPersonLicense, error) {
   228  	data := sys_entity.SysPersonLicense{}
   229  	err := sys_dao.SysPersonLicense.Ctx(ctx).Scan(&data, sys_do.SysPersonLicense{Id: id})
   230  	if err != nil {
   231  		return nil, sys_service.SysLogs().ErrorSimple(ctx, err, "操作失败,资质信息不存在", sys_dao.SysPersonLicense.Table())
   232  	}
   233  
   234  	if data.State == -1 {
   235  		return nil, sys_service.SysLogs().ErrorSimple(ctx, gerror.NewCode(gcode.CodeNil, "操作是不,资质信息被冻结,禁止修改"), "", sys_dao.SysPersonLicense.Table())
   236  	}
   237  
   238  	newData := sys_do.SysPersonLicense{}
   239  
   240  	gconv.Struct(info, &newData)
   241  
   242  	// TODO 校验
   243  	{
   244  		_, err := funs.CheckPersonLicenseFiles(ctx, info, &newData)
   245  		if err != nil {
   246  			return nil, err
   247  		}
   248  	}
   249  
   250  	err = sys_dao.SysPersonLicense.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
   251  
   252  		newAudit := sys_do.SysAudit{
   253  			Id:          idgen.NextId(),
   254  			State:       0,
   255  			UnionMainId: data.Id,
   256  			Category:    1,
   257  			AuditData:   gjson.MustEncodeString(data),
   258  			ExpireAt:    gtime.Now().Add(time.Hour * 24 * 7),
   259  		}
   260  
   261  		{
   262  			audit := sys_service.SysAudit().GetAuditById(ctx, data.LatestAuditLogId)
   263  			// 未审核通过的资质资质,直接更改待审核的资质信息
   264  			if audit != nil && audit.State == 0 {
   265  				_, err := tx.Ctx(ctx).Model(sys_dao.SysPersonLicense.Table()).Where(sys_do.SysPersonLicense{Id: id}).OmitNil().Save(&newData)
   266  				if err != nil {
   267  					return sys_service.SysLogs().ErrorSimple(ctx, err, "操作失败,更新资质信息失败", sys_dao.SysPersonLicense.Table())
   268  				}
   269  
   270  				// 更新待审核的审核信息
   271  				newAudit.Id = audit.Id
   272  				_, err = sys_dao.SysAudit.Ctx(ctx).Data(newAudit).Where(sys_do.SysAudit{Id: audit.Id}).Update()
   273  				if err != nil {
   274  					return sys_service.SysLogs().ErrorSimple(ctx, err, "更新审核信息失败", sys_dao.SysPersonLicense.Table())
   275  				}
   276  				return nil
   277  			}
   278  		}
   279  
   280  		return nil
   281  	})
   282  	if err != nil {
   283  		return nil, err
   284  	}
   285  
   286  	return s.GetLicenseById(ctx, id)
   287  }
   288  
   289  // GetLicenseByLatestAuditId  获取最新的审核记录Id获取资质信息
   290  func (s *sSysPersonLicense) GetLicenseByLatestAuditId(ctx context.Context, auditId int64) *sys_entity.SysPersonLicense {
   291  	result := sys_entity.SysPersonLicense{}
   292  	err := sys_dao.SysPersonLicense.Ctx(ctx).Where(sys_do.SysPersonLicense{LatestAuditLogId: auditId}).OrderDesc(sys_dao.SysPersonLicense.Columns().CreatedAt).Limit(1).Scan(&result)
   293  	if err != nil {
   294  		return nil
   295  	}
   296  
   297  	// 需要将持久化的文件ID替换成可访问的接口URL
   298  	s.buildURL(ctx, &result)
   299  
   300  	//return &result
   301  	return s.Masker(&result)
   302  }
   303  
   304  // SetLicenseState  设置个人资质信息状态 -1未通过 0待审核 1通过
   305  func (s *sSysPersonLicense) SetLicenseState(ctx context.Context, id int64, state int) (bool, error) {
   306  	data := sys_entity.SysPersonLicense{}
   307  	err := sys_dao.SysPersonLicense.Ctx(ctx).Scan(&data, sys_do.SysPersonLicense{Id: id})
   308  
   309  	if err != nil {
   310  		return false, sys_service.SysLogs().ErrorSimple(ctx, err, "操作失败,资质信息不存在", sys_dao.SysPersonLicense.Table())
   311  	}
   312  
   313  	_, err = sys_dao.SysPersonLicense.Ctx(ctx).Data(sys_do.SysPersonLicense{State: state, UpdatedAt: gtime.Now()}).OmitNilData().Where(sys_do.SysPersonLicense{Id: id}).Update()
   314  
   315  	if err != nil {
   316  		return false, sys_service.SysLogs().ErrorSimple(ctx, err, "更新个人资质状态信息失败", sys_dao.SysPersonLicense.Table())
   317  	}
   318  
   319  	return true, nil
   320  }
   321  
   322  // SetLicenseAuditNumber  设置个人资质神审核编号
   323  func (s *sSysPersonLicense) SetLicenseAuditNumber(ctx context.Context, id int64, auditNumber string) (bool, error) {
   324  	data := sys_entity.SysPersonLicense{}
   325  	err := sys_dao.SysPersonLicense.Ctx(ctx).Scan(&data, sys_do.SysPersonLicense{Id: id})
   326  	if err != nil {
   327  		return false, sys_service.SysLogs().ErrorSimple(ctx, err, "操作失败,资质信息不存在", sys_dao.SysPersonLicense.Table())
   328  	}
   329  
   330  	_, err = sys_dao.SysPersonLicense.Ctx(ctx).Data(sys_do.SysPersonLicense{LatestAuditLogId: auditNumber, UpdatedAt: gtime.Now()}).OmitNilData().Where(sys_do.SysPersonLicense{Id: id}).Update()
   331  
   332  	if err != nil {
   333  		return false, sys_service.SysLogs().ErrorSimple(ctx, err, "更新个人资质证照审核编号失败", sys_dao.SysPersonLicense.Table())
   334  	}
   335  	return true, nil
   336  }
   337  
   338  // DeleteLicense  删除个人资质
   339  func (s *sSysPersonLicense) DeleteLicense(ctx context.Context, id int64, flag bool) (bool, error) {
   340  	return false, nil
   341  }
   342  
   343  // UpdateLicenseAuditLogId  设置个人资质资质关联的审核ID
   344  func (s *sSysPersonLicense) UpdateLicenseAuditLogId(ctx context.Context, id int64, latestAuditLogId int64) (bool, error) {
   345  	auditLog := sys_service.SysAudit().GetAuditById(ctx, latestAuditLogId)
   346  	if nil == auditLog {
   347  		return false, sys_service.SysLogs().ErrorSimple(ctx, nil, "资质信息校验失败", sys_dao.SysPersonLicense.Table())
   348  	}
   349  
   350  	audit := sys_model.AuditPersonLicense{}
   351  
   352  	err := gjson.DecodeTo(auditLog.AuditData, &audit)
   353  
   354  	if err != nil || audit.LicenseId != id {
   355  		return false, sys_service.SysLogs().ErrorSimple(ctx, err, "资质校验失败", sys_dao.SysPersonLicense.Table())
   356  	}
   357  
   358  	// 构建资质对象
   359  	license := sys_entity.SysPersonLicense{}
   360  	// 加载资质信息
   361  	err = sys_dao.SysPersonLicense.Ctx(ctx).Scan(&license, sys_do.SysPersonLicense{Id: id})
   362  	// 如果资质不存在则无需更新,直接返回
   363  	if err == sql.ErrNoRows {
   364  		return true, nil
   365  	}
   366  	if err != nil {
   367  		return false, err
   368  	}
   369  
   370  	// 将新创建的个人资质认证信息关联至个人资质
   371  	_, err = sys_dao.SysPersonLicense.Ctx(ctx).
   372  		Data(sys_do.SysPersonLicense{LatestAuditLogId: latestAuditLogId, UpdatedAt: gtime.Now()}).
   373  		Where(sys_do.SysPersonLicense{Id: id}).
   374  		Update()
   375  
   376  	return err == nil, err
   377  }
   378  
   379  // Masker  个人资质信息脱敏
   380  func (s *sSysPersonLicense) Masker(license *sys_entity.SysPersonLicense) *sys_entity.SysPersonLicense {
   381  	license.No = masker.MaskString(license.No, masker.IDCard)
   382  	license.Name = masker.MaskString(license.Name, masker.Other)
   383  
   384  	return license
   385  }
   386  
   387  // buildURL 将文件id替换成可访问的URL
   388  func (s *sSysPersonLicense) buildURL(ctx context.Context, data *sys_entity.SysPersonLicense) {
   389  	{
   390  		//tempIdcardFrontPath := ""
   391  		if gstr.IsNumeric(data.IdcardFrontPath) {
   392  			// License情况1: 已经审核过,存储的License中的文件ID都是持久化后的, 直接找SysFile数据库即可
   393  			//if sysFile, err := sys_service.File().GetFileById(ctx, gconv.Int64(data.IdcardFrontPath), "根据文件ID查询文件失败"); err == nil && sysFile != nil {
   394  			//	tempIdcardFrontPath = sysFile.Src
   395  			//}
   396  			//
   397  
   398  			// License情况2:
   399  			data.IdcardFrontPath = sys_service.File().MakeFileUrl(ctx, gconv.Int64(data.IdcardFrontPath))
   400  
   401  			// Audit情况: 没有审核过,存储的audit中的文件ID需要从缓存获取
   402  			//if uploadFile, err := sys_service.File().GetUploadFile(ctx, gconv.Int64(data.IdcardFrontPath), data.UserId); err == nil && uploadFile != nil {
   403  			//	tempIdcardFrontPath = uploadFile.Src
   404  			//}
   405  			//data.IdcardFrontPath = sys_service.File().MakeFileUrlByPath(ctx, tempIdcardFrontPath)
   406  		}
   407  
   408  	}
   409  	{
   410  		if gstr.IsNumeric(data.IdcardBackPath) {
   411  			data.IdcardBackPath = sys_service.File().MakeFileUrl(ctx, gconv.Int64(data.IdcardBackPath))
   412  		}
   413  
   414  	}
   415  
   416  	//return data
   417  }