初始
This commit is contained in:
@ -0,0 +1,438 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-09-05 12:07:41
|
||||
// 生成路径: internal/app/system/logic/qianqi_guangfuban_ids_zhuangdian.go
|
||||
// 生成人:gfast
|
||||
// desc:光伏板桩点
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/google/uuid"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/shp"
|
||||
"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"
|
||||
"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/model/entity"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterQianqiGuangfubanIdsZhuangdian(New())
|
||||
}
|
||||
|
||||
func New() *sQianqiGuangfubanIdsZhuangdian {
|
||||
return &sQianqiGuangfubanIdsZhuangdian{}
|
||||
}
|
||||
|
||||
type sQianqiGuangfubanIdsZhuangdian struct{}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) GisListFunc(ctx context.Context, req *system.GisListReq, num string) (listRes *system.GisListRes, err error) {
|
||||
listRes = new(system.GisListRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.QianqiGuangfubanIdsZhuangdian.Ctx(ctx).WithAll()
|
||||
if num == "1" {
|
||||
m = m.Fields("id,name,source_type,source_id,project_id,detail")
|
||||
}
|
||||
if num == "2" {
|
||||
m = m.Fields("id,name,source_type,source_id,project_id,detail")
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().CreatedAt+" >=? AND "+dao.QianqiGuangfubanIdsZhuangdian.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
|
||||
if req.FangZhenID != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().FangzhenId+" = ?", req.FangZhenID)
|
||||
}
|
||||
|
||||
order := "name asc,id asc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
array, err := m.Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
listRes.Total = len(array)
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
var res []*model.QianqiGuangfubanIdsZhuangdianInfoRes
|
||||
if num == "1" {
|
||||
if strings.EqualFold(req.IsPaging, "YES") {
|
||||
m = m.Fields(system.QianqiGuangfubanIdsZhuangdianSearchRes{}).Page(req.PageNum, req.PageSize)
|
||||
}
|
||||
} else {
|
||||
if !req.NotInPlan {
|
||||
m = m.Fields(system.QianqiGuangfubanIdsZhuangdianSearchRes{}).Page(req.PageNum, req.PageSize)
|
||||
} else {
|
||||
m = m.Fields(system.QianqiGuangfubanIdsZhuangdianSearchRes{}).Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().SourceId+" not in(select source_id from "+dao.PlanWeek.Table()+" where project_id=?)", req.ProjectId)
|
||||
}
|
||||
}
|
||||
err = m.Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.QianqiGuangfubanIdsZhuangdianListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.QianqiGuangfubanIdsZhuangdianListRes{
|
||||
Id: v.Id,
|
||||
Name: v.Name,
|
||||
SourceType: v.SourceType,
|
||||
SourceId: v.SourceId,
|
||||
Detail: v.Detail,
|
||||
}
|
||||
}
|
||||
sort.Slice(listRes.List, func(i, j int) bool {
|
||||
return listRes.List[i].Name < listRes.List[j].Name
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) EditDetail(ctx context.Context, req *system.QianqiGuangfubanIdsZhuangdianEditDetailReq) (err error) {
|
||||
g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 1、证明有数据,需sql语句
|
||||
if len(req.List) > 0 {
|
||||
for i := 0; i < 3; i++ {
|
||||
var wh []string
|
||||
str := ""
|
||||
if i == 0 {
|
||||
str = "update " + dao.QianqiGuangfubanIdsZhuangdian.Table() + " set detail= case "
|
||||
}
|
||||
if i == 1 {
|
||||
str = "update " + dao.QianqiGuangfubanIdsLizhu.Table() + " set detail= case "
|
||||
}
|
||||
|
||||
if i == 2 {
|
||||
str = "update " + dao.QianqiGuangfubanIdsZhijia.Table() + " set detail= case "
|
||||
}
|
||||
|
||||
for _, detail := range req.List {
|
||||
// if i < 10 {
|
||||
marshal, err := json.Marshal(detail)
|
||||
if err != nil {
|
||||
fmt.Println("是否错误:", err)
|
||||
continue
|
||||
}
|
||||
st := "when source_id='" + detail.Position.SourceId + "' then '" + string(marshal) + "'"
|
||||
wh = append(wh, st)
|
||||
if len(wh) == 1000 {
|
||||
exec := str + strings.Join(wh, " ") + " ELSE detail end where project_id= " + req.ProjectId + ";"
|
||||
_, err = g.DB().Exec(ctx, exec)
|
||||
if err != nil {
|
||||
err = errors.New("更新失败!")
|
||||
return
|
||||
}
|
||||
wh = []string{}
|
||||
}
|
||||
//}
|
||||
}
|
||||
if len(wh) > 0 {
|
||||
exec := str + strings.Join(wh, " ") + " ELSE detail end where project_id= " + req.ProjectId + ";"
|
||||
_, err = g.DB().Exec(ctx, exec)
|
||||
if err != nil {
|
||||
err = errors.New("更新失败!")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) List(ctx context.Context, req *system.QianqiGuangfubanIdsZhuangdianSearchReq) (listRes *system.QianqiGuangfubanIdsZhuangdianSearchRes, err error) {
|
||||
listRes = new(system.QianqiGuangfubanIdsZhuangdianSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.QianqiGuangfubanIdsZhuangdian.Ctx(ctx).WithAll()
|
||||
if req.Id != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().Id+" = ?", req.Id)
|
||||
}
|
||||
if req.Name != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().Name+" like ?", "%"+req.Name+"%")
|
||||
}
|
||||
if req.CreateBy != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().CreateBy+" = ?", req.CreateBy)
|
||||
}
|
||||
if req.UpdateBy != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().UpdateBy+" = ?", req.UpdateBy)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().CreatedAt+" >=? AND "+dao.QianqiGuangfubanIdsZhuangdian.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if req.SourceType != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().SourceType+" = ?", req.SourceType)
|
||||
}
|
||||
if req.SourceId != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().SourceId+" = ?", req.SourceId)
|
||||
}
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().Status+" = ?", gconv.Int(req.Status))
|
||||
}
|
||||
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 := "name asc,id asc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.QianqiGuangfubanIdsZhuangdianInfoRes
|
||||
|
||||
if !req.NotInPlan {
|
||||
m = m.Fields(system.QianqiGuangfubanIdsZhuangdianSearchRes{}).Page(req.PageNum, req.PageSize)
|
||||
} else {
|
||||
m = m.Fields(system.QianqiGuangfubanIdsZhuangdianSearchRes{}).Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().SourceId+" not in(select source_id from "+dao.PlanWeek.Table()+" where project_id=?)", req.ProjectId)
|
||||
}
|
||||
err = m.Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.QianqiGuangfubanIdsZhuangdianListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.QianqiGuangfubanIdsZhuangdianListRes{
|
||||
Id: v.Id,
|
||||
Name: v.Name,
|
||||
CreateBy: v.CreateBy,
|
||||
UpdateBy: v.UpdateBy,
|
||||
CreatedAt: v.CreatedAt,
|
||||
SourceType: v.SourceType,
|
||||
SourceId: v.SourceId,
|
||||
ProjectId: v.ProjectId,
|
||||
Status: v.Status,
|
||||
Detail: v.Detail,
|
||||
}
|
||||
}
|
||||
sort.Slice(listRes.List, func(i, j int) bool {
|
||||
return listRes.List[i].Name < listRes.List[j].Name
|
||||
})
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) GetById(ctx context.Context, id int) (res *model.QianqiGuangfubanIdsZhuangdianInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.QianqiGuangfubanIdsZhuangdian.Ctx(ctx).WithAll().Where(dao.QianqiGuangfubanIdsZhuangdian.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
type Zhuangdian struct {
|
||||
Position shp.Point `json:"position"`
|
||||
}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) Add(ctx context.Context, req *system.QianqiGuangfubanIdsZhuangdianAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 读取shp文件
|
||||
shapes, err := ProcessFiles(ctx, req.File)
|
||||
liberr.ErrIsNil(ctx, err, "读取shp文件失败")
|
||||
|
||||
// 分组后的结构如下:
|
||||
// [T01] |--> G01.01.01 --> [G01.01.01.1, G01.01.01.2]
|
||||
// |--> G01.01.02 --> [G01.01.02.1, G01.01.02.2]
|
||||
//
|
||||
// [T02] |--> G02.01.01 --> [G02.01.01.1, G02.01.01.2]
|
||||
// |--> G02.01.02 --> [G02.01.02.1, G02.01.02.2]
|
||||
//
|
||||
// 其中,[T01] 和 [T02] 是主键 | G01.01.01、G01.01.02 等是子键,
|
||||
// [G01.01.01.1, G01.01.01.2] 等是子键下的元素数组。
|
||||
|
||||
group := make(map[string]map[string][]do.QianqiGuangfubanIdsZhuangdian)
|
||||
|
||||
// 开始处理分组
|
||||
for i := 0; i < len(shapes.Points); i++ {
|
||||
point := shapes.Points[i] // 当前点
|
||||
|
||||
originalName := strings.Split(point.Name, ".") // 拆分为数组
|
||||
|
||||
// 取出方阵名,并将 G 替换为 T
|
||||
// 用于后续匹配更新对应方阵的数据
|
||||
fangZhenName := strings.ReplaceAll(originalName[0], "G", "T") // 方阵名 T01
|
||||
|
||||
// 根据方阵名取出对应的数据
|
||||
fangZhen, ok := group[fangZhenName]
|
||||
if !ok {
|
||||
// 如果不存在则创建
|
||||
fangZhen = make(map[string][]do.QianqiGuangfubanIdsZhuangdian)
|
||||
group[fangZhenName] = fangZhen
|
||||
}
|
||||
|
||||
// 去除 #
|
||||
// 替换 - 为 .
|
||||
// 用于将其插入到二级分组中
|
||||
insertName := strings.ReplaceAll(strings.ReplaceAll(point.Name, "#", ""), "-", ".") // G01.01.01
|
||||
|
||||
// 获取当前已存在的元素数量
|
||||
existingCount := len(fangZhen[insertName])
|
||||
|
||||
// 每新增一个重复的元素,将其名称的数字自增加一
|
||||
// 例如: G01.01.01.1, G01.01.01.2
|
||||
newName := fmt.Sprintf("%s.%d", point.Name, existingCount+1)
|
||||
|
||||
// 调用 buildDetailAndSourceID 生成 detail 和 SourceID
|
||||
detail, sourceID := buildDetailAndSourceID(point, newName)
|
||||
|
||||
newElement := do.QianqiGuangfubanIdsZhuangdian{
|
||||
Name: newName, // G01.01.01.1
|
||||
ProjectId: req.ProjectId, // 主项目ID
|
||||
Detail: detail,
|
||||
SourceId: sourceID,
|
||||
}
|
||||
|
||||
// 将新元素追加到到 fangZhen[insertName] 后的切片中
|
||||
fangZhen[insertName] = append(fangZhen[insertName], newElement)
|
||||
}
|
||||
|
||||
// 获取当前项目下的所有方阵
|
||||
fangzhenList := []entity.QianqiFangzhen{}
|
||||
err = dao.QianqiFangzhen.Ctx(ctx).Where(dao.QianqiFangzhen.Columns().ProjectId, req.SubProjectid).Scan(&fangzhenList)
|
||||
liberr.ErrIsNil(ctx, err, "获取当前项目下的方阵失败")
|
||||
|
||||
// 批量插入列表
|
||||
insertList := []do.QianqiGuangfubanIdsZhuangdian{}
|
||||
|
||||
// 遍历所有方阵
|
||||
for i := 0; i < len(fangzhenList); i++ {
|
||||
f := fangzhenList[i] // 当前方阵
|
||||
// 根据方阵名取出对应的数据
|
||||
if v, ok := group[f.Name]; ok {
|
||||
// 处理每个方阵应该被写入的数据
|
||||
extractedValues := extractValuesFromMap(f.Id, f.ProjectId, v)
|
||||
insertList = append(insertList, extractedValues...)
|
||||
}
|
||||
}
|
||||
|
||||
if len(insertList) > 0 {
|
||||
batchNumber := 700
|
||||
_, err = dao.QianqiGuangfubanIdsZhuangdian.Ctx(ctx).Batch(batchNumber).Insert(&insertList)
|
||||
liberr.ErrIsNil(ctx, err, "光伏板插入失败")
|
||||
_, err = dao.QianqiGuangfubanIdsLizhu.Ctx(ctx).Batch(batchNumber).Insert(&insertList)
|
||||
liberr.ErrIsNil(ctx, err, "立柱插入失败")
|
||||
_, err = dao.QianqiGuangfubanIdsZhijia.Ctx(ctx).Batch(batchNumber).Insert(&insertList)
|
||||
liberr.ErrIsNil(ctx, err, "支架失败")
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ProcessFiles(ctx context.Context, files []*ghttp.UploadFile) (*shp.ShpObj, error) {
|
||||
var filePaths string
|
||||
|
||||
// 遍历请求中的所有文件
|
||||
for _, file := range files {
|
||||
// 保存文件后返回其路径
|
||||
uploadedFilePath, err := coryCommon.UploadFile(ctx, file, coryCommon.LargeFileShp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 将文件路径按"."分割成切片
|
||||
filePathParts := strings.Split(uploadedFilePath, ".")
|
||||
// 如果切片长度大于1,将最后一个元素(文件扩展名)改为"shp"
|
||||
if len(filePathParts) > 1 {
|
||||
filePathParts[len(filePathParts)-1] = "shp"
|
||||
}
|
||||
// 将修改后的切片重新组合成文件路径,并将其添加到文件路径中
|
||||
updatedFilePath := strings.Join(filePathParts, ".")
|
||||
filePaths = updatedFilePath
|
||||
}
|
||||
|
||||
err, shp := shp.ReadShp(filePaths)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return shp, nil
|
||||
}
|
||||
|
||||
// extractValuesFromMap 传入一个数组,遍历后为其添加方阵ID和子项目ID
|
||||
func extractValuesFromMap(fangzhenID int, subprojectid string, inputMap map[string][]do.QianqiGuangfubanIdsZhuangdian) []do.QianqiGuangfubanIdsZhuangdian {
|
||||
var result []do.QianqiGuangfubanIdsZhuangdian
|
||||
for _, value := range inputMap {
|
||||
for _, v := range value {
|
||||
result = append(result, do.QianqiGuangfubanIdsZhuangdian{
|
||||
Name: v.Name,
|
||||
ProjectId: v.ProjectId,
|
||||
SubProjectid: subprojectid, // 子项目ID
|
||||
FangzhenId: fangzhenID, // 方阵ID
|
||||
Detail: v.Detail,
|
||||
SourceId: v.SourceId,
|
||||
})
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// 传入 point 构建出 detail 和 SourceID
|
||||
func buildDetailAndSourceID(point shp.Point, name string) (string, string) {
|
||||
// 生成 SourceID
|
||||
sourceID := fmt.Sprintf("%s.%s", point.Name, strings.ReplaceAll(uuid.New().String(), "-", ""))
|
||||
|
||||
detail := Zhuangdian{
|
||||
Position: point,
|
||||
}
|
||||
detail.Position.SourceId = sourceID
|
||||
|
||||
// 序列化后传回
|
||||
data, err := json.Marshal(detail)
|
||||
if err != nil {
|
||||
return "", ""
|
||||
}
|
||||
return string(data), sourceID
|
||||
}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) Edit(ctx context.Context, req *system.QianqiGuangfubanIdsZhuangdianEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.QianqiGuangfubanIdsZhuangdian.Ctx(ctx).WherePri(req.Id).Update(do.QianqiGuangfubanIdsZhuangdian{
|
||||
Name: req.Name,
|
||||
CreateBy: req.CreateBy,
|
||||
UpdateBy: req.UpdateBy,
|
||||
SourceType: req.SourceType,
|
||||
SourceId: req.SourceId,
|
||||
ProjectId: req.ProjectId,
|
||||
Status: req.Status,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sQianqiGuangfubanIdsZhuangdian) Delete(ctx context.Context, ids []int) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.QianqiGuangfubanIdsZhuangdian.Ctx(ctx).Delete(dao.QianqiGuangfubanIdsZhuangdian.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user