lanjianrong 4 rokov pred
rodič
commit
7cc4aa637e

+ 1 - 1
conf/db.go

@@ -46,4 +46,4 @@ var DbMasterList = []DbConfig{
 	},
 }
 
-var DbMaster = DbMasterList[2]
+var DbMaster = DbMasterList[1]

+ 3 - 0
conf/project.go

@@ -46,3 +46,6 @@ var MergeLocalRootDir = "./public/"
 
 // 分页-页数
 var PageSize = 10
+
+// 正式服务器文件存储地址
+var NormalFileStorage = "/mnt/dq"

+ 37 - 6
dao/tree_contract_dao.go

@@ -31,10 +31,29 @@ func NewTreeContractDao(engine *xorm.Engine) *TreeContractDao {
 	}
 }
 
-func (d *TreeContractDao) GetDetail(id int) *viewmodels.TreeSectionDetail {
+// 获取项目节名称
+func (d *TreeContractDao) GetDetailName(id int) string {
+	data := &models.CmTreeContracts{}
+	_, _ = d.engine.Where("id = ?", id).Get(data)
+	return data.Name
+}
+
+// 获取项目节pdf文件信息
+func (d *TreeContractDao) GetDetailWithPdf(id int) *viewmodels.TreeSectionDetail {
 	data := &viewmodels.TreeSectionDetail{}
-	_, _ = d.engine.Sql("select t.name, s.content, f.ext, f.path as filepath, f.id as fid, f.name as filename from cm_tree_contracts as t left join cm_contracts as s on t.contract_id = s.id  left join cm_upload as f on t.contract_id = f.id where t.id = ?", id).Get(data)
-	fmt.Println(data)
+	_, _ = d.engine.Sql("select s.content, f.ext, f.path as filepath, f.id as fid, f.name as filename from cm_tree_contracts as t left join cm_contracts as s on t.contract_id = s.id  left join cm_upload as f on t.contract_id = f.id where t.id = ?", id).Get(data)
+	if data.Fid != "" {
+		fid, _ := comm.AesEncrypt(data.Fid, conf.SignSecret)
+		data.Fid = fid
+
+	}
+	return data
+}
+
+// 获取项目节excel文件信息
+func (d *TreeContractDao) GetDetailWithExcel(id int) *viewmodels.TreeSectionDetail {
+	data := &viewmodels.TreeSectionDetail{}
+	_, _ = d.engine.Sql("select s.content, f.ext, f.path as filepath, f.id as fid, f.name as filename from cm_tree_contracts as t left join cm_contracts as s on t.contract_id2 = s.id  left join cm_upload as f on t.contract_id2 = f.id where t.id = ?", id).Get(data)
 	if data.Fid != "" {
 		fid, _ := comm.AesEncrypt(data.Fid, conf.SignSecret)
 		data.Fid = fid
@@ -585,7 +604,19 @@ func (d *TreeContractDao) replaceContractAttribution(session *xorm.Session, attr
 
 // 更新合同id
 func (d *TreeContractDao) UpdateContract(id int, contractId int) error {
-	data := &models.CmTreeContracts{Id: id, ContractId: contractId}
-	_, err := d.engine.Where("id = ?", id).Cols("contract_id").Update(data)
-	return err
+	file := &models.CmUpload{}
+	_, err := d.engine.Where("id = ?", contractId).Get(file)
+	if err != nil {
+		return err
+	}
+	if file.Ext == "application/vnd.ms-excel" {
+		data := &models.CmTreeContracts{Id: id, ContractId2: contractId}
+		_, err := d.engine.Where("id = ?", id).Cols("contract_id2").Update(data)
+		return err
+	} else if file.Ext == "application/pdf" {
+		data := &models.CmTreeContracts{Id: id, ContractId: contractId}
+		_, err := d.engine.Where("id = ?", id).Cols("contract_id").Update(data)
+		return err
+	}
+	return nil
 }

+ 7 - 0
dao/upload_dao.go

@@ -28,3 +28,10 @@ func (d *UploadDao) Add(upload *models.CmUpload) (int64, error) {
 	id, err := d.engine.Insert(upload)
 	return id, err
 }
+
+// 获取文件
+func (d *UploadDao) GetFile(id int) (*models.CmUpload, error) {
+	file := &models.CmUpload{}
+	_, err := d.engine.Where("id = ?", id).Get(file)
+	return file, err
+}

+ 2 - 1
models/cm_tree_contracts.go

@@ -15,7 +15,8 @@ type CmTreeContracts struct {
 	Serial       int       `xorm:"not null comment('序号') INT(11)"`
 	Attribution  string    `xorm:"comment('归属') VARCHAR(32)"`
 	Sort         int       `xorm:"not null comment('排序') TINYINT(4)"`
-	ContractId   int       `xorm:"not null comment('合同ID') INT(11)"`
+	ContractId   int       `xorm:"not null comment('pdf ID') INT(11)"`
+	ContractId2  int       `xorm:"not null comment('excel ID') INT(11)"`
 	ContractName string    `xorm:"comment('合同名称') VARCHAR(64)"`
 	ContractCode string    `xorm:"comment('合同编号') VARCHAR(32)"`
 	CreateTime   time.Time `xorm:"comment('创建时间') DATETIME"`

+ 15 - 6
services/contract_section_tree_service.go

@@ -32,9 +32,12 @@ func (s *contractService) GetSecionTree(operation string) *viewmodels.TreeSectio
 	return Node
 }
 
-func (s *contractService) GetSection(id int) *viewmodels.TreeSectionDetail {
-	data := s.treeContractDao.GetDetail(id)
-	return data
+func (s *contractService) GetSection(id int) (string, *viewmodels.TreeSectionDetail, *viewmodels.TreeSectionDetail) {
+
+	name := s.treeContractDao.GetDetailName(id)
+	pdfData := s.treeContractDao.GetDetailWithPdf(id)
+	excelData := s.treeContractDao.GetDetailWithExcel(id)
+	return name, pdfData, excelData
 }
 
 // 获得合同项目节-不包含合同
@@ -168,9 +171,9 @@ func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract
 		return nil, err
 	}
 	sectionFather := s.treeContractDao.Get(treeId)
-	if sectionFather.Id == 0 {
-		return nil, errors.New("未找到合同项目节")
-	}
+	// if sectionFather.Id == 0 {
+	// 	return nil, errors.New("未找到合同项目节")
+	// }
 	// 1-1 深度为>=2才能新增项目节
 	// if sectionFather.Depth < 3 {
 	// 	return errors.New("请在项目节第三层开始编辑")
@@ -203,8 +206,14 @@ func (s *contractService) SectionAdd(sectionData *viewmodels.TreeSectionContract
 	sectionCM.Depth = sectionFather.Depth + 1
 	sectionCM.Serial = serial
 	sectionCM.Attribution = attribution
+	sectionCM.CreateTime = time.Now()
 	sectionCM.Code = code
 	sectionCM.Code2 = sectionData.Code2
+	if sectionFather.Id == 0 {
+		sectionCM.Depth = 0
+		sectionCM.Attribution = ""
+		sectionCM.Code = fmt.Sprintf("%d", serial)
+	}
 
 	data, err := s.treeContractDao.Create(sectionCM)
 	if err != nil {

+ 1 - 1
services/contract_service.go

@@ -37,7 +37,7 @@ type ContractService interface {
 	// 项目节
 	GetSecionTree(operation string) *viewmodels.TreeSectionContract
 	SetSection() error
-	GetSection(id int) *viewmodels.TreeSectionDetail
+	GetSection(id int) (string, *viewmodels.TreeSectionDetail, *viewmodels.TreeSectionDetail)
 	SectionAdd(sectionData *viewmodels.TreeSectionContract) (*models.CmTreeContracts, error)
 	SectionSave(sectionData *viewmodels.TreeSectionContract) error
 	UpdateSerial(sectionData *viewmodels.TreeSectionContract) error

+ 28 - 0
services/upload_service.go

@@ -0,0 +1,28 @@
+package services
+
+import (
+	"go.mod/dao"
+	"go.mod/datasource"
+	"go.mod/models"
+)
+
+type UploadService interface {
+	Get(id int) (*models.CmUpload, error)
+}
+
+//返回service操作类
+type uploadService struct {
+	dao *dao.UploadDao
+}
+
+//创建项目用户service
+func NewUploadService() UploadService {
+	return &uploadService{
+		dao: dao.NewUploadDao(datasource.InstanceDbMaster()),
+	}
+}
+
+func (s *uploadService) Get(id int) (*models.CmUpload, error) {
+	file, err := s.dao.GetFile(id)
+	return file, err
+}

+ 10 - 4
web/api/contract_api.go

@@ -58,10 +58,12 @@ func (c *ContractApi) GetSectionAll() {
 	//获得合同项目节
 	sectionTree := c.ServiceContract.GetSecionTree(sectionData.Operation)
 
+	result := make([]*viewmodels.TreeSectionContract, 0)
+	result = append(result, sectionTree)
 	c.Ctx.JSON(iris.Map{
 		"code":   0,
 		"msg":    "",
-		"result": sectionTree,
+		"result": result,
 	})
 }
 
@@ -84,11 +86,15 @@ func (c *ContractApi) GetSection() {
 	id, _ := utils.GetDecryptId(sectionData.Id)
 
 	//获得合同项目节
-	sectionTree := c.ServiceContract.GetSection(id)
-
+	name, pdfData, excelData := c.ServiceContract.GetSection(id)
+	result := map[string]interface{}{
+		"name":      name,
+		"pdfData":   pdfData,
+		"excelData": excelData,
+	}
 	c.Ctx.JSON(iris.Map{
 		"code":   0,
 		"msg":    "",
-		"result": sectionTree,
+		"result": result,
 	})
 }

+ 54 - 6
web/api/upload_api.go

@@ -18,6 +18,8 @@ import (
 	"github.com/kataras/iris/v12"
 	"go.mod/conf"
 	"go.mod/services"
+	"go.mod/web/utils"
+	"go.mod/web/viewmodels"
 )
 
 type UploadApi struct {
@@ -25,6 +27,7 @@ type UploadApi struct {
 	Ctx iris.Context
 	// // 需要用的service
 	ServiceContract services.ContractService
+	ServiceUpload   services.UploadService
 }
 
 // Upload : 处理文件上传
@@ -45,7 +48,6 @@ func (c *UploadApi) Post() {
 	file, head, err := c.Ctx.FormFile("file")
 	if err != nil {
 		log.Printf("Failed to get form data, err:%s\n", err.Error())
-		fmt.Println("1111")
 		errCode = -1
 		return
 	}
@@ -54,7 +56,6 @@ func (c *UploadApi) Post() {
 	buf := bytes.NewBuffer(nil)
 	if _, err := io.Copy(buf, file); err != nil {
 		log.Printf("Failed to get file data, err:%s\n", err.Error())
-		fmt.Println("2222")
 		errCode = -2
 		return
 	}
@@ -64,11 +65,10 @@ func (c *UploadApi) Post() {
 	FileSize := int64(len(buf.Bytes()))
 	UploadAt := fmt.Sprintf("%d", time.Now().UnixNano())
 
-	Location := conf.MergeLocalRootDir + UploadAt + head.Filename // 存储地址
+	Location := conf.MergeLocalRootDir + UploadAt // 存储地址
 	webLocation := "/public/" + UploadAt + head.Filename
 	newFile, err := os.Create(Location)
 	if err != nil {
-		fmt.Println("33333")
 		log.Printf("Failed to create file, err:%s\n", err.Error())
 		errCode = -3
 		return
@@ -77,7 +77,6 @@ func (c *UploadApi) Post() {
 
 	nByte, err := newFile.Write(buf.Bytes())
 	if int64(nByte) != FileSize || err != nil {
-		fmt.Println("44444")
 		log.Printf("Failed to save data into file, writtenSize:%d, err:%s\n", nByte, err.Error())
 		errCode = -4
 		return
@@ -105,9 +104,58 @@ func (c *UploadApi) Post() {
 
 	id, err = c.ServiceContract.SaveUpload(webLocation, head.Filename, ext)
 	if err != nil {
-		fmt.Println("55555")
 		errCode = -6
 		return
 	}
 
 }
+
+func (c *UploadApi) GetFile() {
+	fileData := viewmodels.Upload{}
+	err := c.Ctx.ReadForm(&fileData)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	id, err := utils.GetDecryptId(fileData.Id)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": "解析异常,请检查参数"})
+		return
+	}
+	fileVM, err := c.ServiceUpload.Get(id)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	// c.Ctx.Header("Content-Type", "text/html; charset=utf-8;")
+	// c.Ctx.Header("Content-Disposition", "inline;"+fileVM.Name)
+	filepath := conf.NormalFileStorage + fileVM.Path
+	file, err := os.Open(filepath)
+	readseek := io.ReadSeeker(file)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	// 读取文件缓冲区
+	// var buf [128]byte
+	// // 文件内容字节数组
+	// var content []byte
+	// for {
+	// 	n, err := file.Read(buf[:])
+	// 	if err == io.EOF {
+	// 		// 文件读取完成
+	// 		break
+	// 	}
+	// 	if err != nil {
+	// 		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+	// 		return
+	// 	}
+	// 	content = append(content, buf[:n]...)
+	// }
+	// c.Ctx.Header("Content-Disposition", "inline;filename="+fileVM.Name+";")
+	c.Ctx.ServeContent(readseek, fileVM.Name, time.Now())
+	// c.Ctx.Header("Content-Type", "text/html; charset=utf-8;")
+	// p, e := c.Ctx.Write(content)
+	// fmt.Println("p", p)
+	// fmt.Println("e", e)
+}

+ 4 - 0
web/build.bat

@@ -0,0 +1,4 @@
+SET CGO_ENABLED=0
+SET GOOS=linux
+SET GOARCH=amd64
+go build main.go

BIN
web/main


+ 2 - 0
web/routes/routes.go

@@ -19,6 +19,7 @@ func Configure(b *bootstrap.Bootstrapper) {
 	ProjectAccountService := services.NewProjectAccountService()
 	LoginService := services.NewLoginService()
 	ContractService := services.NewContractService()
+	UploadService := services.NewUploadService()
 
 	//CSRF相关
 	//  b.Use(middleware.SetCsrf)
@@ -48,6 +49,7 @@ func Configure(b *bootstrap.Bootstrapper) {
 
 	apiUpload := mvc.New(b.Party("/api/upload"))
 	apiUpload.Register(ContractService)
+	apiUpload.Register(UploadService)
 	apiUpload.Handle(new(api.UploadApi))
 
 	// oss相关

+ 2 - 2
web/viewmodels/upload.go

@@ -7,8 +7,8 @@
 package viewmodels
 
 type Upload struct {
-	Id     int    `form:"id" json:"id"`
+	Id     string `form:"id" json:"id"`
 	Name   string `form:"name" json:"name"`
 	Path   string `form:"path" json:"path"`
-	TreeId int    `form:"treeId" json:"treeId"`
+	TreeId string `form:"treeId" json:"treeId"`
 }