307 lines
10 KiB
Go
307 lines
10 KiB
Go
// ==========================================================================
|
||
// GFast自动生成logic操作代码。
|
||
// 生成日期:2023-10-07 16:08:10
|
||
// 生成路径: internal/app/system/logic/xiangmuhuafenbiao.go
|
||
// 生成人:gfast
|
||
// desc:项目划分
|
||
// company:云南奇讯科技有限公司
|
||
// ==========================================================================
|
||
|
||
package logic
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"github.com/gogf/gf/v2/util/gconv"
|
||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||
"github.com/tiger1103/gfast/v3/api/v1/common/globe"
|
||
"github.com/tiger1103/gfast/v3/api/v1/common/tool/excel"
|
||
"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"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
func init() {
|
||
service.RegisterXiangmuhuafenbiao(New())
|
||
}
|
||
|
||
func New() *sXiangmuhuafenbiao {
|
||
return &sXiangmuhuafenbiao{}
|
||
}
|
||
|
||
type sXiangmuhuafenbiao struct{}
|
||
|
||
func (s *sXiangmuhuafenbiao) EditExcelFunc(ctx context.Context, req *system.EditExcelFuncReq) (err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
//1、获取到document表对应的文件,然后返回这一行的数据
|
||
var dt *model.DocumentInfoRes
|
||
dao.Document.Ctx(ctx).Where("name", req.FileName).Limit(1).Scan(&dt)
|
||
//2、根据
|
||
var ey *model.XiangmuhuafenbiaoInfoRes
|
||
dao.Xiangmuhuafenbiao.Ctx(ctx).WherePri(req.Id).Scan(&ey)
|
||
var pathName = ""
|
||
if ey != nil {
|
||
dir := filepath.Dir(dt.FilenPath)
|
||
dir = strings.ReplaceAll(dir, "/file", coryCommon.GetCWD()+"/resource/public")
|
||
timestamp := time.Now().UnixNano() / int64(time.Millisecond)
|
||
sjc := strconv.FormatInt(timestamp, 10)
|
||
pathName = dir + "/" + strconv.FormatInt(ey.ProjectId, 10) + strconv.FormatInt(ey.Id, 10) + sjc + ".txt"
|
||
flag, err := coryCommon.OutJson([]byte(req.Json), pathName)
|
||
if err != nil || flag == false {
|
||
err = errors.New("修改失败")
|
||
return
|
||
}
|
||
_, err = dao.Xiangmuhuafenbiao.Ctx(ctx).WherePri(req.Id).Update(do.DocumentProject{
|
||
Json: strings.ReplaceAll(pathName, "\\", "/"),
|
||
})
|
||
//把原本的json删除
|
||
if err == nil && ey.Json != "" {
|
||
dirTwo := strings.ReplaceAll(ey.Json, "/file", coryCommon.GetCWD()+"/resource/public")
|
||
os.Remove(dirTwo)
|
||
}
|
||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||
}
|
||
})
|
||
return
|
||
}
|
||
|
||
func (s *sXiangmuhuafenbiao) List(ctx context.Context, req *system.XiangmuhuafenbiaoSearchReq) (listRes *system.XiangmuhuafenbiaoSearchRes, err error) {
|
||
listRes = new(system.XiangmuhuafenbiaoSearchRes)
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
m := dao.Xiangmuhuafenbiao.Ctx(ctx).WithAll()
|
||
if req.Id != "" {
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Id+" = ?", req.Id)
|
||
}
|
||
if req.Danweigongcheng != "" {
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Danweigongcheng+" = ?", req.Danweigongcheng)
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Danweigongcheng+" like ?", "%"+req.Danweigongcheng+"%")
|
||
|
||
}
|
||
if req.Zidanweigongcheng != "" {
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Zidanweigongcheng+" = ?", req.Zidanweigongcheng)
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Zidanweigongcheng+" like ?", "%"+req.Zidanweigongcheng+"%")
|
||
|
||
}
|
||
if req.Fenbugongcheng != "" {
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Fenbugongcheng+" = ?", req.Fenbugongcheng)
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Fenbugongcheng+" like ?", "%"+req.Fenbugongcheng+"%")
|
||
|
||
}
|
||
if req.Zifenbugongcheng != "" {
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Zifenbugongcheng+" = ?", req.Zifenbugongcheng)
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Zifenbugongcheng+" like ?", "%"+req.Zifenbugongcheng+"%")
|
||
|
||
}
|
||
if req.Fenxianggongcheng != "" {
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Fenxianggongcheng+" = ?", req.Fenxianggongcheng)
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Fenxianggongcheng+" like ?", "%"+req.Fenxianggongcheng+"%")
|
||
|
||
}
|
||
if req.Jianyanpi != "" {
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Jianyanpi+" = ?", req.Jianyanpi)
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Jianyanpi+" like ?", "%"+req.Jianyanpi+"%")
|
||
|
||
}
|
||
if req.Name != "" {
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Name+" like ?", "%"+req.Name+"%")
|
||
}
|
||
if req.Zhixingbiaohao != "" {
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().Zhixingbiaohao+" like ?", "%"+req.Zhixingbiaohao+"%")
|
||
|
||
//m = m.Where(dao.Xiangmuhuafenbiao.Columns().Zhixingbiaohao+" = ?", req.Zhixingbiaohao)
|
||
}
|
||
//if req.Json != "" {
|
||
// m = m.Where(dao.Xiangmuhuafenbiao.Columns().Json+" = ?", req.Json)
|
||
//}
|
||
if req.PrintStatus != "" {
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().PrintStatus+" = ?", gconv.Int(req.PrintStatus))
|
||
}
|
||
if req.ProjectId != "" {
|
||
m = m.Where(dao.Xiangmuhuafenbiao.Columns().ProjectId+" = ?", gconv.Int(req.ProjectId))
|
||
}
|
||
//if req.CreateBy != "" {
|
||
// m = m.Where(dao.Xiangmuhuafenbiao.Columns().CreateBy+" = ?", req.CreateBy)
|
||
//}
|
||
//if req.UpdateBy != "" {
|
||
// m = m.Where(dao.Xiangmuhuafenbiao.Columns().UpdateBy+" = ?", req.UpdateBy)
|
||
//}
|
||
//if req.CreateddAt != "" {
|
||
// m = m.Where(dao.Xiangmuhuafenbiao.Columns().CreateddAt+" = ?", gconv.Time(req.CreateddAt))
|
||
//}
|
||
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 asc"
|
||
if req.OrderBy != "" {
|
||
order = req.OrderBy
|
||
}
|
||
var res []*model.XiangmuhuafenbiaoInfoRes
|
||
err = m.Fields(system.XiangmuhuafenbiaoSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||
listRes.List = make([]*model.XiangmuhuafenbiaoListRes, len(res))
|
||
for k, v := range res {
|
||
listRes.List[k] = &model.XiangmuhuafenbiaoListRes{
|
||
Id: v.Id,
|
||
Danweigongcheng: v.Danweigongcheng,
|
||
Zidanweigongcheng: v.Zidanweigongcheng,
|
||
Fenbugongcheng: v.Fenbugongcheng,
|
||
Zifenbugongcheng: v.Zifenbugongcheng,
|
||
Fenxianggongcheng: v.Fenxianggongcheng,
|
||
Jianyanpi: v.Jianyanpi,
|
||
Name: v.Name,
|
||
Zhixingbiaohao: v.Zhixingbiaohao,
|
||
Json: v.Json,
|
||
PrintStatus: v.PrintStatus,
|
||
ProjectId: v.ProjectId,
|
||
CreateBy: v.CreateBy,
|
||
UpdateBy: v.UpdateBy,
|
||
CreateddAt: v.CreateddAt,
|
||
}
|
||
}
|
||
})
|
||
return
|
||
}
|
||
|
||
func (s *sXiangmuhuafenbiao) GetById(ctx context.Context, id int) (res *model.XiangmuhuafenbiaoInfoRes, err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
err = dao.Xiangmuhuafenbiao.Ctx(ctx).WithAll().Where(dao.Xiangmuhuafenbiao.Columns().Id, id).Scan(&res)
|
||
json := res.Json
|
||
if json != "" {
|
||
data, err2 := coryCommon.PutJson(json)
|
||
if err2 != nil {
|
||
err = err2
|
||
}
|
||
res.Json = data
|
||
}
|
||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||
})
|
||
return
|
||
}
|
||
|
||
func (s *sXiangmuhuafenbiao) Add(ctx context.Context, req *system.XiangmuhuafenbiaoAddReq) (err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
|
||
res, e := service.SysProject().GetByProjectId(ctx, req.ProjectId)
|
||
if e != nil {
|
||
liberr.ErrIsNil(ctx, e)
|
||
return
|
||
}
|
||
if res == nil {
|
||
liberr.ErrIsNil(ctx, errors.New("项目不存在"))
|
||
return
|
||
}
|
||
str, err1 := coryCommon.UploadFile(ctx, req.File, coryCommon.ProjectSplitTable)
|
||
if err1 != nil {
|
||
liberr.ErrIsNil(ctx, err1, "上传失败!")
|
||
}
|
||
if !strings.HasSuffix(str, ".xls") {
|
||
liberr.ErrIsNil(ctx, err, "仅支持xls格式数据")
|
||
}
|
||
err2, sheets := excel.ReadXlsx(str)
|
||
if err2 != nil {
|
||
liberr.ErrIsNil(ctx, err2, "表格读取失败")
|
||
}
|
||
|
||
dao.Xiangmuhuafenbiao.Ctx(ctx).Where(dao.Xiangmuhuafenbiao.Columns().ProjectId, req.ProjectId).Delete()
|
||
var rows []do.Xiangmuhuafenbiao
|
||
for _, sheet := range sheets {
|
||
for _, row := range sheet.Rows {
|
||
r := do.Xiangmuhuafenbiao{
|
||
ProjectId: req.ProjectId,
|
||
PrintStatus: globe.DISABLE,
|
||
}
|
||
l := len(row)
|
||
if l > 0 {
|
||
r.Danweigongcheng = row[0]
|
||
}
|
||
if l > 1 {
|
||
r.Zidanweigongcheng = row[1]
|
||
}
|
||
if l > 2 {
|
||
r.Fenbugongcheng = row[2]
|
||
}
|
||
if l > 3 {
|
||
r.Zifenbugongcheng = row[3]
|
||
}
|
||
if l > 4 {
|
||
r.Fenxianggongcheng = row[4]
|
||
}
|
||
if l > 5 {
|
||
r.Jianyanpi = row[5]
|
||
}
|
||
if l > 6 {
|
||
r.Name = row[6]
|
||
}
|
||
if l > 7 {
|
||
r.Zhixingbiaohao = row[7]
|
||
}
|
||
rows = append(rows, r)
|
||
if len(rows) == 1000 {
|
||
dao.Xiangmuhuafenbiao.Ctx(ctx).Insert(&rows)
|
||
rows = []do.Xiangmuhuafenbiao{}
|
||
}
|
||
}
|
||
}
|
||
if len(rows) > 0 {
|
||
dao.Xiangmuhuafenbiao.Ctx(ctx).Insert(&rows)
|
||
}
|
||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||
})
|
||
return
|
||
}
|
||
|
||
func (s *sXiangmuhuafenbiao) Edit(ctx context.Context, req *system.XiangmuhuafenbiaoEditReq) (err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
xiangmuhuafenbiao := do.Xiangmuhuafenbiao{
|
||
Danweigongcheng: req.Danweigongcheng,
|
||
Zidanweigongcheng: req.Zidanweigongcheng,
|
||
Fenbugongcheng: req.Fenbugongcheng,
|
||
Zifenbugongcheng: req.Zifenbugongcheng,
|
||
Fenxianggongcheng: req.Fenxianggongcheng,
|
||
Jianyanpi: req.Jianyanpi,
|
||
Name: req.Name,
|
||
Zhixingbiaohao: req.Zhixingbiaohao,
|
||
PrintStatus: req.PrintStatus,
|
||
//ProjectId: req.ProjectId,
|
||
//CreateBy: req.CreateBy,
|
||
//UpdateBy: req.UpdateBy,
|
||
//CreateddAt: req.CreateddAt,
|
||
}
|
||
//json不为空表示更改了文件,把原本的文件删除了
|
||
if req.Json != "" {
|
||
value, _ := dao.Xiangmuhuafenbiao.Ctx(ctx).WherePri(req.Id).Fields("json").Value()
|
||
dir, _ := os.Getwd()
|
||
newPath := strings.ReplaceAll(value.String(), "/file", dir+"/resource/public")
|
||
os.Remove(newPath)
|
||
}
|
||
_, err = dao.Xiangmuhuafenbiao.Ctx(ctx).OmitEmpty().WherePri(req.Id).Update(xiangmuhuafenbiao)
|
||
_, err = dao.Xiangmuhuafenbiao.Ctx(ctx).WherePri(req.Id).Update(g.Map{"json": nil})
|
||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||
})
|
||
return
|
||
}
|
||
|
||
func (s *sXiangmuhuafenbiao) Delete(ctx context.Context, ids []int) (err error) {
|
||
err = g.Try(ctx, func(ctx context.Context) {
|
||
_, err = dao.Xiangmuhuafenbiao.Ctx(ctx).Delete(dao.Xiangmuhuafenbiao.Columns().Id+" in (?)", ids)
|
||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||
})
|
||
return
|
||
}
|