tree_service.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * @description:层级文件夹数据操作相关
  3. * @Author: CP
  4. * @Date: 2020-09-11 14:43:58
  5. * @FilePath: \construction_management\services\tree_service.go
  6. */
  7. package services
  8. import (
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "log"
  13. "strconv"
  14. "time"
  15. "github.com/kataras/iris/v12"
  16. "go.mod/comm"
  17. "go.mod/conf"
  18. "go.mod/dao"
  19. "go.mod/datasource"
  20. "go.mod/models"
  21. "go.mod/web/utils"
  22. "go.mod/web/viewmodels"
  23. )
  24. // 定义项目Service接口
  25. type TreeService interface {
  26. //ValidManager(code string, account string, password string) error
  27. ValidRule(ctx iris.Context) (viewmodels.Tree, error)
  28. ValidRuleBidsectionType(ctx iris.Context) (viewmodels.Permission, error)
  29. Create(data viewmodels.Tree) error
  30. GetAllProject(projectId int) *viewmodels.Tree
  31. GetAllContract(projectId int, account *models.CmProjectAccount, bidsectionType int) *viewmodels.FolderContract
  32. GetAllContractByJL(projectId int) *viewmodels.FolderContract
  33. Rename(treevm viewmodels.Tree, projectId int) error
  34. GetFolderAndBid(id int, projectId int) ([]models.CmTree, error)
  35. DeleteFolderAndBid(id int, projectId int) error
  36. Move(id int, moveId int, projectId int) error
  37. }
  38. // 返回service操作类
  39. type treeService struct {
  40. dao *dao.TreeDao
  41. permissionAccountDao *dao.PermissionAccountDao
  42. }
  43. // 创建项目service
  44. func NewTreeService() TreeService {
  45. return &treeService{
  46. dao: dao.NewTreeDao(datasource.InstanceDbMaster()),
  47. permissionAccountDao: dao.NewPermissionAccountDao(datasource.InstanceDbMaster()),
  48. }
  49. }
  50. // 文件夹规则验证
  51. func (s *treeService) ValidRule(ctx iris.Context) (viewmodels.Tree, error) {
  52. folderVaild := viewmodels.Tree{}
  53. err := ctx.ReadJSON(&folderVaild)
  54. if err != nil {
  55. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  56. return folderVaild, err
  57. }
  58. err = folderVaild.Validate()
  59. if err != nil {
  60. log.Println("文件夹验证, error=", err)
  61. return folderVaild, err
  62. }
  63. return folderVaild, nil
  64. }
  65. // 文件夹规则验证
  66. func (s *treeService) ValidRuleBidsectionType(ctx iris.Context) (viewmodels.Permission, error) {
  67. folderVaild := viewmodels.Permission{}
  68. err := ctx.ReadForm(&folderVaild)
  69. if err != nil {
  70. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  71. return folderVaild, err
  72. }
  73. err = folderVaild.ValidateType()
  74. if err != nil {
  75. log.Println("请求标段类型验证, error=", err)
  76. return folderVaild, err
  77. }
  78. return folderVaild, nil
  79. }
  80. // 获得项目下 相关文件夹-整个树结构
  81. func (s *treeService) GetAllProject(projectId int) *viewmodels.Tree {
  82. datalist := s.dao.GetAllTree(projectId)
  83. folderlist := make([]*viewmodels.Tree, 0)
  84. // 生成根
  85. folder := &viewmodels.Tree{}
  86. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  87. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  88. folder.Id = id
  89. folder.Name = "根目录"
  90. folder.Isfolder = 1
  91. folder.ParentId = parentId
  92. folder.Children = make([]*viewmodels.Tree, 0)
  93. folderlist = append(folderlist, folder)
  94. // 加入数据
  95. for _, data := range datalist {
  96. folder := &viewmodels.Tree{}
  97. //folder.Id = comm.Encrypt([]byte(conf.SignSecret), []byte(strconv.Itoa(data.Id)))
  98. id, _ := comm.AesEncrypt(strconv.Itoa(data.Id), conf.SignSecret)
  99. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  100. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  101. serial, _ := comm.AesEncrypt(strconv.Itoa(data.Serial), conf.SignSecret)
  102. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  103. folder.Id = id
  104. folder.Name = data.Name
  105. folder.ParentId = parentId
  106. folder.ProjectId = projectId
  107. folder.BidsectionId = bidsectionId
  108. folder.Depth = data.Depth + 1
  109. folder.Serial = serial
  110. folder.Accounts = data.Accounts
  111. folder.Isfolder = data.Isfolder
  112. folder.IsEnd = false
  113. //folder.Leaf = true
  114. folder.HasFolder = false
  115. folder.IsBid = false
  116. folder.CreateTime = data.CreateTime.Format(conf.SysTimeform)
  117. folderlist = append(folderlist, folder)
  118. }
  119. //fmt.Println(folderlist)
  120. //var data []*viewmodels.Tree
  121. // data := make([]*viewmodels.Tree, 0)
  122. // for i, _ := range folderlist {
  123. // var a *viewmodels.Tree
  124. // a = &folderlist[i]
  125. // data = append(data, a)
  126. // }
  127. node := folderlist[0] //父节点
  128. maketree(folderlist, node) //调用生成tree
  129. //transformjson(node) //转化为json
  130. return node
  131. }
  132. // 获得合同管理的目录
  133. func (s *treeService) GetAllContract(projectId int, account *models.CmProjectAccount, bidsectionType int) *viewmodels.FolderContract {
  134. datalist := s.dao.GetAllTree(projectId)
  135. folderlist := make([]viewmodels.FolderContract, 0)
  136. // 2.获得该账号的权限
  137. permissionData := s.permissionAccountDao.GetProjectIdAccountId(projectId, account.Id)
  138. // 生成根
  139. folder := viewmodels.FolderContract{}
  140. id, _ := comm.AesEncrypt(strconv.Itoa(0), conf.SignSecret)
  141. parentId, _ := comm.AesEncrypt(strconv.Itoa(-1), conf.SignSecret)
  142. folder.Id = id
  143. folder.Name = "root"
  144. folder.ParentId = parentId
  145. folder.Isfolder = 1
  146. // folder.Children = make([]*viewmodels.FolderContract, 0)
  147. folderlist = append(folderlist, folder)
  148. // 加入数据
  149. for _, data := range datalist {
  150. flag := true
  151. // 标段过滤开始
  152. if data.BidsectionId != 0 && account.IsAdmin != 1 {
  153. flag = false
  154. }
  155. // 过滤没有权限访问的标段-管理员不需要过滤
  156. if data.BidsectionId != 0 && account.IsAdmin != 1 {
  157. permission := map[string]int{}
  158. for _, item := range permissionData {
  159. if data.BidsectionId == item.BidsectionId {
  160. // 区别合同,安全,质量
  161. if bidsectionType == 1 {
  162. json.Unmarshal([]byte(item.SafePermission), &permission)
  163. } else if bidsectionType == 2 {
  164. json.Unmarshal([]byte(item.QualityPermission), &permission)
  165. } else if bidsectionType == 0 {
  166. json.Unmarshal([]byte(item.ContractPermission), &permission)
  167. // fmt.Println(permission)
  168. // if permission["access"] == 1 {
  169. // flag = true
  170. // break
  171. // }
  172. } else {
  173. return nil
  174. }
  175. if permission["access"] == 1 {
  176. flag = true
  177. break
  178. }
  179. }
  180. }
  181. }
  182. // 标段过滤结束
  183. if flag {
  184. folder := viewmodels.FolderContract{}
  185. id, _ := comm.AesEncrypt(strconv.Itoa(data.Id), conf.SignSecret)
  186. parentId, _ := comm.AesEncrypt(strconv.Itoa(data.ParentId), conf.SignSecret)
  187. projectId, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  188. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  189. folder.Id = id
  190. folder.Name = data.Name
  191. folder.ParentId = parentId
  192. folder.ProjectId = projectId
  193. folder.BidsectionId = bidsectionId
  194. // 合同数据
  195. folder.Contracts = data.Contracts
  196. contractsIncome, _ := strconv.ParseFloat(data.ContractsIncome, 64)
  197. contractsReturned, _ := strconv.ParseFloat(data.ContractsReturned, 64)
  198. folder.ContractsIncome = utils.PriceToStringFormat(contractsIncome)
  199. folder.ContractsReturned = utils.PriceToStringFormat(contractsReturned)
  200. contractDeductionTotal, _ := strconv.ParseFloat(data.ContractDeductionTotal, 64)
  201. folder.ContractDeductionTotal = utils.PriceToStringFormat(contractDeductionTotal)
  202. // 汇款进度
  203. ContractsIncome, err := strconv.ParseFloat(data.ContractsIncome, 64)
  204. if err != nil {
  205. ContractsIncome = 0
  206. }
  207. ContractsReturned, err := strconv.ParseFloat(data.ContractsReturned, 64)
  208. if err != nil || ContractsReturned == 0 {
  209. ContractsReturned = 0
  210. folder.ContractsIncomeProgress = "0"
  211. } else {
  212. folder.ContractsIncomeProgress = fmt.Sprintf("%.2f", (ContractsReturned/ContractsIncome)*100)
  213. }
  214. contractsPay, _ := strconv.ParseFloat(data.ContractsPay, 64)
  215. contractsPaid, _ := strconv.ParseFloat(data.ContractsPaid, 64)
  216. folder.ContractsPay = utils.PriceToStringFormat(contractsPay)
  217. folder.ContractsPaid = utils.PriceToStringFormat(contractsPaid)
  218. // 支付进度
  219. ContractsPay, err := strconv.ParseFloat(data.ContractsPay, 64)
  220. if err != nil {
  221. ContractsPay = 0
  222. }
  223. ContractsPaid, err := strconv.ParseFloat(data.ContractsPaid, 64)
  224. if err != nil || ContractsPaid == 0 {
  225. ContractsPaid = 0
  226. folder.ContractsPayProgress = "0"
  227. } else {
  228. folder.ContractsPayProgress = fmt.Sprintf("%.2f", (ContractsPaid/ContractsPay)*100)
  229. }
  230. // 安全
  231. folder.SafeTotal = data.SafeTotal
  232. folder.SafeRectification = data.SafeRectification
  233. folder.SafeRectificationFinish = data.SafeRectificationFinish
  234. folder.SafeRectificationIn = data.SafeRectificationIn
  235. folder.SafeApproval = data.SafeApproval
  236. folder.SafeClose = data.SafeClose
  237. // 质量
  238. folder.QualityTotal = data.QualityTotal
  239. folder.QualityApproval = data.QualityApproval
  240. folder.QualityRectification = data.QualityRectification
  241. folder.QualityRectificationFinish = data.QualityRectificationFinish
  242. folder.QualityRectificationIn = data.QualityRectificationIn
  243. folder.QualityClose = data.QualityClose
  244. folder.Isfolder = data.Isfolder
  245. folder.IsEnd = false
  246. folder.HasFolder = false
  247. folder.IsBid = false
  248. folderlist = append(folderlist, folder)
  249. }
  250. }
  251. var data []*viewmodels.FolderContract
  252. data = make([]*viewmodels.FolderContract, 0)
  253. for i, _ := range folderlist {
  254. var a *viewmodels.FolderContract
  255. a = &folderlist[i]
  256. data = append(data, a)
  257. }
  258. node := &folderlist[0] //父节点
  259. comm.MakeFolderContract(data, node)
  260. // 求和
  261. // dataLenght := len(data) + 4
  262. s.filterFolder(node)
  263. // 删除空目录
  264. ChildrenShow := make([]*viewmodels.FolderContract, 0)
  265. for _, item := range node.Children {
  266. if item.IsHidden == 0 {
  267. ChildrenShow = append(ChildrenShow, item)
  268. }
  269. }
  270. node.Children = ChildrenShow
  271. // node.Children = s.copyShowFolder(node.Children)
  272. // fmt.Println(node)
  273. return node
  274. }
  275. // 复制出需要展示的目录
  276. // func (s *treeService) copyShowFolder(children []*viewmodels.FolderContract) []*viewmodels.FolderContract {
  277. // ChildrenShow := make([]*viewmodels.FolderContract, 0)
  278. // for _, item := range children {
  279. // if item.IsHidden == 0 {
  280. // ChildrenShow = append(ChildrenShow, item)
  281. // }
  282. // }
  283. // return ChildrenShow
  284. // }
  285. func (s *treeService) filterFolder(node *viewmodels.FolderContract) {
  286. if node == nil {
  287. return
  288. }
  289. // 目录下是否为空目录 1空目录
  290. isEmptyFolder := 1
  291. length := len(node.Children)
  292. // 如果length为0叶子节点---为目录才需要遍历
  293. if length == 0 {
  294. // 1.如果是目录-已经为叶子目录不需要遍历
  295. // 2.是标段 目录不为空
  296. if node.Isfolder == 0 {
  297. isEmptyFolder = 0
  298. }
  299. } else {
  300. // 1.遍历目录下
  301. for i := 0; i < length; i++ {
  302. // 2.该节点下子对象 从0-length 依次进入
  303. s.filterFolder(node.Children[i])
  304. // 后序-父节点逻辑 处理完0 ... -length
  305. // 2-1 是标段 设置目录不为空
  306. if node.Children[i].Isfolder == 0 {
  307. // 有标段
  308. isEmptyFolder = 0
  309. } else {
  310. // 2-2 是目录 ----后序遍历,最后一层下的孩子们是否能删除 走完三个点后返回到父亲
  311. // 2-2-1 可展示目录 集合
  312. ChildrenShow := make([]*viewmodels.FolderContract, 0)
  313. // 2-2-2 目录下内容
  314. if len(node.Children[i].Children) != 0 {
  315. // 2-2-3 内容中是否有需要显示的目录
  316. for _, item := range node.Children[i].Children {
  317. // 2-2-4 有 设置为需要显示并拷贝显示内容
  318. if item.IsHidden == 0 {
  319. ChildrenShow = append(ChildrenShow, item)
  320. node.Children[i].IsHidden = 0
  321. }
  322. }
  323. }
  324. // 2-2-5 覆盖父亲节点的显示内容
  325. node.Children[i].Children = ChildrenShow
  326. // s.copyShowFolder(node.Children[i].Children)
  327. // fmt.Println("==========filterFolder执行完成")
  328. // fmt.Println(node.Children[i].Name)
  329. // fmt.Println(fmt.Sprintf("删除:%d", node.Children[i].IsHidden))
  330. // fmt.Println(node.Children[i].Children)
  331. }
  332. }
  333. }
  334. // fmt.Println("=========================进入")
  335. // fmt.Println(node.Name)
  336. // fmt.Println(fmt.Sprintf("空目录:%d", isEmptyFolder))
  337. // 后序的第一位叶子节点 如果目录为空,设置不显示
  338. if isEmptyFolder == 1 {
  339. node.IsHidden = 1
  340. }
  341. }
  342. // 新增一个文件夹
  343. func (s *treeService) Create(data viewmodels.Tree) error {
  344. // 类型校验
  345. folder := models.CmTree{}
  346. folder.Name = data.Name
  347. // 项目ID
  348. ProjectId, err := strconv.Atoi(data.ProjectId)
  349. if err != nil {
  350. return err
  351. }
  352. // 获得该目录ID
  353. Id, err := comm.AesDecrypt(data.Id, conf.SignSecret)
  354. if err != nil {
  355. return err
  356. }
  357. IdInt, err1 := strconv.Atoi(Id)
  358. if err1 != nil {
  359. return err1
  360. }
  361. // 该目录中是否有标段
  362. bidlist := s.dao.GetBidsection(IdInt)
  363. if len(bidlist) > 0 {
  364. return errors.New("该文件夹已存在标段,不能在新增目录")
  365. }
  366. // 获得该深度的文件夹最大序号
  367. serial := 0
  368. depth := data.Depth + 1
  369. if data.Depth == -1 {
  370. // 1-创建顶级目录--获得最大序号
  371. datalist := s.dao.GetAllDepth(depth, ProjectId)
  372. maxIndex := len(datalist)
  373. if maxIndex != 0 {
  374. serial = datalist[maxIndex-1].Serial + 1
  375. }
  376. } else {
  377. // 1-创建次目录-获得次级目录最大序号
  378. treeNode := s.dao.Get(IdInt, ProjectId)
  379. // 去除前端depth作用-在这里获得下级目录的深度
  380. depth = treeNode.Depth + 1
  381. if treeNode.Id == 0 {
  382. return errors.New("上级目录不正确")
  383. }
  384. attribution := fmt.Sprintf("%s%d-", treeNode.Attribution, treeNode.Serial)
  385. datalist := s.dao.GetALLDepthByAttribution(depth, ProjectId, attribution)
  386. maxIndex := len(datalist)
  387. if maxIndex != 0 {
  388. serial = datalist[maxIndex-1].Serial + 1
  389. }
  390. // 设置归属
  391. folder.Attribution = attribution //treeNode.Attribution + strconv.Itoa(treeNode.Serial) + "-"
  392. folder.ParentId = IdInt
  393. }
  394. folder.ProjectId = ProjectId
  395. folder.Serial = serial
  396. folder.Depth = depth
  397. folder.Isfolder = 1
  398. folder.CreateTime = time.Now()
  399. folder.UpdateTime = time.Now()
  400. folder.ContractsIncome = "0"
  401. folder.ContractsPaid = "0"
  402. folder.ContractsPay = "0"
  403. folder.ContractDeductionTotal = "0"
  404. folder.ContractsReturned = "0"
  405. err = s.dao.Create(&folder)
  406. if err != nil {
  407. log.Println("添加目录出错, error=", err)
  408. return errors.New("添加目录出错")
  409. }
  410. return nil
  411. }
  412. // 重命名
  413. func (s *treeService) Rename(data viewmodels.Tree, projectId int) error {
  414. // 获得该目录ID
  415. Id, err := comm.AesDecrypt(data.Id, conf.SignSecret)
  416. if err != nil {
  417. return err
  418. }
  419. IdInt, err1 := strconv.Atoi(Id)
  420. if err1 != nil {
  421. return err1
  422. }
  423. // 获得树信息
  424. treeData := s.dao.Get(IdInt, projectId)
  425. if treeData.Id == 0 {
  426. return errors.New("修改的目录不合法")
  427. }
  428. treeNode := models.CmTree{}
  429. treeNode.Id = IdInt
  430. treeNode.Name = data.Name
  431. if treeData.Isfolder == 1 {
  432. // 目录
  433. err = s.dao.Update(&treeNode, []string{"Name"})
  434. } else {
  435. // 标段
  436. treeNode.BidsectionId = treeData.BidsectionId
  437. err = s.dao.Rename(&treeNode, []string{"Name"})
  438. }
  439. if err != nil {
  440. return err
  441. }
  442. return nil
  443. }
  444. // 获得该目录下所有的目录和标段
  445. func (s *treeService) GetFolderAndBid(id int, projectId int) ([]models.CmTree, error) {
  446. // 获得该目录ID
  447. // id, err := comm.AesDecrypt(id, conf.SignSecret)
  448. // if err != nil {
  449. // return nil, errors.New("目录解析错误")
  450. // }
  451. // IdInt, err := strconv.Atoi(id)
  452. // if err != nil {
  453. // return nil, errors.New("目录解析错误")
  454. // }
  455. // 获得该目录下所有数据
  456. folder := s.dao.Get(id, projectId)
  457. if folder.Id == 0 {
  458. return nil, errors.New("目录不合法")
  459. }
  460. attribution := fmt.Sprintf("%s%d-", folder.Attribution, folder.Serial)
  461. // 获得目录归属
  462. dataList := s.dao.GetFolderAndBid(folder.ProjectId, attribution)
  463. return dataList, nil
  464. }
  465. // 删除该目录和所属的目录和标段
  466. func (s *treeService) DeleteFolderAndBid(id int, projectId int) error {
  467. // 获得该目录ID
  468. // id, err := comm.AesDecrypt(id, conf.SignSecret)
  469. // if err != nil {
  470. // return errors.New("目录解析错误")
  471. // }
  472. // IdInt, err := strconv.Atoi(id)
  473. // if err != nil {
  474. // return errors.New("目录解析错误")
  475. // }
  476. // 获得该目录下 归属关系
  477. folder := s.dao.Get(id, projectId)
  478. if folder.Id == 0 {
  479. return errors.New("目录不合法")
  480. }
  481. attribution := fmt.Sprintf("%s%d-", folder.Attribution, folder.Serial)
  482. // --逻辑删除 目录和数据
  483. err := s.dao.DeleteFolderAndBid(folder.Id, folder.ProjectId, attribution)
  484. if err != nil {
  485. return err
  486. }
  487. // TODO -加入堆栈 物理删除数据
  488. return nil
  489. }
  490. // 移动目录
  491. func (s *treeService) Move(id int, moveId int, projectId int) error {
  492. // 获得项目目录
  493. treeNode := s.dao.Get(id, projectId)
  494. if treeNode.Id == 0 {
  495. return errors.New("目录或标段解析错误")
  496. }
  497. targetFolder := &models.CmTree{}
  498. // id为0是跟目录
  499. if moveId == 0 {
  500. // id
  501. targetFolder.Id = 0
  502. // 1.获得最大序号
  503. // seriallist := s.dao.GetALLDepthByAttribution(0, projectId, "")
  504. // maxIndex := len(seriallist)
  505. // serial := 0
  506. // if maxIndex != 0 {
  507. // serial = seriallist[maxIndex-1].Serial + 1
  508. // }
  509. // targetFolder.Serial = serial
  510. targetFolder.Serial = 0
  511. // 2.归属
  512. targetFolder.Attribution = ""
  513. // 3.深度depth-根目录为-1
  514. targetFolder.Depth = -1
  515. // 4.为目录
  516. targetFolder.Isfolder = 1
  517. // 5.项目ID
  518. targetFolder.ProjectId = projectId
  519. // fmt.Println(targetFolder)
  520. // if targetFolder.Id == 0 {
  521. // return errors.New("目录解析错误-测试")
  522. // }
  523. } else {
  524. targetFolder = s.dao.Get(moveId, projectId)
  525. if targetFolder.Id == 0 {
  526. return errors.New("目录解析错误")
  527. }
  528. }
  529. // 目录必须是目录
  530. if targetFolder.Isfolder == 0 {
  531. return errors.New("目标不是文件夹")
  532. }
  533. // 目录的目标不能是自己
  534. if id == moveId {
  535. return errors.New("目标目录是原目录-不能移动")
  536. }
  537. // 移动的是目录
  538. if treeNode.Isfolder == 1 {
  539. // 1.目录下不能有标段
  540. // 被移动的目录存在标段
  541. moveBidList := s.dao.GetBidsection(moveId)
  542. if len(moveBidList) > 0 {
  543. return errors.New("该目录中存在标段,不能移动")
  544. }
  545. } else {
  546. // 标段
  547. // 1.移动的目录是否为叶子节点
  548. moveBidList := s.dao.GetChildFolder(moveId)
  549. if len(moveBidList) > 0 {
  550. return errors.New("该目录中存在其他目录,不能移动")
  551. }
  552. }
  553. err := s.dao.Move(treeNode, targetFolder)
  554. if err != nil {
  555. return err
  556. }
  557. return nil
  558. }
  559. // 获得合同管理的目录
  560. func (s *treeService) GetAllContractByJL(projectId int) *viewmodels.FolderContract {
  561. datalist := s.dao.GetAllTree(projectId)
  562. folderlist := make([]viewmodels.FolderContract, 0)
  563. // 生成根
  564. folder := viewmodels.FolderContract{}
  565. id := strconv.Itoa(0)
  566. parentId := strconv.Itoa(-1)
  567. folder.Id = id
  568. folder.Name = "root"
  569. folder.ParentId = parentId
  570. folder.Isfolder = 1
  571. // folder.Children = make([]*viewmodels.FolderContract, 0)
  572. folderlist = append(folderlist, folder)
  573. // 加入数据
  574. for _, data := range datalist {
  575. folder := viewmodels.FolderContract{}
  576. id := strconv.Itoa(data.Id)
  577. parentId := strconv.Itoa(data.ParentId)
  578. projectId := strconv.Itoa(data.ProjectId)
  579. bidsectionId := strconv.Itoa(data.BidsectionId)
  580. folder.Id = id
  581. folder.Name = data.Name
  582. folder.ParentId = parentId
  583. folder.ProjectId = projectId
  584. folder.BidsectionId = bidsectionId
  585. // 合同数据
  586. folder.Contracts = data.Contracts
  587. contractsIncome, _ := strconv.ParseFloat(data.ContractsIncome, 64)
  588. contractsReturned, _ := strconv.ParseFloat(data.ContractsReturned, 64)
  589. folder.ContractsIncome = utils.PriceToStringFormat(contractsIncome)
  590. folder.ContractsReturned = utils.PriceToStringFormat(contractsReturned)
  591. contractDeductionTotal, _ := strconv.ParseFloat(data.ContractDeductionTotal, 64)
  592. folder.ContractDeductionTotal = utils.PriceToStringFormat(contractDeductionTotal)
  593. // 汇款进度
  594. ContractsIncome, err := strconv.ParseFloat(data.ContractsIncome, 64)
  595. if err != nil {
  596. ContractsIncome = 0
  597. }
  598. ContractsReturned, err := strconv.ParseFloat(data.ContractsReturned, 64)
  599. if err != nil || ContractsReturned == 0 {
  600. ContractsReturned = 0
  601. folder.ContractsIncomeProgress = "0"
  602. } else {
  603. folder.ContractsIncomeProgress = fmt.Sprintf("%.2f", (ContractsReturned/ContractsIncome)*100)
  604. }
  605. contractsPay, _ := strconv.ParseFloat(data.ContractsPay, 64)
  606. contractsPaid, _ := strconv.ParseFloat(data.ContractsPaid, 64)
  607. folder.ContractsPay = utils.PriceToStringFormat(contractsPay)
  608. folder.ContractsPaid = utils.PriceToStringFormat(contractsPaid)
  609. // 支付进度
  610. ContractsPay, err := strconv.ParseFloat(data.ContractsPay, 64)
  611. if err != nil {
  612. ContractsPay = 0
  613. }
  614. ContractsPaid, err := strconv.ParseFloat(data.ContractsPaid, 64)
  615. if err != nil || ContractsPaid == 0 {
  616. ContractsPaid = 0
  617. folder.ContractsPayProgress = "0"
  618. } else {
  619. folder.ContractsPayProgress = fmt.Sprintf("%.2f", (ContractsPaid/ContractsPay)*100)
  620. }
  621. // 安全
  622. folder.SafeTotal = data.SafeTotal
  623. folder.SafeRectification = data.SafeRectification
  624. folder.SafeRectificationFinish = data.SafeRectificationFinish
  625. folder.SafeRectificationIn = data.SafeRectificationIn
  626. folder.SafeApproval = data.SafeApproval
  627. folder.SafeClose = data.SafeClose
  628. // 质量
  629. folder.QualityTotal = data.QualityTotal
  630. folder.QualityApproval = data.QualityApproval
  631. folder.QualityRectification = data.QualityRectification
  632. folder.QualityRectificationFinish = data.QualityRectificationFinish
  633. folder.QualityRectificationIn = data.QualityRectificationIn
  634. folder.QualityClose = data.QualityClose
  635. folder.Isfolder = data.Isfolder
  636. folder.IsEnd = false
  637. folder.HasFolder = false
  638. folder.IsBid = false
  639. folderlist = append(folderlist, folder)
  640. }
  641. var data []*viewmodels.FolderContract
  642. data = make([]*viewmodels.FolderContract, 0)
  643. for i, _ := range folderlist {
  644. var a *viewmodels.FolderContract
  645. a = &folderlist[i]
  646. data = append(data, a)
  647. }
  648. node := &folderlist[0] //父节点
  649. comm.MakeFolderContract(data, node)
  650. // 求和
  651. // dataLenght := len(data) + 4
  652. s.filterFolder(node)
  653. // 删除空目录
  654. ChildrenShow := make([]*viewmodels.FolderContract, 0)
  655. for _, item := range node.Children {
  656. if item.IsHidden == 0 {
  657. ChildrenShow = append(ChildrenShow, item)
  658. }
  659. }
  660. node.Children = ChildrenShow
  661. // node.Children = s.copyShowFolder(node.Children)
  662. // fmt.Println(node)
  663. return node
  664. }
  665. // 创建一颗树...interface{}
  666. func maketree(Data []*viewmodels.Tree, node *viewmodels.Tree) { //参数为父节点,添加父节点的子节点指针切片
  667. childs, _ := havechild(Data, node) //判断节点是否有子节点并返回
  668. if childs != nil {
  669. // fmt.Printf("\n")
  670. // fmt.Println(*node)
  671. // fmt.Println("子节点:")
  672. // for _, v := range childs {
  673. // fmt.Println(*v)
  674. // } //打印
  675. // 子节点总数
  676. Total := len(childs)
  677. node.ChildsTotal = Total
  678. // 标记最后一个元素
  679. end := Total - 1
  680. childs[end].IsEnd = true
  681. // 往父节点添加子节点
  682. node.Children = append(node.Children, childs[0:]...) //添加子节点
  683. for _, v := range childs { //查询子节点的子节点,并添加到子节点
  684. _, has := havechild(Data, v)
  685. if has {
  686. // 递归添加节点
  687. maketree(Data, v)
  688. }
  689. // else {
  690. // // 叶子节点
  691. // node.Leaf = false
  692. // }
  693. // 目录下是否包含标段
  694. if v.Isfolder == 0 {
  695. node.IsBid = true
  696. } else {
  697. node.HasFolder = true
  698. }
  699. }
  700. }
  701. }
  702. // 是否有子树
  703. func havechild(Data []*viewmodels.Tree, node *viewmodels.Tree) (child []*viewmodels.Tree, yes bool) {
  704. for _, v := range Data {
  705. if v.ParentId == node.Id {
  706. child = append(child, v)
  707. }
  708. }
  709. if child != nil {
  710. yes = true
  711. }
  712. return
  713. }
  714. func transformjson(Data *viewmodels.Tree) { //转为json
  715. Jsondata, _ := json.Marshal(Data)
  716. fmt.Println(string(Jsondata))
  717. }
  718. //now := comm.NowUnix()
  719. //comm.FormatFromUnixTime(now)
  720. // ParentIdInt := 0
  721. // if data.ParentId != "" {
  722. // ParentId, err := comm.AesDecrypt(data.ParentId, conf.SignSecret)
  723. // if err != nil {
  724. // return err
  725. // }
  726. // ParentIdInt, err = strconv.Atoi(ParentId)
  727. // if err != nil {
  728. // ParentIdInt = 0
  729. // }
  730. // }