contract_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * @description: 合同数据相关操作
  3. * @Author: CP
  4. * @Date: 2020-10-27 11:28:24
  5. * @FilePath: \construction_management\services\contract_service.go
  6. */
  7. package services
  8. import (
  9. "errors"
  10. "fmt"
  11. "html"
  12. "log"
  13. "strconv"
  14. "time"
  15. "github.com/kataras/iris/v12"
  16. "github.com/shopspring/decimal"
  17. "go.mod/comm"
  18. "go.mod/conf"
  19. "go.mod/dao"
  20. "go.mod/datasource"
  21. "go.mod/models"
  22. "go.mod/web/viewmodels"
  23. )
  24. //定义项目用户Service接口
  25. type ContractService interface {
  26. ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  27. ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  28. ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  29. ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  30. ValidRuleSectionNot(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  31. ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  32. ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  33. ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error)
  34. ValidRuleContractEdi(ctx iris.Context) (*viewmodels.Contracts, error)
  35. ValidRuleContractDel(ctx iris.Context) (*viewmodels.Contracts, error)
  36. ValidRuleContractClose(ctx iris.Context) (*viewmodels.Contracts, error)
  37. ValidRuleContractRetrunAdd(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  38. ValidRuleContractRetrun(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  39. ValidRuleContractRetrunDel(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  40. ValidRuleContractPaidAdd(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  41. ValidRuleContractPaid(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  42. ValidRuleContractPaidDel(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  43. // 项目节
  44. Get(treeId int, bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  45. GetSectionTreeContract(attribution string, bidsectionId int, projectId int, treeType int) []*viewmodels.Contracts
  46. GetSecionTree(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  47. SetSection(templateNumber int, bidsectionId int, projectIdInt int, treeType int) error
  48. SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error)
  49. ContractSectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error)
  50. SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  51. UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  52. SectionDelete(treeId int, bidsectionId int, projectId int, treeType int) error
  53. MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  54. MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  55. GetSecionTreeNotContract(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  56. GetContract(contractId int) *viewmodels.Contracts
  57. Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  58. Update(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  59. Delete(projectId int, bidsectionId int, treeId int, id int) error
  60. Close(projectId int, bidsectionId int, treeId int, id int, treeType int) error
  61. Unlock(projectId int, bidsectionId int, treeId int, id int, treeType int) error
  62. GetSurvey(bidsectionId int, projectId int, contractsType int) map[string]interface{}
  63. ReturnCreate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, projectAccountId int) error
  64. ReturnUpdate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, id int) error
  65. ReturnAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsReturn
  66. ReturnDelete(projectId int, bidsectionId int, contractsId int, id int) error
  67. //支出合同
  68. AddExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  69. UpdateExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  70. DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error
  71. // 已支付
  72. PaidAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsPaid
  73. PaidCreate(returnData *viewmodels.ContractsPaid, projectId int, bidsectionId int, contractsId int, projectAccountId int) error
  74. PaidUpdate(returnData *viewmodels.ContractsPaid, projectId int, bidsectionId int, contractsId int, id int) error
  75. PaidDelete(projectId int, bidsectionId int, contractsId int, id int) error
  76. }
  77. //返回service操作类
  78. type contractService struct {
  79. treeContractDao *dao.TreeContractDao
  80. contractDao *dao.ContractDao
  81. contractReturnDao *dao.ContractReturnDao
  82. contractPaidDao *dao.ContractPaidDao
  83. treeDao *dao.TreeDao
  84. annexDao *dao.AnnexDao
  85. }
  86. //创建项目用户service
  87. func NewContractService() ContractService {
  88. return &contractService{
  89. treeContractDao: dao.NewTreeContractDao(datasource.InstanceDbMaster()),
  90. contractDao: dao.NewContractDao(datasource.InstanceDbMaster()),
  91. contractReturnDao: dao.NewContractReturnDao(datasource.InstanceDbMaster()),
  92. contractPaidDao: dao.NewContractPaidDao(datasource.InstanceDbMaster()),
  93. treeDao: dao.NewTreeDao(datasource.InstanceDbMaster()),
  94. annexDao: dao.NewAnnexDao(datasource.InstanceDbMaster()),
  95. }
  96. }
  97. // 升级降级规则验证
  98. func (s *contractService) ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  99. treeSectionVaild := &viewmodels.TreeSectionContract{}
  100. err := ctx.ReadJSON(treeSectionVaild)
  101. if err != nil {
  102. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  103. return treeSectionVaild, err
  104. }
  105. err = treeSectionVaild.ValidateDepth()
  106. if err != nil {
  107. log.Println("参数验证错误, error=", err)
  108. return treeSectionVaild, err
  109. }
  110. return treeSectionVaild, nil
  111. }
  112. // 模板规则验证
  113. func (s *contractService) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  114. treeSectionVaild := &viewmodels.TreeSectionContract{}
  115. err := ctx.ReadJSON(treeSectionVaild)
  116. if err != nil {
  117. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  118. return treeSectionVaild, err
  119. }
  120. err = treeSectionVaild.ValidateTemplate()
  121. if err != nil {
  122. log.Println("参数验证错误, error=", err)
  123. return treeSectionVaild, err
  124. }
  125. return treeSectionVaild, nil
  126. }
  127. // 模板规则新增项目节
  128. func (s *contractService) ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  129. treeSectionVaild := &viewmodels.TreeSectionContract{}
  130. err := ctx.ReadJSON(treeSectionVaild)
  131. if err != nil {
  132. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  133. return treeSectionVaild, err
  134. }
  135. err = treeSectionVaild.ValidateSectionAdd()
  136. if err != nil {
  137. log.Println("参数验证错误, error=", err)
  138. return treeSectionVaild, err
  139. }
  140. return treeSectionVaild, nil
  141. }
  142. // 模板规则新增项目节
  143. func (s *contractService) ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  144. treeSectionVaild := &viewmodels.TreeSectionContract{}
  145. err := ctx.ReadForm(treeSectionVaild)
  146. if err != nil {
  147. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  148. return treeSectionVaild, err
  149. }
  150. err = treeSectionVaild.ValidateSectionDelete()
  151. if err != nil {
  152. log.Println("参数验证错误, error=", err)
  153. return treeSectionVaild, err
  154. }
  155. return treeSectionVaild, nil
  156. }
  157. func (s *contractService) ValidRuleSectionNot(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  158. treeSectionVaild := &viewmodels.TreeSectionContract{}
  159. err := ctx.ReadForm(treeSectionVaild)
  160. if err != nil {
  161. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  162. return treeSectionVaild, err
  163. }
  164. err = treeSectionVaild.ValidateSectionNot()
  165. if err != nil {
  166. log.Println("参数验证错误, error=", err)
  167. return treeSectionVaild, err
  168. }
  169. return treeSectionVaild, nil
  170. }
  171. func (s *contractService) ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  172. treeSectionVaild := &viewmodels.TreeSectionContract{}
  173. err := ctx.ReadForm(treeSectionVaild)
  174. if err != nil {
  175. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  176. return treeSectionVaild, err
  177. }
  178. err = treeSectionVaild.ValidateSectionDelete()
  179. if err != nil {
  180. log.Println("参数验证错误, error=", err)
  181. return treeSectionVaild, err
  182. }
  183. return treeSectionVaild, nil
  184. }
  185. // 验证序号相关
  186. func (s *contractService) ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  187. treeSectionVaild := &viewmodels.TreeSectionContract{}
  188. err := ctx.ReadJSON(treeSectionVaild)
  189. if err != nil {
  190. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  191. return treeSectionVaild, err
  192. }
  193. err = treeSectionVaild.ValidateSectionSerial()
  194. if err != nil {
  195. log.Println("参数验证错误, error=", err)
  196. return treeSectionVaild, err
  197. }
  198. return treeSectionVaild, nil
  199. }
  200. // 新增合同参数验证
  201. func (s *contractService) ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error) {
  202. // 创建一个存放前端传过来参数
  203. contractsVaild := &viewmodels.Contracts{}
  204. // 存放raw的值,放入到contractsVaild
  205. err := ctx.ReadJSON(contractsVaild)
  206. if err != nil {
  207. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  208. return contractsVaild, err
  209. }
  210. // 验证合同传参
  211. err = contractsVaild.ValidateAdd()
  212. if err != nil {
  213. log.Println("参数验证错误, error=", err)
  214. return contractsVaild, err
  215. }
  216. // xss
  217. contractsVaild.Code = html.EscapeString(contractsVaild.Code)
  218. contractsVaild.Name = html.EscapeString(contractsVaild.Name)
  219. // contractsVaild.Price = html.EscapeString(contractsVaild.Price)
  220. return contractsVaild, nil
  221. }
  222. // 校验编辑接口
  223. func (s *contractService) ValidRuleContractEdi(ctx iris.Context) (*viewmodels.Contracts, error) {
  224. // 创建一个存放前端传过来参数
  225. contractsVaild := &viewmodels.Contracts{}
  226. // 存放raw的值,放入到contractsVaild
  227. err := ctx.ReadJSON(contractsVaild)
  228. if err != nil {
  229. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  230. return contractsVaild, err
  231. }
  232. // 验证合同传参
  233. err = contractsVaild.ValidateEdi()
  234. if err != nil {
  235. log.Println("参数验证错误, error=", err)
  236. return contractsVaild, err
  237. }
  238. contractsVaild.Content = html.EscapeString(contractsVaild.Content)
  239. contractsVaild.Name = html.EscapeString(contractsVaild.Name)
  240. // contractsVaild.Price = html.EscapeString(contractsVaild.Price)
  241. contractsVaild.PartyA = html.EscapeString(contractsVaild.PartyA)
  242. contractsVaild.PartyASigner = html.EscapeString(contractsVaild.PartyASigner)
  243. contractsVaild.PartyB = html.EscapeString(contractsVaild.PartyB)
  244. contractsVaild.PartyBSigner = html.EscapeString(contractsVaild.PartyBSigner)
  245. return contractsVaild, nil
  246. }
  247. // 校验删除合同参数
  248. func (s *contractService) ValidRuleContractDel(ctx iris.Context) (*viewmodels.Contracts, error) {
  249. // 创建一个存放前端传过来参数
  250. contractsVaild := &viewmodels.Contracts{}
  251. // 存放raw的值,放入到contractsVaild
  252. err := ctx.ReadForm(contractsVaild)
  253. if err != nil {
  254. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  255. return contractsVaild, err
  256. }
  257. // 验证合同传参
  258. err = contractsVaild.ValidateDel()
  259. if err != nil {
  260. log.Println("参数验证错误, error=", err)
  261. return contractsVaild, err
  262. }
  263. return contractsVaild, nil
  264. }
  265. // 校验关闭合同参数
  266. func (s *contractService) ValidRuleContractClose(ctx iris.Context) (*viewmodels.Contracts, error) {
  267. // 创建一个存放前端传过来参数
  268. contractsVaild := &viewmodels.Contracts{}
  269. // 存放raw的值,放入到contractsVaild
  270. err := ctx.ReadJSON(contractsVaild)
  271. if err != nil {
  272. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  273. return contractsVaild, err
  274. }
  275. // 验证合同传参
  276. err = contractsVaild.ValidateDel()
  277. if err != nil {
  278. log.Println("参数验证错误, error=", err)
  279. return contractsVaild, err
  280. }
  281. return contractsVaild, nil
  282. }
  283. //------------------------------------------------------------
  284. // 获得项目节
  285. func (s *contractService) Get(treeId int, bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract {
  286. // 1.获得项目节
  287. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  288. // 2.构造数据
  289. sectionVM := s.makeSectionView(section)
  290. // 3.更新 上移和下一的限制
  291. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  292. if len(youngerBrotherList) == 0 {
  293. sectionVM.IsEnd = true
  294. }
  295. sectionVM.ElderBrother = true
  296. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  297. if len(elderBrotherList) == 0 {
  298. sectionVM.ElderBrother = false
  299. }
  300. return sectionVM
  301. }
  302. // 获得合同详情
  303. func (s *contractService) GetContract(contractId int) *viewmodels.Contracts {
  304. contract := s.contractDao.Get(contractId)
  305. contractsVM := s.makeContractVM(contract)
  306. return &contractsVM
  307. }
  308. // 新增合同
  309. func (s *contractService) Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
  310. // 1. 项目节存在
  311. contracts := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  312. if contracts.Id == 0 {
  313. return errors.New("未找到项目节")
  314. }
  315. // 2.项目节是没有合同
  316. if contracts.ContractId != 0 {
  317. return errors.New("该项目节上已经存在合同")
  318. }
  319. // 2-1.查找合同编号是否存在
  320. codeData := s.contractDao.GetByCode(projectId, bidsectionId, contractData.Code, 1)
  321. if len(codeData) != 0 {
  322. return errors.New("该合同编号已经存在")
  323. }
  324. // 3.新增合同 --合计标段上的金额
  325. contractsCm := &models.CmContracts{}
  326. contractsCm.Code = contractData.Code
  327. contractsCm.Name = contractData.Name
  328. contractsCm.ContractsType = 1
  329. contractsCm.Price = strconv.FormatFloat(contractData.Price, 'f', 1, 64)
  330. // contractsCm.Price = contractData.Price
  331. contractsCm.Returned = "0"
  332. contractsCm.Paid = "0"
  333. contractsCm.TreeId = treeId
  334. contractsCm.ProjectId = projectId
  335. contractsCm.BidsectionId = bidsectionId
  336. contractsCm.Status = 0
  337. contractsCm.CreateTime = time.Now()
  338. contractsCm.UpdateTime = time.Now()
  339. err := s.contractDao.Add(contractsCm)
  340. if err != nil {
  341. return err
  342. }
  343. // 3.获得该标段下合同总数 - 总收入金额
  344. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  345. // 更新标段目录上合同金额和总数
  346. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  347. if err != nil {
  348. return err
  349. }
  350. return nil
  351. }
  352. // 更新合同
  353. func (s *contractService) Update(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
  354. // 1. 项目节存在
  355. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  356. if contractsTree.Id == 0 {
  357. return errors.New("未找到项目节")
  358. }
  359. // 2.项目节是没有合同
  360. if contractsTree.ContractId == 0 {
  361. return errors.New("该项目节上没有找到合同")
  362. }
  363. // 3.合同锁定 不能删除
  364. if contractsTree.ContractLocking == 1 {
  365. return errors.New("该合同已锁定")
  366. }
  367. // 4.合同金额不能小于回款金额
  368. returnedPrice, _ := decimal.NewFromString(contractsTree.ContractReturned)
  369. price := decimal.NewFromFloat(contractData.Price)
  370. // price, err := decimal.NewFromString(contractData.Price)
  371. // if err != nil {
  372. // return errors.New("金额填写有误")
  373. // }
  374. if returnedPrice.GreaterThan(price) {
  375. return errors.New(fmt.Sprintf("合同金额不能低于%s", contractsTree.ContractReturned))
  376. }
  377. contractsCm := &models.CmContracts{}
  378. contractsCm.Id = contractsTree.ContractId
  379. contractsCm.Content = contractData.Content
  380. contractsCm.Name = contractData.Name
  381. contractsCm.Price = strconv.FormatFloat(contractData.Price, 'f', 1, 64)
  382. // contractsCm.Price = contractData.Price
  383. contractsCm.PartyA = contractData.PartyA
  384. contractsCm.PartyASigner = contractData.PartyASigner
  385. contractsCm.PartyB = contractData.PartyB
  386. contractsCm.PartyBSigner = contractData.PartyBSigner
  387. loc, _ := time.LoadLocation("Local")
  388. SignerTime, err := time.ParseInLocation(conf.SysTimeform, contractData.SignerTime, loc)
  389. if err != nil {
  390. return errors.New("签约时间填写异常")
  391. }
  392. contractsCm.SignerTime = SignerTime
  393. contractsCm.Remarks = contractData.Remarks
  394. columns := []string{"Content", "Name", "Price", "PartyA", "PartyASigner", "PartyB", "PartyBSigner"}
  395. err = s.contractDao.Update(contractsCm, columns, projectId, bidsectionId, treeId)
  396. if err != nil {
  397. return err
  398. }
  399. // 3.获得该标段下合同总数 - 总收入金额
  400. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  401. // 更新标段目录上合同金额和总数
  402. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  403. if err != nil {
  404. return err
  405. }
  406. return nil
  407. }
  408. // 删除合同
  409. func (s *contractService) Delete(projectId int, bidsectionId int, treeId int, id int) error {
  410. // 1. 项目节存在
  411. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  412. if contractsTree.Id == 0 {
  413. return errors.New("未找到项目节")
  414. }
  415. // 2.项目节是没有合同
  416. if contractsTree.ContractId == 0 {
  417. return errors.New("该项目节上没有找到合同")
  418. }
  419. // 3.合同锁定 不能删除
  420. if contractsTree.ContractLocking == 1 {
  421. return errors.New("该合同已锁定")
  422. }
  423. // 删除合同
  424. err := s.contractDao.Delete(projectId, bidsectionId, treeId, id)
  425. if err != nil {
  426. return err
  427. }
  428. // 3.获得该标段下合同总数 - 总收入金额
  429. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  430. // 更新标段目录上合同金额和总数
  431. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  432. if err != nil {
  433. return err
  434. }
  435. // 4.更新回款总金额
  436. err = s.contractReturnDao.UpdateTotalPrice(projectId, bidsectionId, id)
  437. if err != nil {
  438. return err
  439. }
  440. return nil
  441. }
  442. // 关闭合同
  443. func (s *contractService) Close(projectId int, bidsectionId int, treeId int, id int, treeType int) error {
  444. // 1. 项目节存在
  445. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  446. if contractsTree.Id == 0 {
  447. return errors.New("未找到项目节")
  448. }
  449. // 2.项目节是没有合同
  450. if contractsTree.ContractId == 0 {
  451. return errors.New("该项目节上没有找到合同")
  452. }
  453. // 3.合同锁定 不能删除
  454. if contractsTree.ContractLocking == 1 {
  455. return errors.New("该合同已锁定")
  456. }
  457. // 关闭合同
  458. err := s.contractDao.Close(projectId, bidsectionId, treeId, id)
  459. if err != nil {
  460. return err
  461. }
  462. return nil
  463. }
  464. // 解锁合同
  465. func (s *contractService) Unlock(projectId int, bidsectionId int, treeId int, id int, treeType int) error {
  466. // 1. 项目节存在
  467. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  468. if contractsTree.Id == 0 {
  469. return errors.New("未找到项目节")
  470. }
  471. // 2.项目节是没有合同
  472. if contractsTree.ContractId == 0 {
  473. return errors.New("该项目节上没有找到合同")
  474. }
  475. // 解锁合同
  476. err := s.contractDao.Unlock(projectId, bidsectionId, treeId, id)
  477. if err != nil {
  478. return err
  479. }
  480. return nil
  481. }
  482. //获得合同收入概况
  483. func (s *contractService) GetSurvey(bidsectionId int, projectId int, contractsType int) map[string]interface{} {
  484. // 1.获得收入合同
  485. year := time.Now().Year()
  486. incomeList := s.contractDao.GetTypeYear(bidsectionId, projectId, contractsType, year)
  487. // 2.初始化
  488. totalContractPrice := 0.00
  489. totalTypePrice := 0.00
  490. performNumber := 0
  491. closeNumber := 0
  492. uncloseNumber := 0
  493. // 3.当年数据初始化
  494. returnDate := map[string]float64{
  495. fmt.Sprintf("%d-01", year): 0.00,
  496. fmt.Sprintf("%d-02", year): 0.00,
  497. fmt.Sprintf("%d-03", year): 0.00,
  498. fmt.Sprintf("%d-04", year): 0.00,
  499. fmt.Sprintf("%d-05", year): 0.00,
  500. fmt.Sprintf("%d-06", year): 0.00,
  501. fmt.Sprintf("%d-07", year): 0.00,
  502. fmt.Sprintf("%d-08", year): 0.00,
  503. fmt.Sprintf("%d-09", year): 0.00,
  504. fmt.Sprintf("%d-10", year): 0.00,
  505. fmt.Sprintf("%d-11", year): 0.00,
  506. fmt.Sprintf("%d-12", year): 0.00,
  507. }
  508. contractIds := []int{}
  509. for _, item := range incomeList {
  510. contractPrice, _ := strconv.ParseFloat(item.Price, 64)
  511. totalContractPrice = totalContractPrice + contractPrice
  512. // 回款金额或已支付金额合计
  513. contractIds = append(contractIds, item.Id)
  514. // typePrice := 0.00
  515. // if contractsType == 1 {
  516. // typePrice, _ = strconv.ParseFloat(item.Returned, 64)
  517. // totalTypePrice = totalTypePrice + typePrice
  518. // } else {
  519. // typePrice, _ = strconv.ParseFloat(item.Paid, 64)
  520. // totalTypePrice = totalTypePrice + typePrice
  521. // }
  522. if item.Status == 0 {
  523. performNumber = performNumber + 1
  524. } else if item.Status == 1 {
  525. uncloseNumber = uncloseNumber + 1
  526. } else if item.Status == 2 {
  527. closeNumber = closeNumber + 1
  528. }
  529. // returnDate[item.CreateTime.Format(conf.SysTimeformMonth)] = returnDate[item.CreateTime.Format(conf.SysTimeformMonth)] + typePrice
  530. }
  531. fmt.Println(contractIds)
  532. // contractInId := strings.Join(contractIds, ",")
  533. if contractsType == 1 {
  534. list := s.contractReturnDao.GetInContractsIds(contractIds)
  535. for _, item := range list {
  536. typePrice := 0.00
  537. typePrice, _ = strconv.ParseFloat(item.Price, 64)
  538. totalTypePrice = totalTypePrice + typePrice
  539. fmt.Println(item)
  540. // :=decimal.NewFromFloat(typePrice)
  541. returnDate[item.Time.Format(conf.SysTimeformMonth)], _ = decimal.NewFromFloat(returnDate[item.Time.Format(conf.SysTimeformMonth)]).Add(decimal.NewFromFloat(typePrice)).Float64()
  542. }
  543. } else {
  544. list := s.contractPaidDao.GetInContractsIds(contractIds)
  545. for _, item := range list {
  546. typePrice := 0.00
  547. typePrice, _ = strconv.ParseFloat(item.Price, 64)
  548. totalTypePrice = totalTypePrice + typePrice
  549. returnDate[item.Time.Format(conf.SysTimeformMonth)], _ = decimal.NewFromFloat(returnDate[item.Time.Format(conf.SysTimeformMonth)]).Add(decimal.NewFromFloat(typePrice)).Float64()
  550. }
  551. }
  552. totalContractPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalContractPrice), 64)
  553. totalTypePrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalTypePrice), 64)
  554. // ac := accounting.Accounting{Symbol: "", Precision: 2}
  555. surveryData := map[string]interface{}{
  556. "totalContractPrice": totalContractPrice,
  557. "totalContractPriceShow": totalContractPrice,
  558. // "totalReturnPrice": ac.FormatMoney(totalReturnPrice),
  559. "performNumber": performNumber,
  560. "closeNumber": closeNumber,
  561. "uncloseNumber": uncloseNumber,
  562. "returnDate": returnDate,
  563. }
  564. if contractsType == 1 {
  565. surveryData["totalReturnPriceShow"] = totalTypePrice
  566. } else {
  567. surveryData["totalPaidPriceShow"] = totalTypePrice
  568. }
  569. return surveryData
  570. }
  571. // 获得合同总数量和总金额
  572. func (s *contractService) getContractTotalAndPrice(bidsectionId int, projectId int, treeType int) (contractTotal int, priceTotal float64) {
  573. contractListAll := s.treeContractDao.GetContractAll(bidsectionId, projectId)
  574. // 获得收入合同
  575. contractList := s.treeContractDao.GetContract(bidsectionId, projectId, treeType)
  576. priceTotal = 0.00
  577. for _, item := range contractList {
  578. contractPrice, _ := strconv.ParseFloat(item.ContractPrice, 64)
  579. priceTotal = priceTotal + contractPrice
  580. }
  581. // 合同总数
  582. // contractTotal = len(contractList) + 1
  583. contractTotal = len(contractListAll)
  584. // 合同收入总金额
  585. // price, _ := strconv.ParseFloat(priceString, 64)
  586. // priceTotal = priceTotal + price
  587. // 保留2位小数
  588. priceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", priceTotal), 64)
  589. return contractTotal, priceTotal
  590. }
  591. // 构建合同详情视图
  592. func (s *contractService) makeContractVM(contract *models.CmContracts) viewmodels.Contracts {
  593. contractsVM := viewmodels.Contracts{}
  594. if contract.Id == 0 {
  595. return contractsVM
  596. }
  597. id, _ := comm.AesEncrypt(strconv.Itoa(contract.Id), conf.SignSecret)
  598. treeId, _ := comm.AesEncrypt(strconv.Itoa(contract.TreeId), conf.SignSecret)
  599. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(contract.BidsectionId), conf.SignSecret)
  600. contractsVM.Id = id
  601. contractsVM.TreeId = treeId
  602. contractsVM.ContractsType = contract.ContractsType
  603. contractsVM.BidsectionId = bidsectionId
  604. contractsVM.Name = contract.Name
  605. contractsVM.Content = contract.Content
  606. contractsVM.Code = contract.Code
  607. contractsVM.PartyA = contract.PartyA
  608. contractsVM.PartyASigner = contract.PartyASigner
  609. contractsVM.PartyB = contract.PartyB
  610. contractsVM.PartyBSigner = contract.PartyBSigner
  611. contractsVM.Remarks = contract.Remarks
  612. // price, _ := strconv.ParseFloat(contract.Price, 64)
  613. // returned, _ := strconv.ParseFloat(contract.Returned, 64)
  614. // paid, _ := strconv.ParseFloat(contract.Paid, 64)
  615. price, _ := strconv.ParseFloat(contract.Price, 64)
  616. // contractsVM.PriceFloat = price
  617. contractsVM.Price = price
  618. contractsVM.Returned = contract.Returned
  619. contractsVM.Paid = contract.Paid
  620. contractsVM.Status = contract.Status
  621. contractsVM.Locking = contract.Locking
  622. contractsVM.CreateTime = contract.CreateTime.Format(conf.SysTimeform)
  623. contractsVM.UpdateTime = contract.UpdateTime.Format(conf.SysTimeform)
  624. // nilTime := time.Time{}
  625. // contract.SignerTime != nilTime
  626. if !contract.SignerTime.IsZero() {
  627. contractsVM.SignerTime = contract.SignerTime.Format(conf.SysTimeform)
  628. }
  629. return contractsVM
  630. }