contract_api.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * @description: 合同管理 相关API
  3. * @Author: CP
  4. * @Date: 2020-10-26 15:27:04
  5. * @FilePath: \construction_management\web\api\contract_api.go
  6. */
  7. package api
  8. import (
  9. "fmt"
  10. "github.com/kataras/iris/v12"
  11. "go.mod/lib"
  12. "go.mod/models"
  13. "go.mod/services"
  14. "go.mod/web/utils"
  15. "go.mod/web/viewmodels"
  16. )
  17. type ContractApi struct {
  18. //框架-web应用上下文环境
  19. Ctx iris.Context
  20. // // 需要用的service
  21. ServiceTree services.TreeService
  22. ServiceContract services.ContractService
  23. // ServiceLogin services.LoginService
  24. // ServiceProject services.ProjectService
  25. }
  26. // @Summary 获得合同目录和标段
  27. // @Tags 合同管理
  28. // @Description 获得合同目录和标段
  29. // @Accept json
  30. // @Produce json
  31. // @Param bidsectionType path string true "标段类型0合同1安全2质量"
  32. // @Success 200 {object} viewmodels.FolderContract "{code:0成功,-1参数类错误,data:viewmodels.ProjectAccount,msg:错误信息}"
  33. // @Router /api/contract/folder [get]
  34. func (c *ContractApi) GetFolder() {
  35. bidTypeData, err := c.ServiceTree.ValidRuleBidsectionType(c.Ctx)
  36. if err != nil {
  37. ErrMsg := utils.FormValidError(err)
  38. c.Ctx.JSON(iris.Map{"code": -1, "msg": ErrMsg})
  39. return
  40. }
  41. // 获得项目ID
  42. projectId, err := utils.GetProjectId(c.Ctx)
  43. if err != nil {
  44. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  45. return
  46. }
  47. // 获得项目账号ID
  48. // projectAccountId, err := utils.GetProjectAccountId(c.Ctx)
  49. // if err != nil {
  50. // c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  51. // return
  52. // }
  53. account := c.Ctx.Values().Get("account").(*models.CmProjectAccount)
  54. // 获得层级文件夹
  55. FolderData := c.ServiceTree.GetAllContract(projectId, account, bidTypeData.BidsectionType)
  56. c.Ctx.JSON(iris.Map{
  57. "code": 0,
  58. "msg": "",
  59. "data": FolderData,
  60. })
  61. }
  62. // @Summary 获得标段收入-项目节信息
  63. // @Tags 合同管理-收入合同
  64. // @Description 未设置合同项目节 返回项目节模板信息
  65. // @Accept json
  66. // @Produce json
  67. // @Security ApiKeyAuth
  68. // @Param bidsectionId path string true "标段ID"
  69. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,data:viewmodels.TreeSectionContract,msg:错误信息}"
  70. // @Router /api/contract/income/section/all [get]
  71. func (c *ContractApi) GetIncomeSectionAll() {
  72. sectionData := viewmodels.TreeSectionContract{}
  73. err := c.Ctx.ReadForm(&sectionData)
  74. if err != nil {
  75. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  76. return
  77. }
  78. if sectionData.BidsectionId == "" {
  79. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  80. return
  81. }
  82. bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId)
  83. if err != nil {
  84. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  85. return
  86. }
  87. // 项目ID
  88. projectIdInt, err := utils.GetProjectId(c.Ctx)
  89. if err != nil {
  90. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  91. return
  92. }
  93. //获得合同项目节
  94. sectionTree := c.ServiceContract.GetSecionTree(bidsectionId, projectIdInt, 0)
  95. // 1.未设置了项目节
  96. if len(sectionTree.Children) == 0 {
  97. // 返回项目节2个基础模板
  98. templateTree1 := lib.NewItemSection().TemplateTree1
  99. templateTree2 := lib.NewItemSection().TemplateTree2
  100. c.Ctx.JSON(iris.Map{
  101. "code": 0,
  102. "msg": "",
  103. "isTemplate": 1,
  104. "sectionTemplate1": templateTree1,
  105. "sectionTemplate2": templateTree2,
  106. })
  107. return
  108. //2.项目节已设置
  109. } else {
  110. // 2.已设置项目节
  111. c.Ctx.JSON(iris.Map{
  112. "code": 0,
  113. "msg": "",
  114. "isTemplate": 0,
  115. "sectionTree": sectionTree,
  116. })
  117. return
  118. // 返回项目相关所有信息
  119. }
  120. }
  121. // @Summary 单个合同和项目节
  122. // @Tags 合同管理-收入合同
  123. // @Description 获得合同详情和项目节详情
  124. // @Accept json
  125. // @Produce json
  126. // @Security ApiKeyAuth
  127. // @Param id path string true "项目节ID"
  128. // @Param bidsectionId path string true "标段ID"
  129. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,isContract:是否有合同(包含孩子们),section:viewmodels.TreeSectionContract,msg:错误信息}"
  130. // @Router /api/contract/income [get]
  131. func (c *ContractApi) GetIncome() {
  132. // 1.规则验证
  133. sectionData, err := c.ServiceContract.ValidRuleGet(c.Ctx)
  134. if err != nil {
  135. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  136. return
  137. }
  138. // 2.项目ID
  139. projectId, err := utils.GetProjectId(c.Ctx)
  140. if err != nil {
  141. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  142. return
  143. }
  144. // 3.标段ID
  145. bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId)
  146. if err != nil {
  147. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  148. return
  149. }
  150. // 4.树ID
  151. treeId, err := utils.GetDecryptId(sectionData.Id)
  152. if err != nil {
  153. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  154. return
  155. }
  156. // 获得项目节详情
  157. section := c.ServiceContract.Get(treeId, bidsectionId, projectId, 0)
  158. // 该项目节 子树下是否有合同
  159. isContract := true
  160. contractList := c.ServiceContract.GetSectionTreeContract(section.Attribution, bidsectionId, projectId, 0)
  161. if len(contractList) == 0 {
  162. isContract = false
  163. }
  164. // 获得合同详情
  165. contractId, _ := utils.GetDecryptId(section.ContractId)
  166. contract := &viewmodels.Contracts{}
  167. if contractId != 0 {
  168. contract = c.ServiceContract.GetContract(contractId)
  169. }
  170. c.Ctx.JSON(iris.Map{
  171. "code": 0,
  172. "msg": "",
  173. "section": section,
  174. "isContract": isContract, //该项目节(包含子孙)下是否有合同
  175. "contract": contract,
  176. })
  177. }
  178. // @Summary 新增合同
  179. // @Tags 合同管理-收入合同
  180. // @Description 新增合同
  181. // @Accept json
  182. // @Produce json
  183. // @Security ApiKeyAuth
  184. // @Param treeId path string true "项目节ID"
  185. // @Param bidsectionId path string true "标段ID"
  186. // @Param code path string true "合同编号"
  187. // @Param name path string true "合同名称"
  188. // @Param contractsType path int true "合同类型(1)"
  189. // @Param price path string true "合同金额"
  190. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  191. // @Router /api/contract/income/create [post]
  192. func (c *ContractApi) PostIncomeCreate() {
  193. // 验证参数
  194. contractData, err := c.ServiceContract.ValidRuleContractAdd(c.Ctx)
  195. if err != nil {
  196. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  197. return
  198. }
  199. // 项目ID
  200. projectIdInt, err := utils.GetProjectId(c.Ctx)
  201. if err != nil {
  202. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  203. return
  204. }
  205. // 标段ID
  206. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  207. if err != nil {
  208. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  209. return
  210. }
  211. // 项目节ID
  212. treeId, err := utils.GetDecryptId(contractData.TreeId)
  213. if err != nil {
  214. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  215. return
  216. }
  217. // 可优化-事务问题
  218. // 先添加项目节
  219. sectionData := &viewmodels.TreeSectionContract{}
  220. sectionData.Id = contractData.TreeId
  221. sectionData.Name = " "
  222. sectionResult, err := c.ServiceContract.SectionAdd(sectionData, bidsectionId, projectIdInt, 0)
  223. if err != nil {
  224. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  225. return
  226. }
  227. // 在添加合同
  228. err = c.ServiceContract.Add(contractData, projectIdInt, bidsectionId, sectionResult.Id)
  229. if err != nil {
  230. // 需要删除项目节-有问题
  231. c.ServiceContract.SectionDelete(sectionResult.Id, bidsectionId, projectIdInt, 0)
  232. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  233. return
  234. }
  235. // 获得项目节
  236. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  237. // 合同ID
  238. contractId, _ := utils.GetDecryptId(sectionDetail.ContractId)
  239. contractDetail := c.ServiceContract.GetContract(contractId)
  240. c.Ctx.JSON(iris.Map{
  241. "code": 0,
  242. "msg": "新增成功",
  243. "contract": contractDetail,
  244. })
  245. }
  246. // @Summary 编辑合同
  247. // @Tags 合同管理-收入合同
  248. // @Description 编辑合同
  249. // @Accept json
  250. // @Produce json
  251. // @Security ApiKeyAuth
  252. // @Param id path string true "合同ID"
  253. // @Param treeId path string true "项目节ID"
  254. // @Param bidsectionId path string true "标段ID"
  255. // @Param content path string true "合同内容"
  256. // @Param name path string true "合同名称"
  257. // @Param price path string true "合同金额"
  258. // @Param partyA path string true "甲方"
  259. // @Param partyASigner path string true "甲方签约人"
  260. // @Param partyB path string true "已方"
  261. // @Param partyBSigner path string true "已方签约人"
  262. // @Param signerTime path string true "签约时间"
  263. // @Param remarks path string true "备注"
  264. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  265. // @Router /api/contract/income/update [post]
  266. func (c *ContractApi) PostIncomeUpdate() {
  267. // 验证参数
  268. contractData, err := c.ServiceContract.ValidRuleContractEdi(c.Ctx)
  269. if err != nil {
  270. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  271. return
  272. }
  273. // 项目ID
  274. projectIdInt, err := utils.GetProjectId(c.Ctx)
  275. if err != nil {
  276. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  277. return
  278. }
  279. // 标段ID
  280. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  281. if err != nil {
  282. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  283. return
  284. }
  285. // 项目节ID
  286. treeId, err := utils.GetDecryptId(contractData.TreeId)
  287. if err != nil {
  288. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  289. return
  290. }
  291. err = c.ServiceContract.Update(contractData, projectIdInt, bidsectionId, treeId)
  292. if err != nil {
  293. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  294. return
  295. }
  296. //2.请求当前项目信息
  297. // 1.验证项目节ID
  298. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  299. // 合同ID
  300. contractId, _ := utils.GetDecryptId(sectionDetail.ContractId)
  301. contractDetail := c.ServiceContract.GetContract(contractId)
  302. c.Ctx.JSON(iris.Map{
  303. "code": 0,
  304. "msg": "编辑成功",
  305. "section": sectionDetail,
  306. "contract": contractDetail,
  307. })
  308. }
  309. // @Summary 删除合同
  310. // @Tags 合同管理-收入合同
  311. // @Description 删除合同
  312. // @Accept json
  313. // @Produce json
  314. // @Security ApiKeyAuth
  315. // @Param id path string true "合同ID"
  316. // @Param treeId path string true "项目节ID"
  317. // @Param bidsectionId path string true "标段ID"
  318. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  319. // @Router /api/contract [delete]
  320. func (c *ContractApi) Delete() {
  321. // 验证参数
  322. contractData, err := c.ServiceContract.ValidRuleContractDel(c.Ctx)
  323. if err != nil {
  324. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  325. return
  326. }
  327. // 项目ID
  328. projectIdInt, err := utils.GetProjectId(c.Ctx)
  329. if err != nil {
  330. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  331. return
  332. }
  333. // 标段ID
  334. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  335. if err != nil {
  336. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  337. return
  338. }
  339. // 项目节ID
  340. treeId, err := utils.GetDecryptId(contractData.TreeId)
  341. if err != nil {
  342. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  343. return
  344. }
  345. // 合同ID
  346. id, err := utils.GetDecryptId(contractData.Id)
  347. if err != nil {
  348. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  349. return
  350. }
  351. // 删除合同
  352. err = c.ServiceContract.Delete(projectIdInt, bidsectionId, treeId, id)
  353. if err != nil {
  354. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  355. return
  356. }
  357. //2.请求当前项目信息--需求变更 删除合同要删除项目节,不需要返回项目节信息
  358. // 1.验证项目节ID
  359. // sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  360. c.Ctx.JSON(iris.Map{"code": 0, "msg": "删除成功", "section": ""})
  361. }
  362. // @Summary 关闭合同
  363. // @Tags 合同管理
  364. // @Description 关闭合同
  365. // @Accept json
  366. // @Produce json
  367. // @Security ApiKeyAuth
  368. // @Param id path string true "合同ID"
  369. // @Param treeId path string true "项目节ID"
  370. // @Param bidsectionId path string true "标段ID"
  371. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  372. // @Router /api/contract/close [post]
  373. func (c *ContractApi) PostClose() {
  374. // 验证参数
  375. contractData, err := c.ServiceContract.ValidRuleContractDel(c.Ctx)
  376. if err != nil {
  377. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  378. return
  379. }
  380. // 项目ID
  381. projectIdInt, err := utils.GetProjectId(c.Ctx)
  382. if err != nil {
  383. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  384. return
  385. }
  386. // 标段ID
  387. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  388. if err != nil {
  389. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  390. return
  391. }
  392. // 项目节ID
  393. treeId, err := utils.GetDecryptId(contractData.TreeId)
  394. if err != nil {
  395. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  396. return
  397. }
  398. // 合同ID
  399. id, err := utils.GetDecryptId(contractData.Id)
  400. if err != nil {
  401. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  402. return
  403. }
  404. err = c.ServiceContract.Close(projectIdInt, bidsectionId, treeId, id)
  405. if err != nil {
  406. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  407. return
  408. }
  409. // 1.项目节信息
  410. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  411. c.Ctx.JSON(iris.Map{"code": 0, "msg": "关闭成功", "section": sectionDetail})
  412. }
  413. // @Summary 解锁合同
  414. // @Tags 合同管理
  415. // @Description 解锁合同
  416. // @Accept json
  417. // @Produce json
  418. // @Security ApiKeyAuth
  419. // @Param id path string true "合同ID"
  420. // @Param treeId path string true "项目节ID"
  421. // @Param bidsectionId path string true "标段ID"
  422. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  423. // @Router /api/contract/unlock [post]
  424. func (c *ContractApi) PostUnlock() {
  425. // 验证参数
  426. contractData, err := c.ServiceContract.ValidRuleContractDel(c.Ctx)
  427. if err != nil {
  428. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  429. return
  430. }
  431. // 项目ID
  432. projectIdInt, err := utils.GetProjectId(c.Ctx)
  433. if err != nil {
  434. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  435. return
  436. }
  437. // 标段ID
  438. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  439. if err != nil {
  440. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  441. return
  442. }
  443. // 项目节ID
  444. treeId, err := utils.GetDecryptId(contractData.TreeId)
  445. if err != nil {
  446. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  447. return
  448. }
  449. // 合同ID
  450. id, err := utils.GetDecryptId(contractData.Id)
  451. if err != nil {
  452. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  453. return
  454. }
  455. err = c.ServiceContract.Unlock(projectIdInt, bidsectionId, treeId, id)
  456. if err != nil {
  457. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  458. return
  459. }
  460. // 1.项目节信息
  461. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  462. c.Ctx.JSON(iris.Map{"code": 0, "msg": "解锁成功", "section": sectionDetail})
  463. }
  464. // @Summary 合同概述
  465. // @Tags 合同管理
  466. // @Description 合同概述
  467. // @Accept json
  468. // @Produce json
  469. // @Security ApiKeyAuth
  470. // @Param bidsectionId path string true "标段ID"
  471. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  472. // @Router /api/contract/survey [get]
  473. func (c *ContractApi) GetSurvey() {
  474. sectionData := viewmodels.Contracts{}
  475. err := c.Ctx.ReadForm(&sectionData)
  476. if err != nil {
  477. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  478. return
  479. }
  480. err = sectionData.ValidateBidsectionId()
  481. if err != nil {
  482. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  483. return
  484. }
  485. // 标段ID
  486. bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId)
  487. if err != nil {
  488. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  489. return
  490. }
  491. // 项目ID
  492. projectId, err := utils.GetProjectId(c.Ctx)
  493. if err != nil {
  494. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  495. return
  496. }
  497. // 账号ID
  498. accountId, err := utils.GetProjectAccountId(c.Ctx)
  499. if err != nil {
  500. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  501. return
  502. }
  503. // 缓存标段ID-用于权限
  504. key := fmt.Sprintf("pm_%d_%d", projectId, accountId)
  505. lib.NewRedis().SetBidsectionIdByCache(key, bidsectionId)
  506. incomeData := c.ServiceContract.GetSurvey(bidsectionId, projectId, 1)
  507. expenditureData := c.ServiceContract.GetSurvey(bidsectionId, projectId, 2)
  508. data := map[string]interface{}{
  509. "incomeData": incomeData,
  510. "expenditureData": expenditureData,
  511. }
  512. c.Ctx.JSON(iris.Map{
  513. "code": 0,
  514. "msg": "",
  515. "data": data,
  516. })
  517. }