190 lines
5.2 KiB
Go
190 lines
5.2 KiB
Go
package logic
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
|
||
"github.com/fonchain/fonchain-artistinfo/cmd/internal/dao"
|
||
"github.com/fonchain/fonchain-artistinfo/cmd/model"
|
||
"github.com/fonchain/fonchain-artistinfo/pb/artist"
|
||
"github.com/fonchain/fonchain-artistinfo/pb/contract"
|
||
db "github.com/fonchain/fonchain-artistinfo/pkg/db"
|
||
"github.com/fonchain/fonchain-artistinfo/pkg/m"
|
||
"github.com/fonchain/fonchain-artistinfo/pkg/service"
|
||
uuid "github.com/satori/go.uuid"
|
||
"go.uber.org/zap"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
type IContract interface {
|
||
CreateContract(req *contract.CreateContractRequest) (rep *contract.CreateContractRespond, err error)
|
||
FinishContract(req *contract.FinishContractRequest) (rep *contract.FinishContractRespond, err error)
|
||
ContractList(req *contract.ContractListRequest) (rep *contract.ContractListRespond, err error)
|
||
ContractTxList(req *contract.ContractTxListRequest) (rep *contract.ContractTxListRespond, err error)
|
||
UpdateContract(req *contract.UpdateContractRequest) (rep *contract.UpdateContractRespond, err error)
|
||
UpdateContractTx(req *contract.UpdateContractTxRequest) (rep *contract.UpdateContractTxRespond, err error)
|
||
GetContract(req *contract.GetContractRequest) (rep *contract.ContractData, err error)
|
||
}
|
||
|
||
func NewContract() IContract {
|
||
return &Contract{}
|
||
}
|
||
|
||
type Contract struct {
|
||
}
|
||
|
||
func (a *Contract) CreateContract(req *contract.CreateContractRequest) (rep *contract.CreateContractRespond, err error) {
|
||
rep = &contract.CreateContractRespond{}
|
||
//查看画家是否存在
|
||
if _, err := service.GrpcArtistImpl.ArtistInfo(context.Background(), &artist.ArtistInfoRequest{Uid: req.ArtistUid}); err != nil {
|
||
zap.L().Error("update airDrop err", zap.Error(err))
|
||
return rep, errors.New(m.ERROR_UPDATE + err.Error())
|
||
}
|
||
|
||
//遇到错误数据库回滚
|
||
tx := db.DB.Begin()
|
||
|
||
switch req.Type {
|
||
//1 画家合同(不涉及画家的画作)
|
||
case 1:
|
||
|
||
//查看画家合同是否已经存在,不存在继续
|
||
if err = dao.GetMgmtContractByArtistUid(req.ArtistUid, req.Type); err == gorm.ErrRecordNotFound {
|
||
var uid uuid.UUID
|
||
if uid, err = uuid.NewV4(); err != nil {
|
||
err = errors.New(m.ERROR_UID)
|
||
return
|
||
}
|
||
|
||
//创建画家合同
|
||
contract := &model.Contract{
|
||
Uid: uid.String(),
|
||
ArtistUid: req.ArtistUid,
|
||
Type: 1,
|
||
State: 1,
|
||
}
|
||
|
||
if err = dao.CreateArtistContract(tx, contract); err != nil {
|
||
return
|
||
}
|
||
} else {
|
||
return
|
||
}
|
||
|
||
case 2:
|
||
for _, v := range req.ArtworkUid {
|
||
//查看画作合同是否已经存在,不存在继续,类型2、6
|
||
if err = dao.GetMgmtContractByArtworkUid(v, 2); err == gorm.ErrRecordNotFound {
|
||
if err = dao.GetMgmtContractByArtworkUid(v, 6); err == gorm.ErrRecordNotFound {
|
||
|
||
//创建画家画作合同,类型2
|
||
if err = dao.CreateArtworkContract(tx, v, req.ArtistUid, 2); err != nil {
|
||
tx.Rollback()
|
||
return
|
||
}
|
||
|
||
//创建画家画作合同,类型6
|
||
if err = dao.CreateArtworkContract(tx, v, req.ArtistUid, 6); err != nil {
|
||
tx.Rollback()
|
||
return
|
||
}
|
||
|
||
} else {
|
||
return
|
||
}
|
||
} else {
|
||
return
|
||
}
|
||
}
|
||
|
||
case 3:
|
||
|
||
case 5:
|
||
|
||
default:
|
||
err = errors.New("type值出错!")
|
||
return
|
||
|
||
}
|
||
|
||
tx.Commit()
|
||
return
|
||
}
|
||
|
||
// func (a *Contract) FinishContract(req *contract.FinishContractRequest) (rep *contract.FinishContractRespond, err error) {
|
||
// rep = &contract.FinishContractRespond{}
|
||
|
||
// tx := db.DB.Begin()
|
||
// //更新合同状态并获取合同信息
|
||
// contract, err := dao.FinishContract(tx, req.TransactionId)
|
||
// if err != nil {
|
||
// tx.Rollback()
|
||
// return nil, err
|
||
// }
|
||
|
||
// //查看画作是否已经状态改变,没有则改变 无用
|
||
// if err = dao.UpdateArtworkState(tx, contract.ID); err != nil {
|
||
// tx.Rollback()
|
||
// return
|
||
// }
|
||
|
||
// var tmp struct {
|
||
// Uid int32 `json:"uid"`
|
||
// }
|
||
// tmp.Uid = contract.BatchId
|
||
// tmpByte, _ := json.Marshal(&tmp)
|
||
|
||
// if contract.Type == 4 {
|
||
// _, err = util.Post("https://ms.fontree.cn/mgmt/artworktx/updatebatchtype", string(tmpByte))
|
||
// if err != nil {
|
||
// zap.L().Error("post request err", zap.Error(err))
|
||
// return
|
||
// }
|
||
// }
|
||
|
||
// if contract.Type == 7 {
|
||
// _, err = util.Post("https://ms.fontree.cn/mgmt/artworktx/updatebatchtypecopy", string(tmpByte))
|
||
// if err != nil {
|
||
// zap.L().Error("post request err", zap.Error(err))
|
||
// return
|
||
// }
|
||
// }
|
||
|
||
// tx.Commit()
|
||
// return
|
||
// }
|
||
|
||
// func (a *Contract) ContractList(req *contract.ContractListRequest) (rep *contract.ContractListRespond, err error) {
|
||
|
||
// rep, err = dao.ContractList(req)
|
||
|
||
// return
|
||
// }
|
||
|
||
// func (a *Contract) GetContract(req *contract.GetContractRequest) (rep *contract.ContractData, err error) {
|
||
|
||
// rep, err = dao.GetContract(int32(req.Id))
|
||
|
||
// return
|
||
// }
|
||
// func (a *Contract) ContractTxList(req *contract.ContractTxListRequest) (rep *contract.ContractTxListRespond, err error) {
|
||
|
||
// rep, err = dao.ContractTxList(req)
|
||
|
||
// return
|
||
// }
|
||
|
||
// func (a *Contract) UpdateContract(req *contract.UpdateContractRequest) (rep *contract.UpdateContractRespond, err error) {
|
||
// rep = &contract.UpdateContractRespond{}
|
||
// err = dao.UpdateContract(req)
|
||
|
||
// return
|
||
// }
|
||
|
||
// func (a *Contract) UpdateContractTx(req *contract.UpdateContractTxRequest) (rep *contract.UpdateContractTxRespond, err error) {
|
||
// rep = &contract.UpdateContractTxRespond{}
|
||
// err = dao.UpdateContractTx(req.TransactionId, int32(req.ID))
|
||
|
||
// return
|
||
// }
|