contract_api.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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 sectionName path string true "项目节名称"
  189. // @Param contractsType path int true "合同类型(1)"
  190. // @Param price path string true "合同金额"
  191. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  192. // @Router /api/contract/income/create [post]
  193. func (c *ContractApi) PostIncomeCreate() {
  194. // 验证参数
  195. contractData, err := c.ServiceContract.ValidRuleContractAdd(c.Ctx)
  196. if err != nil {
  197. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  198. return
  199. }
  200. // 项目ID
  201. projectIdInt, err := utils.GetProjectId(c.Ctx)
  202. if err != nil {
  203. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  204. return
  205. }
  206. // 标段ID
  207. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  208. if err != nil {
  209. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  210. return
  211. }
  212. // 项目节ID
  213. treeId, err := utils.GetDecryptId(contractData.TreeId)
  214. if err != nil {
  215. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  216. return
  217. }
  218. sectionData := &viewmodels.TreeSectionContract{}
  219. sectionData.Id = contractData.TreeId
  220. sectionData.Name = contractData.Name
  221. // c.ServiceContract.SectionAdd(sectionData, bidsectionId, projectIdInt, 0)
  222. err = c.ServiceContract.Add(contractData, projectIdInt, bidsectionId, treeId)
  223. if err != nil {
  224. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  225. return
  226. }
  227. // 获得项目节
  228. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  229. // 合同ID
  230. contractId, _ := utils.GetDecryptId(sectionDetail.ContractId)
  231. contractDetail := c.ServiceContract.GetContract(contractId)
  232. c.Ctx.JSON(iris.Map{
  233. "code": 0,
  234. "msg": "新增成功",
  235. "contract": contractDetail,
  236. })
  237. }
  238. // @Summary 编辑合同
  239. // @Tags 合同管理-收入合同
  240. // @Description 编辑合同
  241. // @Accept json
  242. // @Produce json
  243. // @Security ApiKeyAuth
  244. // @Param id path string true "合同ID"
  245. // @Param treeId path string true "项目节ID"
  246. // @Param bidsectionId path string true "标段ID"
  247. // @Param content path string true "合同内容"
  248. // @Param name path string true "合同名称"
  249. // @Param price path string true "合同金额"
  250. // @Param partyA path string true "甲方"
  251. // @Param partyASigner path string true "甲方签约人"
  252. // @Param partyB path string true "已方"
  253. // @Param partyBSigner path string true "已方签约人"
  254. // @Param signerTime path string true "签约时间"
  255. // @Param remarks path string true "备注"
  256. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  257. // @Router /api/contract/income/update [post]
  258. func (c *ContractApi) PostIncomeUpdate() {
  259. // 验证参数
  260. contractData, err := c.ServiceContract.ValidRuleContractEdi(c.Ctx)
  261. if err != nil {
  262. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  263. return
  264. }
  265. // 项目ID
  266. projectIdInt, err := utils.GetProjectId(c.Ctx)
  267. if err != nil {
  268. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  269. return
  270. }
  271. // 标段ID
  272. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  273. if err != nil {
  274. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  275. return
  276. }
  277. // 项目节ID
  278. treeId, err := utils.GetDecryptId(contractData.TreeId)
  279. if err != nil {
  280. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  281. return
  282. }
  283. err = c.ServiceContract.Update(contractData, projectIdInt, bidsectionId, treeId)
  284. if err != nil {
  285. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  286. return
  287. }
  288. //2.请求当前项目信息
  289. // 1.验证项目节ID
  290. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  291. // 合同ID
  292. contractId, _ := utils.GetDecryptId(sectionDetail.ContractId)
  293. contractDetail := c.ServiceContract.GetContract(contractId)
  294. c.Ctx.JSON(iris.Map{
  295. "code": 0,
  296. "msg": "编辑成功",
  297. "section": sectionDetail,
  298. "contract": contractDetail,
  299. })
  300. }
  301. // @Summary 删除合同
  302. // @Tags 合同管理-收入合同
  303. // @Description 删除合同
  304. // @Accept json
  305. // @Produce json
  306. // @Security ApiKeyAuth
  307. // @Param id path string true "合同ID"
  308. // @Param treeId path string true "项目节ID"
  309. // @Param bidsectionId path string true "标段ID"
  310. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  311. // @Router /api/contract [delete]
  312. func (c *ContractApi) Delete() {
  313. // 验证参数
  314. contractData, err := c.ServiceContract.ValidRuleContractDel(c.Ctx)
  315. if err != nil {
  316. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  317. return
  318. }
  319. // 项目ID
  320. projectIdInt, err := utils.GetProjectId(c.Ctx)
  321. if err != nil {
  322. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  323. return
  324. }
  325. // 标段ID
  326. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  327. if err != nil {
  328. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  329. return
  330. }
  331. // 项目节ID
  332. treeId, err := utils.GetDecryptId(contractData.TreeId)
  333. if err != nil {
  334. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  335. return
  336. }
  337. // 合同ID
  338. id, err := utils.GetDecryptId(contractData.Id)
  339. if err != nil {
  340. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  341. return
  342. }
  343. err = c.ServiceContract.Delete(projectIdInt, bidsectionId, treeId, id)
  344. if err != nil {
  345. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  346. return
  347. }
  348. //2.请求当前项目信息
  349. // 1.验证项目节ID
  350. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  351. c.Ctx.JSON(iris.Map{"code": 0, "msg": "删除成功", "section": sectionDetail})
  352. }
  353. // @Summary 关闭合同
  354. // @Tags 合同管理
  355. // @Description 关闭合同
  356. // @Accept json
  357. // @Produce json
  358. // @Security ApiKeyAuth
  359. // @Param id path string true "合同ID"
  360. // @Param treeId path string true "项目节ID"
  361. // @Param bidsectionId path string true "标段ID"
  362. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  363. // @Router /api/contract/close [post]
  364. func (c *ContractApi) PostClose() {
  365. // 验证参数
  366. contractData, err := c.ServiceContract.ValidRuleContractDel(c.Ctx)
  367. if err != nil {
  368. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  369. return
  370. }
  371. // 项目ID
  372. projectIdInt, err := utils.GetProjectId(c.Ctx)
  373. if err != nil {
  374. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  375. return
  376. }
  377. // 标段ID
  378. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  379. if err != nil {
  380. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  381. return
  382. }
  383. // 项目节ID
  384. treeId, err := utils.GetDecryptId(contractData.TreeId)
  385. if err != nil {
  386. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  387. return
  388. }
  389. // 合同ID
  390. id, err := utils.GetDecryptId(contractData.Id)
  391. if err != nil {
  392. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  393. return
  394. }
  395. err = c.ServiceContract.Close(projectIdInt, bidsectionId, treeId, id)
  396. if err != nil {
  397. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  398. return
  399. }
  400. // 1.项目节信息
  401. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  402. c.Ctx.JSON(iris.Map{"code": 0, "msg": "关闭成功", "section": sectionDetail})
  403. }
  404. // @Summary 解锁合同
  405. // @Tags 合同管理
  406. // @Description 解锁合同
  407. // @Accept json
  408. // @Produce json
  409. // @Security ApiKeyAuth
  410. // @Param id path string true "合同ID"
  411. // @Param treeId path string true "项目节ID"
  412. // @Param bidsectionId path string true "标段ID"
  413. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  414. // @Router /api/contract/unlock [post]
  415. func (c *ContractApi) PostUnlock() {
  416. // 验证参数
  417. contractData, err := c.ServiceContract.ValidRuleContractDel(c.Ctx)
  418. if err != nil {
  419. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  420. return
  421. }
  422. // 项目ID
  423. projectIdInt, err := utils.GetProjectId(c.Ctx)
  424. if err != nil {
  425. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  426. return
  427. }
  428. // 标段ID
  429. bidsectionId, err := utils.GetDecryptId(contractData.BidsectionId)
  430. if err != nil {
  431. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  432. return
  433. }
  434. // 项目节ID
  435. treeId, err := utils.GetDecryptId(contractData.TreeId)
  436. if err != nil {
  437. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  438. return
  439. }
  440. // 合同ID
  441. id, err := utils.GetDecryptId(contractData.Id)
  442. if err != nil {
  443. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  444. return
  445. }
  446. err = c.ServiceContract.Unlock(projectIdInt, bidsectionId, treeId, id)
  447. if err != nil {
  448. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  449. return
  450. }
  451. // 1.项目节信息
  452. sectionDetail := c.ServiceContract.Get(treeId, bidsectionId, projectIdInt, 0)
  453. c.Ctx.JSON(iris.Map{"code": 0, "msg": "解锁成功", "section": sectionDetail})
  454. }
  455. // @Summary 合同概述
  456. // @Tags 合同管理
  457. // @Description 合同概述
  458. // @Accept json
  459. // @Produce json
  460. // @Security ApiKeyAuth
  461. // @Param bidsectionId path string true "标段ID"
  462. // @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
  463. // @Router /api/contract/survey [get]
  464. func (c *ContractApi) GetSurvey() {
  465. sectionData := viewmodels.Contracts{}
  466. err := c.Ctx.ReadForm(&sectionData)
  467. if err != nil {
  468. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  469. return
  470. }
  471. err = sectionData.ValidateBidsectionId()
  472. if err != nil {
  473. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  474. return
  475. }
  476. // 标段ID
  477. bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId)
  478. if err != nil {
  479. c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
  480. return
  481. }
  482. // 项目ID
  483. projectId, err := utils.GetProjectId(c.Ctx)
  484. if err != nil {
  485. c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
  486. return
  487. }
  488. // 账号ID
  489. accountId, err := utils.GetProjectAccountId(c.Ctx)
  490. if err != nil {
  491. c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
  492. return
  493. }
  494. // 缓存标段ID-用于权限
  495. key := fmt.Sprintf("pm_%d_%d", projectId, accountId)
  496. lib.NewRedis().SetBidsectionIdByCache(key, bidsectionId)
  497. incomeData := c.ServiceContract.GetSurvey(bidsectionId, projectId, 1)
  498. expenditureData := c.ServiceContract.GetSurvey(bidsectionId, projectId, 2)
  499. data := map[string]interface{}{
  500. "incomeData": incomeData,
  501. "expenditureData": expenditureData,
  502. }
  503. c.Ctx.JSON(iris.Map{
  504. "code": 0,
  505. "msg": "",
  506. "data": data,
  507. })
  508. }