123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package repo
- import (
- "bytes"
- "encoding/json"
- "errors"
- "io/ioutil"
- "mats/conf"
- "mats/db/model"
- "net/http"
- "time"
- "go.mongodb.org/mongo-driver/bson"
- )
- type IRepoAsset interface {
- Upload(ctx *RepoSession, userId string) (interface{}, error)
- Update(ctx *RepoSession) (interface{}, error)
- Detail(ctx *RepoSession, id string) (interface{}, error)
- GetEntity() interface{}
- }
- func RepoAssetCreateDefaultValue(asset *conf.AppAsset) IRepoAsset {
- switch asset.Type {
- case "decorate":
- return &AssetDecorateMeshRepo{Conf: asset, Model: &model.AssetDecorateMesh{}}
- case "texture":
- return &AssetTextureRepo{Conf: asset, Model: &model.AssetTexture{}}
- case "env3d":
- return &AssetEnv3dRepo{Conf: asset, Model: &model.Env3d{}}
- case "boxtpl":
- return &BoxtplRepo{Conf: asset, Model: &model.Boxtpl{}}
- case "material":
- return &MaterialRepo{Conf: asset, Model: &model.AssetMaterial{}}
- }
- return nil
- }
- func CreateMeshConvTask(ctx *RepoSession, file *model.OssType, meshId string, collecton string, userId string) (interface{}, error) {
- taskConfig := &model.TaskConvMesh2Osgjs{
- CreateTime: time.Now(),
- FbxFile: file,
- OsgjsFieldName: "osgjs",
- MeshId: meshId,
- Collecton: collecton,
- UserId: userId,
- }
- taskId, err := RepoAddDoc(ctx, CollectionTaskMeshConv, taskConfig)
- if err != nil {
- return nil, err
- }
- //生成worker请求
- req := &model.WorkerRequest{WorkerType: "osgconverter", MainId: meshId, TaskCollection: CollectionTaskMeshConv, UserId: userId, Name: "模型转换", TaskId: taskId, CreateTime: time.Now()}
- return true, ReqestTaskWorker(ctx, req, CollectionTaskMeshConv)
- }
- func CreateShadowTask(ctx *RepoSession, file *model.OssType, meshId string, collecton string, userId string) (interface{}, error) {
- taskConfig := &model.TaskCreateShadow{
- CreateTime: time.Now(),
- FbxFile: file,
- ShadowFieldName: "shadow",
- MeshId: meshId,
- Collecton: collecton,
- UserId: userId,
- }
- taskId, err := RepoAddDoc(ctx, CollectionTaskShadowCreator, taskConfig)
- if err != nil {
- return nil, err
- }
- //生成worker请求
- req := &model.WorkerRequest{WorkerType: "shadowCreator", MainId: meshId, TaskCollection: CollectionTaskShadowCreator, UserId: userId, Name: "阴影生成", TaskId: taskId, CreateTime: time.Now()}
- return true, ReqestTaskWorker(ctx, req, CollectionTaskShadowCreator)
- }
- func CreateHdrConverterTask(ctx *RepoSession, hdr *model.OssType, env3dId string, collecton string, userId string) (interface{}, error) {
- taskConfig := &model.TaskConvHdr{
- CreateTime: time.Now(),
- HdrFile: hdr,
- Env3dId: env3dId,
- Collecton: collecton,
- UserId: userId,
- }
- taskId, err := RepoAddDoc(ctx, CollectionTaskHdrConv, taskConfig)
- if err != nil {
- return nil, err
- }
- //生成worker请求
- req := &model.WorkerRequest{WorkerType: "hdrconverter", TaskCollection: CollectionTaskHdrConv, UserId: userId, Name: "环境球转换", TaskId: taskId, CreateTime: time.Now()}
- return true, ReqestTaskWorker(ctx, req, CollectionTaskHdrConv)
- }
- func ReqestTaskWorker(ctx *RepoSession, task *model.WorkerRequest, taskCollection string) error {
- taskCenterHost := conf.AppConfig.TaskCenter
- if len(taskCenterHost) < 1 {
- return errors.New("任务中心没有配置,没法执行耗时任务")
- }
- client := &http.Client{}
- bytesData, _ := json.Marshal(task)
- req, err := http.NewRequest("POST", taskCenterHost+"/execTask", bytes.NewReader(bytesData))
- if err != nil {
- return err
- }
- resp, err := client.Do(req)
- if err != nil {
- return err
- }
- if resp.StatusCode != 200 {
- return errors.New("taskCenter 请求出错")
- }
- body, _ := ioutil.ReadAll(resp.Body)
- taskRep := &model.WorkerResponse{}
- err = json.Unmarshal(body, taskRep)
- if err != nil {
- return errors.New("解析WorkerResponse错误!")
- }
- if taskRep.ErrorNo != 200 {
- return errors.New(taskRep.ErrorDesc)
- }
- _, err = RepoUpdateSetDocProps(ctx, taskCollection, task.TaskId, bson.M{"$set": bson.M{"workingId": taskRep.Result.WorkingId}})
- return err
- }
|