初始
This commit is contained in:
327
internal/app/system/logic/manageAirline/manage_airline.go
Normal file
327
internal/app/system/logic/manageAirline/manage_airline.go
Normal file
@ -0,0 +1,327 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-12-11 16:10:19
|
||||
// 生成路径: internal/app/system/logic/manage_airline.go
|
||||
// 生成人:gfast
|
||||
// desc:航线
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
ct "github.com/tiger1103/gfast/v3/internal/app/system/logic/context"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterManageAirline(New())
|
||||
}
|
||||
|
||||
func New() *sManageAirline {
|
||||
return &sManageAirline{}
|
||||
}
|
||||
|
||||
type RouteDocumentEntity struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
Flag bool `json:"flag"`
|
||||
Signature string `json:"signature"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type sManageAirline struct{}
|
||||
|
||||
func (s *sManageAirline) RouteUploadFunc(ctx context.Context, req *system.RouteUploadReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、文件上传
|
||||
file := req.File
|
||||
path, err := coryCommon.UploadFile(ctx, file, coryCommon.Helmet+"/")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//2、计算得出文件MD5
|
||||
const old = "resource/public/"
|
||||
filePath := strings.Replace(path, old, coryCommon.GetCWD()+"/"+old, 1)
|
||||
fileContent, err := os.ReadFile(filePath)
|
||||
liberr.ErrIsNil(ctx, err, "文件读取错误")
|
||||
md5Hash := md5.New()
|
||||
_, err = io.WriteString(md5Hash, string(fileContent))
|
||||
liberr.ErrIsNil(ctx, err, "计算MD5出错")
|
||||
hashBytes := md5Hash.Sum(nil)
|
||||
md5String := fmt.Sprintf("%x", hashBytes)
|
||||
//3、新增航线数据
|
||||
_, err = dao.ManageAirline.Ctx(ctx).OmitEmpty().Insert(do.ManageAirline{
|
||||
ProjectId: req.ProjectId,
|
||||
StrId: strings.ReplaceAll(uuid.NewV4().String(), "-", ""),
|
||||
FilePath: strings.Replace(filePath, coryCommon.GetCWD(), "/"+old, 1),
|
||||
Fingerprint: md5String,
|
||||
Remark: req.Remark,
|
||||
MqClientId: req.MqClientId,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageAirline) List(ctx context.Context, req *system.ManageAirlineSearchReq) (listRes *system.ManageAirlineSearchRes, err error) {
|
||||
listRes = new(system.ManageAirlineSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.ManageAirline.Ctx(ctx).WithAll()
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.ManageAirline.Columns().ProjectId+" = ?", gconv.Int64(req.ProjectId))
|
||||
}
|
||||
if req.MqClientId != "" {
|
||||
m = m.Where(dao.ManageAirline.Columns().MqClientId+" = ?", req.MqClientId)
|
||||
}
|
||||
if req.AirLine != "" {
|
||||
m = m.Where(dao.ManageAirline.Columns().AirLine+" like ?", "%"+req.AirLine+"%")
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.ManageAirline.Columns().CreatedAt+" >=? AND "+dao.ManageAirline.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
listRes.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.ManageAirlineInfoRes
|
||||
err = m.Fields(system.ManageAirlineSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.ManageAirlineListRes, len(res))
|
||||
for k, v := range res {
|
||||
var pe []*model.PositionsEntity
|
||||
err = json.Unmarshal([]byte(v.Positions), &pe)
|
||||
listRes.List[k] = &model.ManageAirlineListRes{
|
||||
Id: v.Id,
|
||||
ProjectId: v.ProjectId,
|
||||
AirLine: v.AirLine,
|
||||
Positions: v.Positions,
|
||||
PositionsEntity: pe,
|
||||
StrId: v.StrId,
|
||||
Speed: v.Speed,
|
||||
Height: v.Height,
|
||||
Gap: v.Gap,
|
||||
FilePath: v.FilePath,
|
||||
CreatedAt: v.CreatedAt,
|
||||
Remark: v.Remark,
|
||||
MqClientId: v.MqClientId,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageAirline) GetById(ctx context.Context, id int64) (res *model.ManageAirlineInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.ManageAirline.Ctx(ctx).WithAll().Where(dao.ManageAirline.Columns().Id, id).Scan(&res)
|
||||
var pe []*model.PositionsEntity
|
||||
err = json.Unmarshal([]byte(res.Positions), &pe)
|
||||
res.PositionsEntity = pe
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func GeneratingCourse(ctx context.Context, air *model.AirlineEntity, height string) (allString string, err error) {
|
||||
uri := g.Cfg().MustGet(gctx.New(), "uav.hostOne").String() + "router/waypoint"
|
||||
for i := range air.LocationList {
|
||||
float1, _ := strconv.ParseFloat(air.LocationList[i].Height, 64)
|
||||
float2, _ := strconv.ParseFloat(height, 64)
|
||||
f := float1 - float2
|
||||
air.LocationList[i].Height = strconv.FormatFloat(f, 'f', -1, 64)
|
||||
}
|
||||
airJson, err := json.Marshal(air)
|
||||
fmt.Printf("传递的参数为-----:%v\n", string(airJson))
|
||||
response, err := g.Client().Post(ctx, uri, airJson)
|
||||
allString = response.ReadAllString()
|
||||
return
|
||||
}
|
||||
|
||||
func ExcelConvert(ctx context.Context, path, imageTargetPath string) (allString string, err error) {
|
||||
uri := g.Cfg().MustGet(gctx.New(), "uav.hostOne").String() + "excel/convert?path=" + path + "&imageTargetPath=" + imageTargetPath
|
||||
response, err := g.Client().Get(ctx, uri)
|
||||
allString = response.ReadAllString()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageAirline) Add(ctx context.Context, req *system.ManageAirlineAddReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、插入基础数据
|
||||
info, err := dao.ManageAirline.Ctx(ctx).OmitEmpty().Insert(do.ManageAirline{
|
||||
ProjectId: req.ProjectId,
|
||||
AirLine: req.AirLine,
|
||||
StrId: req.StrId,
|
||||
Speed: req.Speed,
|
||||
Height: req.Height,
|
||||
Gap: req.Gap,
|
||||
Remark: req.Remark,
|
||||
MqClientId: req.MqClientId,
|
||||
TakeOffPoint: req.Air.TakeOffRefPoint,
|
||||
TakeOffSecurityHeight: req.TakeOffSecurityHeight,
|
||||
GlobalTransitionalSpeed: req.GlobalTransitionalSpeed,
|
||||
CreateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "基础数据添加失败")
|
||||
id, err := info.LastInsertId()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//2、生成新的航线文件
|
||||
path := coryCommon.Ynr(coryCommon.Helmet) + coryCommon.FileName("air_")
|
||||
req.Air.AbsolutePath = coryCommon.GetCWD() + "/" + path
|
||||
//req.Air.AbsolutePath = "/project/zmkg/resource/public/upload_file/2023-12-15/测试航线文件"
|
||||
req.Air.TakeOffSecurityHeight = req.TakeOffSecurityHeight
|
||||
req.Air.GlobalTransitionalSpeed = req.GlobalTransitionalSpeed
|
||||
req.Air.WayLineId = int(id)
|
||||
split := strings.Split(req.LngAndLatAndHeight, ",")
|
||||
allString, err := GeneratingCourse(ctx, req.Air, split[len(split)-1])
|
||||
liberr.ErrIsNil(ctx, err, "生成航线文件失败")
|
||||
var rde *RouteDocumentEntity
|
||||
err = json.Unmarshal([]byte(allString), &rde)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "解析数据失败")
|
||||
return
|
||||
}
|
||||
if rde.Data.Flag == true {
|
||||
path = coryCommon.ResourcePublicToFunc("/"+path, 0) + ".kmz"
|
||||
//path := coryCommon.GetWd + "upload_file/2023-12-15/测试航线文件" + ".kmz"
|
||||
marshal, _ := json.Marshal(req.Positions)
|
||||
//3、将航线文件生成对应的数据存储到数据库
|
||||
_, err = dao.ManageAirline.Ctx(ctx).WherePri(id).Update(
|
||||
g.Map{
|
||||
"positions": marshal,
|
||||
"file_path": path,
|
||||
"fingerprint": rde.Data.Signature})
|
||||
liberr.ErrIsNil(ctx, err, "航线数据修改失败")
|
||||
} else {
|
||||
err = errors.New("添加失败")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageAirline) Edit(ctx context.Context, req *system.ManageAirlineEditReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
flag := false
|
||||
delStr := ""
|
||||
airlineUp := do.ManageAirline{
|
||||
AirLine: req.AirLine,
|
||||
Height: req.Height,
|
||||
Gap: req.Gap,
|
||||
Speed: req.Speed,
|
||||
Remark: req.Remark,
|
||||
UpdateBy: ct.New().GetLoginUser(ctx).Id,
|
||||
}
|
||||
//这三个值有一个改变(传递给我),那证明需要修改航线
|
||||
if req.Speed > 0 || req.Height > 0 || req.Gap > 0 {
|
||||
airlineUp.TakeOffPoint = req.Air.TakeOffRefPoint
|
||||
airlineUp.TakeOffSecurityHeight = req.TakeOffSecurityHeight
|
||||
airlineUp.GlobalTransitionalSpeed = req.GlobalTransitionalSpeed
|
||||
req.Air.TakeOffSecurityHeight = req.TakeOffSecurityHeight
|
||||
req.Air.GlobalTransitionalSpeed = req.GlobalTransitionalSpeed
|
||||
//1、获取原本的路径
|
||||
value, err := dao.ManageAirline.Ctx(ctx).WherePri(req.Id).Fields("file_path").Value()
|
||||
delStr = value.String()
|
||||
//2、生成新的航线文件
|
||||
split := strings.Split(req.LngAndLatAndHeight, ",")
|
||||
path := coryCommon.Ynr(coryCommon.Helmet) + coryCommon.FileName("air_")
|
||||
req.Air.AbsolutePath = coryCommon.GetCWD() + "/" + path
|
||||
//req.Air.AbsolutePath = "/project/zmkg/resource/public/upload_file/2023-12-15/测试航线文件"
|
||||
allString, err := GeneratingCourse(ctx, req.Air, split[len(split)-1])
|
||||
liberr.ErrIsNil(ctx, err, "生成航线文件失败")
|
||||
var rde *RouteDocumentEntity
|
||||
err = json.Unmarshal([]byte(allString), &rde)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
return
|
||||
}
|
||||
airlineUp.FilePath = strings.Replace(path, "resource/public/", coryCommon.GetWd, 1) + ".kmz"
|
||||
//airlineUp.FilePath = coryCommon.GetWd + "upload_file/2023-12-15/测试航线文件" + ".kmz"
|
||||
airlineUp.Fingerprint = rde.Data.Signature
|
||||
marshal, _ := json.Marshal(req.Positions)
|
||||
airlineUp.Positions = marshal
|
||||
flag = rde.Data.Flag
|
||||
} else {
|
||||
flag = true
|
||||
}
|
||||
if flag == true {
|
||||
//3、将航线文件生成对应的数据存储到数据库
|
||||
_, err = dao.ManageAirline.Ctx(ctx).OmitEmpty().WherePri(req.Id).Update(airlineUp)
|
||||
//删除原本的航线文件
|
||||
if err != nil {
|
||||
coryCommon.BatchFile(strings.Split(delStr, ",")) //删除pdf
|
||||
}
|
||||
} else {
|
||||
err = errors.New("修改失败")
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageAirline) Delete(ctx context.Context, ids []int64) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
for _, id := range ids {
|
||||
//1、查询路径
|
||||
var airEntity *model.ManageAirlineInfoRes
|
||||
if err = dao.ManageAirline.Ctx(ctx).Where(dao.ManageAirline.Columns().Id, id).Scan(&airEntity); err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
//2、删除路径
|
||||
if err == nil {
|
||||
coryCommon.BatchFile(strings.Split(airEntity.FilePath, ","))
|
||||
}
|
||||
//3、删除模板
|
||||
if _, err = dao.ManageTask.Ctx(ctx).Unscoped().Delete(dao.ManageTask.Columns().AirLine, id); err != nil {
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
//4、删除航线数据
|
||||
_, err = dao.ManageAirline.Ctx(ctx).Unscoped().Delete(dao.ManageAirline.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user