This commit is contained in:
2025-07-07 20:11:59 +08:00
parent ab0fdbc447
commit 06e3aa2eb3
2009 changed files with 193082 additions and 0 deletions

View File

@ -0,0 +1,450 @@
// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期2023-09-05 12:10:51
// 生成路径: internal/app/system/logic/qianqi_guangfuban_ids_zhijia.go
// 生成人gfast
// desc:光伏板支架
// company:云南奇讯科技有限公司
// ==========================================================================
package logic
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
"strings"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/util/gconv"
"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/service"
"github.com/tiger1103/gfast/v3/library/liberr"
)
func init() {
service.RegisterQianqiGuangfubanIdsZhijia(New())
}
func New() *sQianqiGuangfubanIdsZhijia {
return &sQianqiGuangfubanIdsZhijia{}
}
type sQianqiGuangfubanIdsZhijia struct{}
func (s *sQianqiGuangfubanIdsZhijia) GisListFunc(ctx context.Context, req *system.GisZhijiaReq, num string) (listRes *system.GisZhijiaRes, err error) {
listRes = new(system.GisZhijiaRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.QianqiGuangfubanIdsZhijia.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")
}
if len(req.DateRange) != 0 {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().CreatedAt+" >=? AND "+dao.QianqiGuangfubanIdsZhijia.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
}
if req.ProjectId != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().ProjectId+" = ?", req.ProjectId)
}
if req.FangZhenID != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().FangzhenId+" = ?", req.FangZhenID)
}
order := "id asc"
if req.OrderBy != "" {
order = req.OrderBy
}
var res []*model.QianqiGuangfubanIdsZhijiaInfoRes
m = m.Fields(system.QianqiGuangfubanIdsZhijiaSearchRes{})
array, err := m.Array()
liberr.ErrIsNil(ctx, err, "获取总行数失败")
listRes.Total = len(array)
listRes.CurrentPage = req.PageNum
if req.PageSize == 0 {
req.PageSize = consts.PageSize
}
if req.PageNum == 0 {
req.PageNum = 1
}
if num == "1" {
if strings.EqualFold(req.IsPaging, "YES") {
m = m.Page(req.PageNum, req.PageSize)
}
} else {
if !req.NotInPlan {
m = m.Page(req.PageNum, req.PageSize)
} else {
m = m.Fields(system.QianqiGuangfubanIdsZhijiaSearchRes{}).Where(dao.QianqiGuangfubanIdsZhijia.Columns().SourceId+" not in(select source_id from "+dao.PlanWeek.Table()+" where project_id=? and source_type='lizhu')", req.ProjectId)
}
}
err = m.Order(order).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取数据失败")
listRes.List = make([]*model.QianqiGuangfubanIdsZhijiaListRes, len(res))
for k, v := range res {
listRes.List[k] = &model.QianqiGuangfubanIdsZhijiaListRes{
Id: v.Id,
Name: v.Name,
SourceType: v.SourceType,
SourceId: v.SourceId,
ProjectId: v.ProjectId,
Detail: v.Detail,
}
}
sort.Slice(listRes.List, func(i, j int) bool {
return listRes.List[i].Name < listRes.List[j].Name
})
})
return
}
func (s *sQianqiGuangfubanIdsZhijia) QueryDetailsFunc(ctx context.Context, req *system.QueryDetailsReq) (res *system.QueryDetailsRes, err error) {
name := req.Name
req.Name = strings.ReplaceAll(req.Name, "G0", "NC.")
res = new(system.QueryDetailsRes)
array, err := g.DB().Model("qianqi_warning").
Fields("detail").
Where("project_id", req.ProjectId).
Where("name_one='" + name + "' or name_one='" + req.Name + "'").Array()
if err != nil {
liberr.ErrIsNil(ctx, err, "获取数据失败!")
}
var ptList []*system.Zhuangdian
for i := range array {
var pt *system.Zhuangdian
err := json.Unmarshal([]byte(array[i].String()), &pt)
if err != nil {
liberr.ErrIsNil(ctx, err, "解析数据失败!")
} else {
ptList = append(ptList, pt)
}
}
res.List = ptList
return
}
func (s *sQianqiGuangfubanIdsZhijia) PrewarningValueFunc(ctx context.Context, req *system.PrewarningValueReq) (listRes *system.PrewarningValueRes, err error) {
//listRes = new(system.PrewarningValueRes)
//m := g.DB().Model("qianqi_warning")
//e := g.DB().Model("qianqi_warning").Fields("DISTINCT name_one as nameStr")
////2、获取到下面的数据
//if req.ProjectId != "" {
// m = m.Where("project_id", req.ProjectId)
// e = e.Where("project_id", req.ProjectId)
//}
//if req.SourceType != "" {
// m = m.Where("source_type", req.SourceType)
// e = e.Where("source_type", req.SourceType)
//}
////1、获取到主目录
//var resp []*system.PrewarningValueEntity
//err = e.WithAll().Scan(&resp)
////2、获取到子目录
//for i := range resp {
// var res []*system.WarningEntity
// err = m.Safe().Where("name_one like ?", resp[i].NameStr+"%").OrderDesc("created_at").Scan(&res)
// resp[i].WarningList = res
//}
//listRes.List = resp
//liberr.ErrIsNil(ctx, err, "获取数据失败!")
//return
//listRes = new(system.PrewarningValueRes)
//m := g.DB().Model("qianqi_warning").As("a").Safe()
//e := m.Fields("DISTINCT name_two")
////2、获取到下面的数据
//if req.ProjectId != "" {
// m = m.Where("a.project_id", req.ProjectId)
// e = e.Where("a.project_id", req.ProjectId)
//}
//if req.SourceType != "" {
// m = m.Where("a.source_type", req.SourceType)
// e = e.Where("a.project_id", req.ProjectId)
//}
//var resp []*system.PrewarningValueEntity
//array, err := e.Limit(10).Array()
//for _, arr := range array {
// var respTwo *system.PrewarningValueEntity
// m.Fields("DISTINCT name_one AS nameStr").WithAll().Where("a.name_two", arr.String()).Scan(&respTwo)
// resp = append(resp, respTwo)
//}
//listRes.List = resp
//return
listRes = new(system.PrewarningValueRes)
m := g.DB().Model("qianqi_warning").As("a")
// 2、获取到下面的数据
if req.ProjectId != "" {
m = m.Where("a.project_id", req.ProjectId)
}
if req.SourceType != "" {
m = m.Where("a.source_type", req.SourceType)
}
var resp []*system.PrewarningValueEntity
err = m.Fields("DISTINCT name_one AS nameStr").WithAll().Scan(&resp)
listRes.List = resp
return
}
func (s *sQianqiGuangfubanIdsZhijia) EditDetail(ctx context.Context, req *system.QianqiGuangfubanIdsZhijiaDetailReq) (err error) {
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
// 1、存儲修改高程的数据
for i := 0; i < 2; i++ {
var wh []string
str := ""
if i == 0 {
str = "update " + dao.QianqiGuangfubanIdsLizhu.Table() + " set detail= case "
} else {
str = "update " + dao.QianqiGuangfubanIdsZhijia.Table() + " set detail= case "
}
for _, detail := range req.List {
if i == 0 { // 避免运行两次
result := new(big.Float).SetPrec(uint(13)).Sub(new(big.Float).SetFloat64(detail.Position.Height), new(big.Float).SetFloat64(detail.Position.Alt))
if result.Cmp(new(big.Float).SetFloat64(0)) == -1 {
detail.Position.Difference = 0
} else {
detail.Position.Difference, _ = result.Float64()
}
}
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 err
}
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 err
}
}
}
return err
})
go calculate(ctx, req.ProjectId)
return
}
func calculate(ctx context.Context, projectId string) {
g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
err := g.Try(ctx, func(ctx context.Context) {
// 防止重复预警数据,先把对应项目的预警数据删除
_, err := g.DB().Model("qianqi_warning").Where("project_id", projectId).Delete()
if err != nil {
err = errors.New("更新失败!")
// return err
}
values, err := dao.QianqiGuangfubanIdsLizhu.Ctx(ctx).Fields("detail").Where("project_id", projectId).Array()
// 2、获取所有当前项目的所有名字计算祖串的差值
array, _ := dao.QianqiGuangfubanIdsLizhu.Ctx(ctx).Fields("DISTINCT SUBSTR( NAME, 1, LENGTH( NAME )- 2 ) AS bname ").Where("project_id", projectId).Group("name").Array()
if len(array) > 0 {
for _, data := range array {
var yjArr []float64
i := 0
var entity []g.Map
for _, detail := range values {
var zd *system.Zhuangdian
_ = json.Unmarshal([]byte(detail.String()), &zd)
name := zd.Position.Name
name = name[:len(name)-2]
if data.String() == name && zd.Position.Height > 0 {
yjArr = append(yjArr, zd.Position.Difference)
entityTwo := g.Map{
"name_one": data.String(),
"name_two": zd.Position.Name,
"source_type": "lizhu",
"source_id": zd.Position.SourceId,
"project_id": projectId,
"detail": detail,
}
entity = append(entity, entityTwo)
if i == 3 { // 组串4条数据下标为3
ax := maxFunc(yjArr)
in := minFunc(yjArr)
if (ax - in) > 0.7 {
_, err := g.DB().Model("qianqi_warning").Insert(entity)
if err != nil {
err = errors.New("预警值存储失败!")
return
}
}
}
i = i + 1
}
}
}
}
})
return err
})
}
func maxFunc(nums []float64) float64 {
max := nums[0]
for _, num := range nums {
if num > max {
max = num
}
}
return max
}
func minFunc(nums []float64) float64 {
min := nums[0]
for _, num := range nums {
if num < min {
min = num
}
}
return min
}
func (s *sQianqiGuangfubanIdsZhijia) List(ctx context.Context, req *system.QianqiGuangfubanIdsZhijiaSearchReq) (listRes *system.QianqiGuangfubanIdsZhijiaSearchRes, err error) {
listRes = new(system.QianqiGuangfubanIdsZhijiaSearchRes)
err = g.Try(ctx, func(ctx context.Context) {
m := dao.QianqiGuangfubanIdsZhijia.Ctx(ctx).WithAll()
if req.Id != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().Id+" = ?", req.Id)
}
if req.Name != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().Name+" like ?", "%"+req.Name+"%")
}
if req.CreateBy != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().CreateBy+" = ?", req.CreateBy)
}
if req.UpdateBy != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().UpdateBy+" = ?", req.UpdateBy)
}
if len(req.DateRange) != 0 {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().CreatedAt+" >=? AND "+dao.QianqiGuangfubanIdsZhijia.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
}
if req.SourceType != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().SourceType+" = ?", req.SourceType)
}
if req.SourceId != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().SourceId+" = ?", req.SourceId)
}
if req.ProjectId != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().ProjectId+" = ?", req.ProjectId)
}
if req.Status != "" {
m = m.Where(dao.QianqiGuangfubanIdsZhijia.Columns().Status+" = ?", gconv.Int(req.Status))
}
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.QianqiGuangfubanIdsZhijiaInfoRes
if !req.NotInPlan {
m = m.Fields(system.QianqiGuangfubanIdsZhijiaSearchRes{})
listRes.Total, err = m.Count()
liberr.ErrIsNil(ctx, err, "获取总行数失败")
if req.PageNum == 0 {
req.PageNum = 1
}
m = m.Page(req.PageNum, req.PageSize)
} else {
m = m.Fields(system.QianqiGuangfubanIdsZhijiaSearchRes{}).Where(dao.QianqiGuangfubanIdsZhijia.Columns().SourceId+" not in(select source_id from "+dao.PlanWeek.Table()+" where project_id=? and source_type='lizhu')", req.ProjectId)
}
err = m.Order(order).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取数据失败")
listRes.List = make([]*model.QianqiGuangfubanIdsZhijiaListRes, len(res))
for k, v := range res {
listRes.List[k] = &model.QianqiGuangfubanIdsZhijiaListRes{
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 *sQianqiGuangfubanIdsZhijia) GetById(ctx context.Context, id int) (res *model.QianqiGuangfubanIdsZhijiaInfoRes, err error) {
err = g.Try(ctx, func(ctx context.Context) {
err = dao.QianqiGuangfubanIdsZhijia.Ctx(ctx).WithAll().Where(dao.QianqiGuangfubanIdsZhijia.Columns().Id, id).Scan(&res)
liberr.ErrIsNil(ctx, err, "获取信息失败")
})
return
}
func (s *sQianqiGuangfubanIdsZhijia) Add(ctx context.Context, req *system.QianqiGuangfubanIdsZhijiaAddReq) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.QianqiGuangfubanIdsZhijia.Ctx(ctx).Insert(do.QianqiGuangfubanIdsZhijia{
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 *sQianqiGuangfubanIdsZhijia) Edit(ctx context.Context, req *system.QianqiGuangfubanIdsZhijiaEditReq) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.QianqiGuangfubanIdsZhijia.Ctx(ctx).WherePri(req.Id).Update(do.QianqiGuangfubanIdsZhijia{
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 *sQianqiGuangfubanIdsZhijia) Delete(ctx context.Context, ids []int) (err error) {
err = g.Try(ctx, func(ctx context.Context) {
_, err = dao.QianqiGuangfubanIdsZhijia.Ctx(ctx).Delete(dao.QianqiGuangfubanIdsZhijia.Columns().Id+" in (?)", ids)
liberr.ErrIsNil(ctx, err, "删除失败")
})
return
}