package sign import ( "encoding/json" "errors" "fmt" "mtp20access/global" "mtp20access/model/account" "mtp20access/model/account/request" "mtp20access/service/asign" "strconv" "time" "go.uber.org/zap" ) // QueryUserESignRecord 查询用户电子签记录表 func QueryUserESignRecord(userId int) (rsp []account.Useresignrecord, err error) { rsp = make([]account.Useresignrecord, 0) err = global.M2A_DB.Where("USERID = ?", userId).Find(&rsp) return } // AddUser 添加用户 func AddUser(req request.AddUserReq, userId int) (err error) { // 调用爱签API-添加个人用户(https://{host}/user/addPersonalUser) rsp, err := asign.AddPersonalUserBy( strconv.Itoa(userId), req.Name, req.IdCard, req.Mobile, req.IdCardType, ) if err != nil { return } if rsp.Code != 100000 { err = errors.New(strconv.Itoa(rsp.Code)) global.M2A_LOG.Error("【AddUser】 接口调用失败", zap.Error(err)) return } // 更新用户电子签记录-实名认证状态 authinfo, err := json.Marshal(req) if err != nil { global.M2A_LOG.Error("【AddUser】 构建AUTHINFO失败", zap.Error(err)) return } sql := fmt.Sprintf(` UPDATE useresignrecord SET RECORDSTATUS = 3, UPDATETIME = SYSDATE, AUTHINFO = '%v' WHERE USERID = %v `, string(authinfo), userId) if _, err = global.M2A_DB.Exec(sql); err != nil { global.M2A_LOG.Error("【AddUser】 添加用户电子签记录", zap.Error(err)) return } return } // CreateContractAndAddSigner 上传待签署文件和添加签署方 func CreateContractAndAddSigner(req request.CreateContractAndAddSignerReq, userId int) (err error) { // 获取用户电子签记录 useresignrecord := new(account.Useresignrecord) has, err := global.M2A_DB.Where("USERID = ? AND TEMPLATENO = ?", userId, req.TemplateNo).Get(useresignrecord) if err != nil || !has { global.M2A_LOG.Error("【CreateContractAndAddSigner】 获取用户电子签记录失败", zap.Error(err)) return } // 判断是否需要创建合同(上传待签署文件) if useresignrecord.CONTRACTNO == "" { // 生成合同编号 // #{userid} || '_' || to_char(sysdate, 'yyyyMMddhh24miss') || '_' || seq_useresignrecord.currval, contractNo := fmt.Sprintf("%d_%s_%v", userId, time.Now().Format("2006-01-02 15:04:05"), useresignrecord.RECORDID) // 调用爱签API-上传待签署文件(https://{host}/contract/createContract) rsp, e := asign.CreateContract( contractNo, useresignrecord.TEMPLATENAME, useresignrecord.TEMPLATENO, ) if e != nil { err = e return } if rsp.Code != 100000 { err = errors.New(strconv.Itoa(rsp.Code)) global.M2A_LOG.Error("【CreateContractAndAddSigner】 上传待签署文件接口调用失败", zap.Error(err)) return } // 将返回的合同编号写入数据库 useresignrecord.CONTRACTNO = contractNo sql := fmt.Sprintf(` UPDATE useresignrecord SET SIGNURL = '%v' UPDATETIME = SYSDATE WHERE RECORDID = %v `, useresignrecord.CONTRACTNO, useresignrecord.RECORDID) if _, err = global.M2A_DB.Exec(sql); err != nil { global.M2A_LOG.Error("【CreateContractAndAddSigner】 写入合同编号失败", zap.Error(err)) return } } // 判断是否需要添加签署方(获取合同签约地址) if useresignrecord.SIGNURL == "" { // 调用爱签API-添加签署方(https://{host}/contract/addSigner) rsp, e := asign.AddSigner( useresignrecord.CONTRACTNO, strconv.Itoa(userId), ) if e != nil { err = e return } if rsp.Code != 100000 { err = errors.New(strconv.Itoa(rsp.Code)) global.M2A_LOG.Error("【CreateContractAndAddSigner】 添加签署方接口调用失败", zap.Error(err)) return } if len(rsp.Data.SignUser) > 0 { useresignrecord.SIGNURL = rsp.Data.SignUser[0].SignUrl } // 将返回的合同编号写入数据库 sql := fmt.Sprintf(` UPDATE useresignrecord SET SIGNURL = '%v' UPDATETIME = SYSDATE WHERE RECORDID = %v `, useresignrecord.SIGNURL, useresignrecord.RECORDID) if _, err = global.M2A_DB.Exec(sql); err != nil { global.M2A_LOG.Error("【CreateContractAndAddSigner】 写入合同签署链接失败", zap.Error(err)) return } } return }