contract_api.go 16 KB

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