ContractReview.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package services
  2. import (
  3. "ERP_storage/Nats/NatsServer"
  4. "ERP_storage/conf"
  5. "ERP_storage/dto"
  6. db "ERP_storage/initialize"
  7. "ERP_storage/logs"
  8. "ERP_storage/models/Account"
  9. models "ERP_storage/models/ContractReview"
  10. "errors"
  11. "fmt"
  12. "git.baozhida.cn/ERP_libs/lib"
  13. )
  14. type ContractReview struct {
  15. }
  16. func (e *ContractReview) GetPage(c *dto.ContractReviewPageReq) (list []models.ContractReview, cnt int64) {
  17. var err error
  18. err = db.DB.Model(&models.ContractReview{}).
  19. Scopes(
  20. dto.MakeCondition(c.GetNeedSearch()),
  21. dto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  22. dto.WithNormalState(),
  23. ).
  24. Preload("ServiceItemList", "t__state = ?", 1).
  25. Preload("ServiceItemList.ServiceItem.ServiceType").
  26. Find(&list).Limit(-1).Offset(-1).
  27. Count(&cnt).Error
  28. if err != nil {
  29. logs.Error("db error: %s ", err)
  30. return
  31. }
  32. for i := 0; i < len(list); i++ {
  33. list[i].T_submit_name = Account.Read_User_T_name_Get(list[i].T_submit)
  34. }
  35. return
  36. }
  37. func (e *ContractReview) GetFinancePage(c *dto.ContractReviewPageReq) (list []models.ContractReview, cnt int64) {
  38. var err error
  39. err = db.DB.Model(&models.ContractReview{}).
  40. Scopes(
  41. dto.MakeCondition(c.GetNeedSearch()),
  42. dto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  43. dto.WithNormalState(),
  44. ).
  45. Where("t_audit in (?)", []int{2, 3, 4, 5, 6}).
  46. Preload("ServiceItemList", "t__state = ?", 1).
  47. Preload("ServiceItemList.ServiceItem.ServiceType").
  48. Find(&list).Limit(-1).Offset(-1).
  49. Count(&cnt).Error
  50. if err != nil {
  51. logs.Error("db error: %s ", err)
  52. return
  53. }
  54. for i := 0; i < len(list); i++ {
  55. list[i].T_submit_name = Account.Read_User_T_name_Get(list[i].T_submit)
  56. }
  57. return
  58. }
  59. func (e *ContractReview) GetManagerPage(c *dto.ContractReviewPageReq) (list []models.ContractReview, cnt int64) {
  60. var err error
  61. err = db.DB.Model(&models.ContractReview{}).
  62. Scopes(
  63. dto.MakeCondition(c.GetNeedSearch()),
  64. dto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  65. dto.WithNormalState(),
  66. ).
  67. Where("t_audit in (?)", []int{3, 5, 6}).
  68. Preload("ServiceItemList", "t__state = ?", 1).
  69. Preload("ServiceItemList.ServiceItem.ServiceType").
  70. Find(&list).Limit(-1).Offset(-1).
  71. Count(&cnt).Error
  72. if err != nil {
  73. logs.Error("db error: %s ", err)
  74. return
  75. }
  76. for i := 0; i < len(list); i++ {
  77. list[i].T_submit_name = Account.Read_User_T_name_Get(list[i].T_submit)
  78. }
  79. return
  80. }
  81. func (e *ContractReview) GetSubmitUserPage(c *dto.ContractReviewSubmitUserPageReq) (list []dto.ContractReviewSubmitUserPageRes, cnt int64) {
  82. var err error
  83. if c.T_type == "Manager" {
  84. err = db.DB.Model(&models.ContractReview{}).
  85. Select("Distinct t_submit").
  86. Scopes(
  87. dto.WithNormalState(),
  88. ).
  89. Where("t_audit in (?)", []int{3, 5, 6}).
  90. Find(&list).
  91. Count(&cnt).Error
  92. }
  93. if c.T_type == "Finance" {
  94. err = db.DB.Model(&models.ContractReview{}).
  95. Select("Distinct t_submit").
  96. Scopes(
  97. dto.WithNormalState(),
  98. ).
  99. Where("t_audit in (?)", []int{2, 3, 4, 5, 6}).
  100. Find(&list).
  101. Count(&cnt).Error
  102. }
  103. if err != nil {
  104. logs.Error("db error: %s ", err)
  105. return
  106. }
  107. for i := 0; i < len(list); i++ {
  108. list[i].T_name = Account.Read_User_T_name_Get(list[i].T_submit)
  109. }
  110. return
  111. }
  112. func (e *ContractReview) Get(Id int) (r models.ContractReview, err error) {
  113. err = db.DB.Scopes(dto.WithNormalState()).First(&r, Id).Error
  114. if err != nil {
  115. logs.Error("db error: %s", err)
  116. return
  117. }
  118. return
  119. }
  120. // 添加
  121. func (e *ContractReview) Insert(c *dto.ContractReviewInsertReq) (id int, err error) {
  122. var data models.ContractReview
  123. tx := db.DB.Begin()
  124. defer func() {
  125. if err != nil {
  126. tx.Rollback()
  127. } else {
  128. tx.Commit()
  129. }
  130. }()
  131. c.Generate(&data)
  132. err = tx.Create(&data).Error
  133. if err != nil {
  134. logs.Error("db error: %s", err)
  135. return
  136. }
  137. // 添加 服务内容
  138. for i := 0; i < len(c.ServiceItemList); i++ {
  139. c.ServiceItemList[i].T_contract_review_id = data.Id
  140. c.ServiceItemList[i].T_State = 1
  141. }
  142. err = tx.Create(&c.ServiceItemList).Error
  143. if err != nil {
  144. logs.Error("db error: %s", err)
  145. return
  146. }
  147. id = data.Id
  148. return
  149. }
  150. // 修改
  151. func (e *ContractReview) Update(c *dto.ContractReviewUpdateReq) error {
  152. var contractReview = models.ContractReview{}
  153. err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error
  154. if err != nil {
  155. logs.Error("db error: %s", err)
  156. return dto.GetFailedErr
  157. }
  158. // 财务审核通过3 总经理审核通过5 不可修改
  159. if contractReview.T_audit == 3 || contractReview.T_audit == 5 {
  160. logs.Error("db error: %s", err)
  161. return errors.New("审核通过后不可修改")
  162. }
  163. tx := db.DB.Begin()
  164. defer func() {
  165. if err != nil {
  166. tx.Rollback()
  167. } else {
  168. tx.Commit()
  169. }
  170. }()
  171. c.Generate(&contractReview)
  172. err = tx.Save(&contractReview).Error
  173. if err != nil {
  174. logs.Error("db error: %s", err)
  175. return err
  176. }
  177. // 删除之前的服务内容
  178. err = tx.Model(models.ContractReviewServiceItem{}).Where("t_contract_review_id = ?", contractReview.Id).
  179. Updates(map[string]interface{}{
  180. "t__state": 0,
  181. }).Error
  182. if err != nil {
  183. logs.Error("db error: %s", err)
  184. return err
  185. }
  186. // 添加 新的服务内容
  187. for i := 0; i < len(c.ServiceItemList); i++ {
  188. c.ServiceItemList[i].Id = 0
  189. c.ServiceItemList[i].T_contract_review_id = contractReview.Id
  190. c.ServiceItemList[i].T_State = 1
  191. }
  192. err = tx.Create(&c.ServiceItemList).Error
  193. if err != nil {
  194. logs.Error("db error: %s", err)
  195. return err
  196. }
  197. return nil
  198. }
  199. func (e *ContractReview) SubmitAudit(c *dto.ContractReviewGetReq) error {
  200. var contractReview = models.ContractReview{}
  201. err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error
  202. if err != nil {
  203. logs.Error("db error: %s", err)
  204. return dto.GetFailedErr
  205. }
  206. // 财务审核通过3 总经理审核通过5 不可修改
  207. if contractReview.T_audit == 3 || contractReview.T_audit == 5 {
  208. logs.Error("db error: %s", err)
  209. return errors.New("审核通过后不可提交")
  210. }
  211. tx := db.DB.Begin()
  212. defer func() {
  213. if err != nil {
  214. tx.Rollback()
  215. } else {
  216. tx.Commit()
  217. }
  218. }()
  219. // 提交审核后修改为待审核
  220. contractReview.T_audit = 2
  221. err = tx.Save(&contractReview).Error
  222. if err != nil {
  223. logs.Error("db error: %s", err)
  224. return err
  225. }
  226. return nil
  227. }
  228. func (e *ContractReview) FinanceAudit(c *dto.ContractReviewAuditReq) error {
  229. var contractReview = models.ContractReview{}
  230. err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error
  231. if err != nil {
  232. logs.Error("db error: %s", err)
  233. return dto.GetFailedErr
  234. }
  235. // 财务审核通过3 总经理审核通过5 不可修改
  236. if contractReview.T_audit != 2 {
  237. logs.Error("db error: %s", err)
  238. return errors.New("审核通过后不可提交")
  239. }
  240. // 财务审核
  241. contractReview.T_audit = c.T_audit
  242. contractReview.T_finance_approval_opinion = c.T_approval_opinion
  243. err = db.DB.Save(&contractReview).Error
  244. if err != nil {
  245. logs.Error("db error: %s", err)
  246. return err
  247. }
  248. if c.T_audit == 3 {
  249. NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)财务审核已通过", contractReview.T_name), conf.ContractReviewAuditUrl)
  250. }
  251. if c.T_audit == 4 {
  252. NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)财务审核已驳回", contractReview.T_name), conf.ContractReviewAuditUrl)
  253. }
  254. return nil
  255. }
  256. func (e *ContractReview) ManagerAudit(c *dto.ContractReviewAuditReq) error {
  257. var contractReview = models.ContractReview{}
  258. err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error
  259. if err != nil {
  260. logs.Error("db error: %s", err)
  261. return dto.GetFailedErr
  262. }
  263. // 财务审核通过3 总经理审核通过5 不可修改
  264. if contractReview.T_audit != 3 {
  265. logs.Error("db error: %s", err)
  266. return errors.New("审核通过后不可提交")
  267. }
  268. // 管理员审核
  269. contractReview.T_audit = c.T_audit
  270. contractReview.T_manager_approval_opinion = c.T_approval_opinion
  271. err = db.DB.Save(&contractReview).Error
  272. if err != nil {
  273. logs.Error("db error: %s", err)
  274. return err
  275. }
  276. if c.T_audit == 5 {
  277. NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)总经理审核已通过", contractReview.T_name), conf.ContractReviewAuditUrl)
  278. }
  279. if c.T_audit == 6 {
  280. NatsServer.AddNews(contractReview.T_submit, fmt.Sprintf("【合同评审】您提交的合同评审(%s)总经理审核已驳回", contractReview.T_name), conf.ContractReviewAuditUrl)
  281. }
  282. return nil
  283. }
  284. // 删除
  285. func (e *ContractReview) Delete(c *dto.ContractReviewDeleteReq) error {
  286. var contractReview = models.ContractReview{}
  287. err := db.DB.Scopes(dto.WithNormalState()).First(&contractReview, c.GetId()).Error
  288. if err != nil {
  289. logs.Error("db error: %s", err)
  290. return dto.GetFailedErr
  291. }
  292. //if contractReview.T_audit == 3 || contractReview.T_audit == 5 {
  293. // return errors.New("审核通过后不可删除")
  294. //}
  295. contractReview.T_State = 0
  296. err = db.DB.Save(&contractReview).Error
  297. if err != nil {
  298. logs.Error(lib.FuncName(), err)
  299. return dto.DeleteFailedErr
  300. }
  301. return nil
  302. }