approver_dao.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * @description: 审批流程数据库操作相关
  3. * @Author: LanJianRong
  4. * @Date: 2021-01-06
  5. * @FilePath: \construction_management\dao\approver_dao.go
  6. */
  7. package dao
  8. import (
  9. "errors"
  10. "fmt"
  11. "strconv"
  12. "time"
  13. "github.com/go-xorm/xorm"
  14. "go.mod/comm"
  15. "go.mod/conf"
  16. "go.mod/models"
  17. "go.mod/web/viewmodels"
  18. )
  19. // 数据库操作引擎
  20. type ApproverDao struct {
  21. engine *xorm.Engine
  22. }
  23. // 获得一个DAO对象
  24. func NewApproverDao(engine *xorm.Engine) *ApproverDao {
  25. return &ApproverDao{
  26. engine: engine,
  27. }
  28. }
  29. // 批量插入数据
  30. func (d *ApproverDao) InsertData(cur_uid int, bid int, pid int, dataType int, dataId int, auditors []int, reAuditors []int) error {
  31. data := make([]models.CmApprover, 0)
  32. msg := make([]models.CmProjectMessage, 0)
  33. var content string
  34. var title string
  35. if dataType == 1 {
  36. safe := &models.CmSafe{Id: dataId}
  37. _, err := d.engine.Get(safe)
  38. if err != nil {
  39. return err
  40. }
  41. account := &models.CmProjectAccount{Id: safe.Uid}
  42. _, err = d.engine.Get(account)
  43. title = fmt.Sprintf("%s - %s", account.Name, account.Position)
  44. content = fmt.Sprintf("%s|%s|已上报,审批中", safe.Code, safe.InspectionDetail)
  45. } else {
  46. quality := &models.CmQuality{Id: dataId}
  47. _, err := d.engine.Get(quality)
  48. if err != nil {
  49. return err
  50. }
  51. account := &models.CmProjectAccount{Id: quality.Uid}
  52. _, err = d.engine.Get(account)
  53. if err != nil {
  54. return err
  55. }
  56. title = fmt.Sprintf("%s - %s", account.Name, account.Position)
  57. content = fmt.Sprintf("%s|%s|已上报,审批中", quality.Code, quality.InspectionDetail)
  58. }
  59. _, err := d.engine.Exec("delete from project_message where data_type = ? and data_id = ?", dataType, dataId)
  60. for i, item := range auditors {
  61. approverVM := models.CmApprover{}
  62. approverVM.AuditId = item
  63. approverVM.AuditOrder = i + 1
  64. approverVM.Progress = 0
  65. approverVM.BidsectionId = bid
  66. approverVM.ProjectId = pid
  67. approverVM.DataType = dataType
  68. approverVM.DataId = dataId
  69. approverVM.CreateTime = time.Now()
  70. approverVM.Status = 0
  71. if i == 0 {
  72. approverVM.Status = 1
  73. }
  74. data = append(data, approverVM)
  75. // 消息推送
  76. msgVM := models.CmProjectMessage{}
  77. msgVM.AccountId = item
  78. msgVM.BidsectionId = bid
  79. msgVM.ProjectId = pid
  80. msgVM.DataId = dataId
  81. msgVM.DataType = 1
  82. msgVM.Content = content
  83. msgVM.Title = title
  84. msgVM.CreateTime = time.Now()
  85. msg = append(msg, msgVM)
  86. }
  87. checkVM := models.CmApprover{}
  88. checkVM.AuditOrder = len(auditors) + 1
  89. checkVM.Progress = 1
  90. checkVM.BidsectionId = bid
  91. checkVM.ProjectId = pid
  92. checkVM.DataType = dataType
  93. checkVM.DataId = dataId
  94. data = append(data, checkVM)
  95. for i, item := range reAuditors {
  96. approverVM := models.CmApprover{}
  97. approverVM.AuditId = item
  98. approverVM.AuditOrder = i + 2 + len(auditors)
  99. approverVM.Progress = 2
  100. approverVM.BidsectionId = bid
  101. approverVM.ProjectId = pid
  102. approverVM.DataType = dataType
  103. approverVM.DataId = dataId
  104. approverVM.CreateTime = time.Now()
  105. data = append(data, approverVM)
  106. // 消息推送
  107. msgVM := models.CmProjectMessage{}
  108. msgVM.AccountId = item
  109. msgVM.BidsectionId = bid
  110. msgVM.ProjectId = pid
  111. msgVM.DataId = dataId
  112. msgVM.DataType = 1
  113. msgVM.Content = content
  114. msgVM.Title = title
  115. msgVM.CreateTime = time.Now()
  116. msg = append(msg, msgVM)
  117. }
  118. // 再推送自己
  119. msgVM := models.CmProjectMessage{}
  120. msgVM.AccountId = cur_uid
  121. msgVM.BidsectionId = bid
  122. msgVM.ProjectId = pid
  123. msgVM.DataId = dataId
  124. msgVM.DataType = 1
  125. msgVM.Content = content
  126. msgVM.Title = title
  127. msgVM.CreateTime = time.Now()
  128. msg = append(msg, msgVM)
  129. _, err = d.engine.Insert(data)
  130. _, err = d.engine.Insert(msg)
  131. return err
  132. }
  133. // 更改状态
  134. func (d *ApproverDao) ChangeStatus(id int, status int) error {
  135. data := &models.CmApprover{Status: status}
  136. _, err := d.engine.Where("id = ?", id).Cols("status").Update(data)
  137. return err
  138. }
  139. // 更改审批人
  140. func (d *ApproverDao) ChangeAuditId(bid int, dataType int, dataId int, auditOrder int, uid int) error {
  141. data := &models.CmApprover{AuditId: uid}
  142. _, err := d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", bid, dataType, dataId, auditOrder).Cols("audit_id").Update(data)
  143. return err
  144. }
  145. // 更改下一个审批人的状态
  146. func (d *ApproverDao) ChangeNextStatus(id int, status int) error {
  147. data := &models.CmApprover{Id: id}
  148. _, err := d.engine.Get(data)
  149. newData := &models.CmApprover{Status: status}
  150. _, err = d.engine.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", data.BidsectionId, data.DataType, data.DataId, data.AuditOrder+1).Cols("status").Update(newData)
  151. return err
  152. }
  153. // 根据当前times,获取审批流程(包括原报)
  154. func (d *ApproverDao) GetAuditorsWithOwner(bid int, dataType int, dataId int, cur_uid int) []viewmodels.Auditors {
  155. auditors := make([]viewmodels.Auditors, 0)
  156. auditor := viewmodels.Auditors{Progress: "", Id: "0", Status: 2}
  157. _, err := d.engine.Sql("select name, company, position, mobile, id as `audit_id` from `cm_project_account` where id = ?", cur_uid).Get(&auditor)
  158. if err != nil {
  159. fmt.Println(err)
  160. }
  161. auditors = append(auditors, auditor)
  162. d.engine.Sql("select pa.`company`, pa.`name`, pa.`account_group`,pa.`mobile`, pa.`position`,ca.`audit_id` as `audit_id`, ca.`id`, ca.`status`, ca.`audit_order`, ca.`progress` from `cm_project_account` as pa, `cm_approver` as ca where ca.`bidsection_id` = ? and ca.`data_type` = ? and ca.`data_id` = ? and ca.audit_id = pa.id order by `audit_order`", bid, dataType, dataId).Find(&auditors)
  163. // 原报
  164. return auditors
  165. }
  166. func (d *ApproverDao) FindApproverById(id int) (*models.CmApprover, error) {
  167. data := &models.CmApprover{}
  168. _, err := d.engine.ID(id).Get(data)
  169. return data, err
  170. }
  171. // 获取最新的审核人
  172. func (d *ApproverDao) GetLastedAuditor(bid int, dataType int, dataId int) *viewmodels.Approver {
  173. data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId, Status: 1}
  174. has, _ := d.engine.Get(data)
  175. approverVM := &viewmodels.Approver{}
  176. if has == true {
  177. id, _ := comm.AesEncrypt(strconv.Itoa(data.Id), conf.SignSecret)
  178. approverVM.Id = id
  179. pid, _ := comm.AesEncrypt(strconv.Itoa(data.ProjectId), conf.SignSecret)
  180. approverVM.ProjectId = pid
  181. bid, _ := comm.AesEncrypt(strconv.Itoa(data.BidsectionId), conf.SignSecret)
  182. approverVM.BidsectionId = bid
  183. dataId, _ := comm.AesEncrypt(strconv.Itoa(data.DataId), conf.SignSecret)
  184. approverVM.DataId = dataId
  185. auditId, _ := comm.AesEncrypt(strconv.Itoa(data.AuditId), conf.SignSecret)
  186. approverVM.AuditId = auditId
  187. approverVM.AuditOrder = data.AuditOrder
  188. approverVM.DataType = data.DataType
  189. approverVM.Progress = strconv.Itoa(data.Progress)
  190. approverVM.Status = data.Status
  191. }
  192. return approverVM
  193. }
  194. // 获取最后一个审批人
  195. func (d *ApproverDao) GetLastAuditor(bid int, dataType int, dataId int) (*models.CmApprover, error) {
  196. data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
  197. _, err := d.engine.Desc("audit_order").Limit(1).Get(data)
  198. return data, err
  199. }
  200. // 初始化审批流程状态
  201. func (d *ApproverDao) InitStatus(auditType string, bid int, dataType int, dataId int, auditId int, times int, progress int, opinion string) error {
  202. session := d.engine.NewSession()
  203. defer session.Close()
  204. err := session.Begin()
  205. if err != nil {
  206. return errors.New("操作失败-db")
  207. }
  208. // 消息推送,先删除旧的推送
  209. _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditType, dataId)
  210. if err != nil {
  211. session.Rollback()
  212. return err
  213. }
  214. approvers := make([]models.CmApprover, 0)
  215. err = session.Where("data_type = ? and data_id = ? and audit_id != 0", dataType, dataId).Find(&approvers)
  216. if err != nil {
  217. session.Rollback()
  218. return err
  219. }
  220. data := &models.CmApprover{Status: 0}
  221. _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ?", bid, dataType, dataId).Cols("status").Update(data)
  222. if err != nil {
  223. session.Rollback()
  224. return err
  225. }
  226. if auditType == "safe" {
  227. safe := &models.CmSafe{}
  228. _, err = session.ID(dataId).Get(safe)
  229. if err != nil {
  230. session.Rollback()
  231. return err
  232. }
  233. account := &models.CmProjectAccount{Id: safe.Uid}
  234. _, err = d.engine.Get(account)
  235. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  236. msg := make([]models.CmProjectMessage, 0)
  237. for _, item := range approvers {
  238. msgVM := models.CmProjectMessage{}
  239. msgVM.AccountId = item.AuditId
  240. msgVM.BidsectionId = item.BidsectionId
  241. msgVM.ProjectId = item.ProjectId
  242. msgVM.CreateTime = time.Now()
  243. msgVM.DataId = item.DataId
  244. msgVM.DataType = item.DataType
  245. msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", safe.Code, safe.InspectionDetail)
  246. msgVM.Title = title
  247. msg = append(msg, msgVM)
  248. }
  249. // 添加巡检拥有者的推送
  250. msgVM := models.CmProjectMessage{}
  251. msgVM.AccountId = safe.Uid
  252. msgVM.BidsectionId = safe.BidsectionId
  253. msgVM.ProjectId = safe.ProjectId
  254. msgVM.CreateTime = time.Now()
  255. msgVM.DataId = safe.Id
  256. msgVM.DataType = 1
  257. msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", safe.Code, safe.InspectionDetail)
  258. msgVM.Title = title
  259. msg = append(msg, msgVM)
  260. // 插入消息
  261. _, err = session.Insert(msg)
  262. if err != nil {
  263. session.Rollback()
  264. return err
  265. }
  266. _, err = session.Exec("update cm_safe set status = ?, times = ? where id = ?", 0, times+1, dataId)
  267. if err != nil {
  268. session.Rollback()
  269. return err
  270. }
  271. // 增加审批日志
  272. safeAudit := &models.CmSafeAudit{BidsectionId: bid, SafeId: dataId, Times: times, AuditId: auditId, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
  273. _, err = session.Insert(safeAudit)
  274. if err != nil {
  275. session.Rollback()
  276. return err
  277. }
  278. // 说明本身除于整改人流程,退回原报,要将整改总数-1,未整改总数+1
  279. if progress-1 != 0 {
  280. _, err = session.Exec("update cm_tree set safe_rectification = safe_rectification + 1, safe_rectification_in = if(safe_rectification_in >= 1, safe_rectification_in - 1, 0) where bidsection_id = ?", bid)
  281. }
  282. } else {
  283. quality := &models.CmQuality{}
  284. _, err = session.ID(dataId).Get(quality)
  285. if err != nil {
  286. session.Rollback()
  287. return err
  288. }
  289. account := &models.CmProjectAccount{Id: quality.Uid}
  290. _, err = d.engine.Get(account)
  291. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  292. msg := make([]models.CmProjectMessage, 0)
  293. for _, item := range approvers {
  294. msgVM := models.CmProjectMessage{}
  295. msgVM.AccountId = item.AuditId
  296. msgVM.BidsectionId = item.BidsectionId
  297. msgVM.ProjectId = item.ProjectId
  298. msgVM.CreateTime = time.Now()
  299. msgVM.DataId = item.DataId
  300. msgVM.DataType = item.DataType
  301. msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", quality.Code, quality.InspectionDetail)
  302. msgVM.Title = title
  303. msg = append(msg, msgVM)
  304. }
  305. // 添加巡检拥有者的推送
  306. msgVM := models.CmProjectMessage{}
  307. msgVM.AccountId = quality.Uid
  308. msgVM.BidsectionId = quality.BidsectionId
  309. msgVM.ProjectId = quality.ProjectId
  310. msgVM.CreateTime = time.Now()
  311. msgVM.DataId = quality.Id
  312. msgVM.DataType = 1
  313. msgVM.Content = fmt.Sprintf("%s|%s|审批退回到上报人", quality.Code, quality.InspectionDetail)
  314. msgVM.Title = title
  315. msg = append(msg, msgVM)
  316. // 插入消息
  317. _, err = session.Insert(msg)
  318. if err != nil {
  319. session.Rollback()
  320. return err
  321. }
  322. _, err = session.Exec("update cm_quality set status = ?, times = ? where id = ?", 0, times+1, dataId)
  323. if err != nil {
  324. session.Rollback()
  325. return err
  326. }
  327. // 增加审批日志
  328. qualityAudit := &models.CmQualityAudit{BidsectionId: bid, QualityId: dataId, Times: times, AuditId: auditId, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
  329. _, err = session.Insert(qualityAudit)
  330. if err != nil {
  331. session.Rollback()
  332. return err
  333. }
  334. if progress-1 != 0 {
  335. _, err = session.Exec("update cm_tree set quality_rectification = quality_rectification + 1, quality_rectification_in = if(quality_rectification_in >= 1, quality_rectification_in - 1, 0) where bidsection_id = ?", bid)
  336. }
  337. }
  338. err = session.Commit()
  339. return err
  340. }
  341. // 审批通过
  342. func (d *ApproverDao) PassHandler(auditType string, id int, uid int, auditId int, opinion string, content string, rectifiedTime string) error {
  343. session := d.engine.NewSession()
  344. defer session.Close()
  345. // add Begin() before any action
  346. err := session.Begin()
  347. auditor, err := d.FindApproverById(id)
  348. if err != nil {
  349. return err
  350. }
  351. if auditor.AuditId != uid {
  352. return errors.New("该用户没有审批权限")
  353. }
  354. var msgContent string
  355. switch auditor.Progress {
  356. case 0:
  357. msgContent = "审批完成,待整改"
  358. case 1:
  359. msgContent = "整改完成,待复查"
  360. case 2:
  361. msgContent = "复查完成"
  362. }
  363. // 消息推送,先删除旧的推送
  364. _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
  365. if err != nil {
  366. session.Rollback()
  367. return err
  368. }
  369. approvers := make([]models.CmApprover, 0)
  370. err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
  371. if err != nil {
  372. session.Rollback()
  373. return err
  374. }
  375. // 安全巡检
  376. if auditType == "safe" {
  377. safe := &models.CmSafe{}
  378. _, err = session.ID(auditor.DataId).Get(safe)
  379. if err != nil {
  380. session.Rollback()
  381. return err
  382. }
  383. account := &models.CmProjectAccount{Id: safe.Uid}
  384. _, err = d.engine.Get(account)
  385. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  386. msg := make([]models.CmProjectMessage, 0)
  387. for _, item := range approvers {
  388. msgVM := models.CmProjectMessage{}
  389. msgVM.AccountId = item.AuditId
  390. msgVM.BidsectionId = item.BidsectionId
  391. msgVM.ProjectId = item.ProjectId
  392. msgVM.CreateTime = time.Now()
  393. msgVM.DataId = item.DataId
  394. msgVM.DataType = item.DataType
  395. msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, safe.Code, safe.InspectionDetail)
  396. msgVM.Title = title
  397. msg = append(msg, msgVM)
  398. }
  399. // 添加巡检拥有者的推送
  400. msgVM := models.CmProjectMessage{}
  401. msgVM.AccountId = safe.Uid
  402. msgVM.BidsectionId = safe.BidsectionId
  403. msgVM.ProjectId = safe.ProjectId
  404. msgVM.CreateTime = time.Now()
  405. msgVM.DataId = safe.Id
  406. msgVM.DataType = 1
  407. msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, safe.Code, safe.InspectionDetail)
  408. msgVM.Title = title
  409. msg = append(msg, msgVM)
  410. // 插入消息
  411. _, err = session.Insert(msg)
  412. if err != nil {
  413. session.Rollback()
  414. return err
  415. }
  416. // 增加审批日志
  417. auditReacord := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, AuditId: auditor.AuditId, Times: safe.Times, CreateTime: time.Now(), Status: 0, Progress: auditor.Progress + 1, Opinion: opinion}
  418. _, err = session.Insert(auditReacord)
  419. if err != nil {
  420. session.Rollback()
  421. return err
  422. }
  423. // 改变审批流程中当前审批人以及下一个审批人的审批流程状态
  424. curData := &models.CmApprover{Status: 2}
  425. _, err = session.ID(id).Cols("status").Update(curData)
  426. if err != nil {
  427. session.Rollback()
  428. return err
  429. }
  430. nextData := &models.CmApprover{Status: 1}
  431. _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1).Cols("status").Update(nextData)
  432. if err != nil {
  433. session.Rollback()
  434. return err
  435. }
  436. if auditId != 0 {
  437. // 审批人选择了整改人
  438. // 修改cm_safe表的status状态为待整改
  439. _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 2, auditor.DataId)
  440. if err != nil {
  441. session.Rollback()
  442. return err
  443. }
  444. // 将cm_tree的safe_rectification待整改+1
  445. _, err = session.Exec("update cm_tree set safe_rectification_in = safe_rectification_in + 1, safe_rectification = if(safe_rectification >= 1, safe_rectification - 1, 0) where bidsection_id = ?", auditor.BidsectionId)
  446. if err != nil {
  447. session.Rollback()
  448. return err
  449. }
  450. // 改变审批流程中的整改人id, 以及创建时间
  451. _, err = session.Exec("update `cm_approver` set `audit_id` = ? where `bidsection_id` = ? and `data_type` = ? and `data_id` = ? and `audit_order` = ? create_time = ?", auditId, auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1, time.Now())
  452. if err != nil {
  453. session.Rollback()
  454. return err
  455. }
  456. } else {
  457. if auditor.Progress == 1 {
  458. // 整改人审批流程
  459. // 修改cm_safe表的status状态为待复查
  460. _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 3, auditor.DataId)
  461. if err != nil {
  462. session.Rollback()
  463. return err
  464. }
  465. // 将整改单的状态->完成,并且填入整改日期
  466. rectification := &models.CmRectification{}
  467. rectification.BidsectionId = safe.BidsectionId
  468. rectification.ProjectId = safe.ProjectId
  469. rectification.DataId = safe.Id
  470. rectification.DataType = 0
  471. rectification.CreateTime = time.Now()
  472. newRectifiedTime, _ := time.Parse("2006-01-02 15:04:05", rectifiedTime)
  473. rectification.RectifiedTime = newRectifiedTime
  474. rectification.Content = content
  475. rectification.Status = 1
  476. _, err = session.Insert(rectification)
  477. if err != nil {
  478. session.Rollback()
  479. return err
  480. }
  481. } else {
  482. // 复查流程
  483. // 查找最后一个审批人
  484. lastAuditor := &models.CmApprover{BidsectionId: auditor.BidsectionId, DataType: auditor.DataType, DataId: auditor.DataId}
  485. _, err = session.Desc("audit_order").Limit(1).Get(lastAuditor)
  486. if err != nil {
  487. session.Rollback()
  488. return err
  489. }
  490. if lastAuditor.Id == auditor.Id {
  491. // 说明审批流程已经走完
  492. _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 4, auditor.DataId)
  493. if err != nil {
  494. session.Rollback()
  495. return err
  496. }
  497. // 将cm_tree的safe_rectification_finish待整改+1
  498. _, err := session.Exec("update cm_tree set safe_rectification_finish = safe_rectification_finish + 1, safe_rectification_in = if(safe_rectification_in >= 1, safe_rectification_in - 1, 0)where bidsection_id = ?", auditor.BidsectionId)
  499. if err != nil {
  500. session.Rollback()
  501. return err
  502. }
  503. }
  504. }
  505. }
  506. } else {
  507. // 质量巡检
  508. quality := &models.CmQuality{}
  509. _, err = session.ID(auditor.DataId).Get(quality)
  510. if err != nil {
  511. session.Rollback()
  512. return err
  513. }
  514. account := &models.CmProjectAccount{Id: quality.Uid}
  515. _, err = d.engine.Get(account)
  516. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  517. msg := make([]models.CmProjectMessage, 0)
  518. for _, item := range approvers {
  519. msgVM := models.CmProjectMessage{}
  520. msgVM.AccountId = item.AuditId
  521. msgVM.BidsectionId = item.BidsectionId
  522. msgVM.ProjectId = item.ProjectId
  523. msgVM.CreateTime = time.Now()
  524. msgVM.DataId = item.DataId
  525. msgVM.DataType = item.DataType
  526. msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, quality.Code, quality.InspectionDetail)
  527. msgVM.Title = title
  528. msg = append(msg, msgVM)
  529. }
  530. // 添加巡检拥有者的推送
  531. msgVM := models.CmProjectMessage{}
  532. msgVM.AccountId = quality.Uid
  533. msgVM.BidsectionId = quality.BidsectionId
  534. msgVM.ProjectId = quality.ProjectId
  535. msgVM.CreateTime = time.Now()
  536. msgVM.DataId = quality.Id
  537. msgVM.DataType = 1
  538. msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, quality.Code, quality.InspectionDetail)
  539. msgVM.Title = title
  540. msg = append(msg, msgVM)
  541. // 插入消息
  542. _, err = session.Insert(msg)
  543. if err != nil {
  544. session.Rollback()
  545. return err
  546. }
  547. // 增加审批日志
  548. auditReacord := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, AuditId: auditor.AuditId, Times: quality.Times, CreateTime: time.Now(), Status: 0, Progress: auditor.Progress + 1, Opinion: opinion}
  549. _, err = session.Insert(auditReacord)
  550. if err != nil {
  551. session.Rollback()
  552. return err
  553. }
  554. // 改变审批流程中当前审批人以及下一个审批人的审批流程状态
  555. curData := &models.CmApprover{Status: 2}
  556. _, err = session.ID(id).Cols("status").Update(curData)
  557. if err != nil {
  558. session.Rollback()
  559. return err
  560. }
  561. nextData := &models.CmApprover{Status: 1}
  562. _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order = ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1).Cols("status").Update(nextData)
  563. if err != nil {
  564. session.Rollback()
  565. return err
  566. }
  567. if auditId != 0 {
  568. // 审批人选择了整改人
  569. // 修改cm_quality表的status状态为待整改
  570. _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 2, auditor.DataId)
  571. if err != nil {
  572. session.Rollback()
  573. return err
  574. }
  575. // 将cm_tree的quality_rectification待整改+1
  576. _, err = session.Exec("update cm_tree set quality_rectification_in = quality_rectification_in + 1, quality_rectification = if(quality_rectification >= 1, quality_rectification - 1, 0) where bidsection_id = ?", auditor.BidsectionId)
  577. if err != nil {
  578. session.Rollback()
  579. return err
  580. }
  581. // 改变审批流程中的整改人id
  582. _, err = session.Exec("update `cm_approver` set `audit_id` = ? where `bidsection_id` = ? and `data_type` = ? and `data_id` = ? and `audit_order` = ?", auditId, auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1)
  583. if err != nil {
  584. session.Rollback()
  585. return err
  586. }
  587. } else {
  588. if auditor.Progress == 1 {
  589. // 整改人审批流程
  590. // 修改cm_quality表的status状态为待复查
  591. _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 3, auditor.DataId)
  592. if err != nil {
  593. session.Rollback()
  594. return err
  595. }
  596. // 将整改单的状态->完成,并且填入整改日期
  597. rectification := &models.CmRectification{}
  598. rectification.BidsectionId = quality.BidsectionId
  599. rectification.ProjectId = quality.ProjectId
  600. rectification.DataId = quality.Id
  601. rectification.DataType = 1
  602. rectification.CreateTime = time.Now()
  603. newRectifiedTime, _ := time.Parse("2006-01-02 15:04:05", rectifiedTime)
  604. rectification.RectifiedTime = newRectifiedTime
  605. rectification.Content = content
  606. rectification.Status = 1
  607. _, err = session.Insert(rectification)
  608. if err != nil {
  609. session.Rollback()
  610. return err
  611. }
  612. } else {
  613. // 复查流程
  614. // 查找最后一个审批人
  615. lastAuditor := &models.CmApprover{BidsectionId: auditor.BidsectionId, DataType: auditor.DataType, DataId: auditor.DataId}
  616. _, err = session.Desc("audit_order").Limit(1).Get(lastAuditor)
  617. if err != nil {
  618. session.Rollback()
  619. return err
  620. }
  621. if lastAuditor.Id == auditor.Id {
  622. // 说明审批流程已经走完
  623. _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 4, auditor.DataId)
  624. if err != nil {
  625. session.Rollback()
  626. return err
  627. }
  628. // 将cm_tree的quality_rectification_finish待整改+1
  629. _, err := session.Exec("update cm_tree set quality_rectification_finish = quality_rectification_finish + 1, quality_rectification_in = if(quality_rectification_in >= 1, quality_rectification_in - 1, 0) where bidsection_id = ?", auditor.BidsectionId)
  630. if err != nil {
  631. session.Rollback()
  632. return err
  633. }
  634. }
  635. }
  636. }
  637. }
  638. err = session.Commit()
  639. return err
  640. }
  641. // 审批流程-退回
  642. func (d *ApproverDao) BackHandlerWithId(auditType string, id int, uid int, times int, progress int, opinion string) error {
  643. session := d.engine.NewSession()
  644. defer session.Close()
  645. err := session.Begin()
  646. auditor := &models.CmApprover{}
  647. _, err = session.ID(id).Get(auditor)
  648. // 更改safe/quality表的记录
  649. data := &models.CmApprover{Status: 0}
  650. // 将往后的所有记录的status改为0
  651. _, err = session.Where("bidsection_id = ? and data_type = ? and data_id = ? and audit_order > ?", auditor.BidsectionId, auditor.DataType, auditor.DataId, auditor.AuditOrder).Cols("status").Update(data)
  652. if err != nil {
  653. session.Rollback()
  654. return err
  655. }
  656. // 获取改账号的信息
  657. account := &models.CmProjectAccount{Id: auditor.AuditId}
  658. _, err = session.Get(account)
  659. if err != nil {
  660. session.Rollback()
  661. return err
  662. }
  663. // 将当前的记录改为1-待审批
  664. data.Status = 1
  665. _, err = session.ID(id).Cols("status").Update(data)
  666. if err != nil {
  667. session.Rollback()
  668. return err
  669. }
  670. // 消息推送,先删除旧的推送
  671. _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
  672. if err != nil {
  673. session.Rollback()
  674. return err
  675. }
  676. approvers := make([]models.CmApprover, 0)
  677. err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
  678. if err != nil {
  679. session.Rollback()
  680. return err
  681. }
  682. // 增加审批日志
  683. if auditType == "safe" {
  684. safe := &models.CmSafe{}
  685. _, err = session.ID(auditor.DataId).Get(safe)
  686. if err != nil {
  687. session.Rollback()
  688. return err
  689. }
  690. safe.Status = auditor.Progress + 1
  691. _, err = session.Where("id = ? and bidsection_id = ?", safe.Id, safe.BidsectionId).Limit(1).Update(safe)
  692. if err != nil {
  693. session.Rollback()
  694. return err
  695. }
  696. account := &models.CmProjectAccount{Id: safe.Uid}
  697. _, err = d.engine.Get(account)
  698. if err != nil {
  699. session.Rollback()
  700. return err
  701. }
  702. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  703. msg := make([]models.CmProjectMessage, 0)
  704. for _, item := range approvers {
  705. msgVM := models.CmProjectMessage{}
  706. msgVM.AccountId = item.AuditId
  707. msgVM.BidsectionId = item.BidsectionId
  708. msgVM.ProjectId = item.ProjectId
  709. msgVM.CreateTime = time.Now()
  710. msgVM.DataId = item.DataId
  711. msgVM.DataType = item.DataType
  712. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", safe.Code, safe.InspectionDetail, account.Name)
  713. msgVM.Title = title
  714. msg = append(msg, msgVM)
  715. }
  716. // 添加巡检拥有者的推送
  717. msgVM := models.CmProjectMessage{}
  718. msgVM.AccountId = safe.Uid
  719. msgVM.BidsectionId = safe.BidsectionId
  720. msgVM.ProjectId = safe.ProjectId
  721. msgVM.CreateTime = time.Now()
  722. msgVM.DataId = safe.Id
  723. msgVM.DataType = 1
  724. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", safe.Code, safe.InspectionDetail, account.Name)
  725. msg = append(msg, msgVM)
  726. // 插入消息
  727. _, err = session.Insert(msg)
  728. if err != nil {
  729. session.Rollback()
  730. return err
  731. }
  732. safeAudit := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, Times: times, AuditId: uid, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
  733. _, err = session.Insert(safeAudit)
  734. if err != nil {
  735. session.Rollback()
  736. return err
  737. }
  738. } else {
  739. quality := &models.CmQuality{}
  740. _, err = session.ID(auditor.DataId).Get(quality)
  741. if err != nil {
  742. session.Rollback()
  743. return err
  744. }
  745. quality.Status = auditor.Progress + 1
  746. _, err = session.Where("id =? and bidsection_id = ?", quality.Id, quality.BidsectionId).Limit(1).Update(quality)
  747. if err != nil {
  748. session.Rollback()
  749. return err
  750. }
  751. account := &models.CmProjectAccount{Id: quality.Uid}
  752. _, err = d.engine.Get(account)
  753. if err != nil {
  754. session.Rollback()
  755. return err
  756. }
  757. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  758. msg := make([]models.CmProjectMessage, 0)
  759. for _, item := range approvers {
  760. msgVM := models.CmProjectMessage{}
  761. msgVM.AccountId = item.AuditId
  762. msgVM.BidsectionId = item.BidsectionId
  763. msgVM.ProjectId = item.ProjectId
  764. msgVM.CreateTime = time.Now()
  765. msgVM.DataId = item.DataId
  766. msgVM.DataType = item.DataType
  767. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", quality.Code, quality.InspectionDetail, account.Name)
  768. msgVM.Title = title
  769. msg = append(msg, msgVM)
  770. }
  771. // 添加巡检拥有者的推送
  772. msgVM := models.CmProjectMessage{}
  773. msgVM.AccountId = quality.Uid
  774. msgVM.BidsectionId = quality.BidsectionId
  775. msgVM.ProjectId = quality.ProjectId
  776. msgVM.CreateTime = time.Now()
  777. msgVM.DataId = quality.Id
  778. msgVM.DataType = 2
  779. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", quality.Code, quality.InspectionDetail, account.Name)
  780. msgVM.Title = title
  781. msg = append(msg, msgVM)
  782. // 插入消息
  783. _, err = session.Insert(msg)
  784. if err != nil {
  785. session.Rollback()
  786. return err
  787. }
  788. qualityAudit := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, Times: times, AuditId: uid, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
  789. _, err = session.Insert(qualityAudit)
  790. if err != nil {
  791. session.Rollback()
  792. return err
  793. }
  794. }
  795. curAuditor := &models.CmApprover{}
  796. _, err = session.ID(uid).Get(curAuditor)
  797. if err != nil {
  798. session.Rollback()
  799. return err
  800. }
  801. // 当前审批人是整改或者复查流程的并且退回的是审批流程的
  802. if curAuditor.Progress >= 1 && auditor.Progress == 0 {
  803. var sql string
  804. if auditType == "safe" {
  805. sql = "update cm_tree set safe_rectification = safe_rectification_in + 1 and safe_rectification_in = if(safe_rectification_in >= 1, safe_rectification_in - 1, 0) where bidsection_id = ?"
  806. } else {
  807. sql = "update cm_tree set quality_rectification = quality_rectification_in + 1 and quality_rectification_in = if(quality_rectification_in >= 1, quality_rectification_in - 1, 0) where bidsection_id = ?"
  808. }
  809. _, err = session.Exec(sql, auditor.BidsectionId)
  810. if err != nil {
  811. session.Rollback()
  812. return err
  813. }
  814. }
  815. err = session.Commit()
  816. return err
  817. }
  818. func (d *ApproverDao) CloseHandler(auditType string, id int, opinion string, curUid int, saveId int) error {
  819. session := d.engine.NewSession()
  820. defer session.Close()
  821. // add Begin() before any action
  822. err := session.Begin()
  823. auditor, err := d.FindApproverById(id)
  824. if auditor.AuditId != curUid {
  825. return errors.New("该用户没有审批权限!")
  826. }
  827. // 消息推送,先删除旧的推送
  828. _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
  829. if err != nil {
  830. session.Rollback()
  831. return err
  832. }
  833. approvers := make([]models.CmApprover, 0)
  834. err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
  835. if err != nil {
  836. session.Rollback()
  837. return err
  838. }
  839. if auditType == "safe" {
  840. safe := &models.CmSafe{}
  841. _, err = session.ID(saveId).Get(safe)
  842. if err != nil {
  843. session.Rollback()
  844. return err
  845. }
  846. account := &models.CmProjectAccount{Id: safe.Uid}
  847. _, err = d.engine.Get(account)
  848. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  849. msg := make([]models.CmProjectMessage, 0)
  850. for _, item := range approvers {
  851. msgVM := models.CmProjectMessage{}
  852. msgVM.AccountId = item.AuditId
  853. msgVM.BidsectionId = item.BidsectionId
  854. msgVM.ProjectId = item.ProjectId
  855. msgVM.CreateTime = time.Now()
  856. msgVM.DataId = item.DataId
  857. msgVM.DataType = item.DataType
  858. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", safe.Code, safe.InspectionDetail)
  859. msgVM.Title = title
  860. msg = append(msg, msgVM)
  861. }
  862. // 添加巡检拥有者的推送
  863. msgVM := models.CmProjectMessage{}
  864. msgVM.AccountId = safe.Uid
  865. msgVM.BidsectionId = safe.BidsectionId
  866. msgVM.ProjectId = safe.ProjectId
  867. msgVM.CreateTime = time.Now()
  868. msgVM.DataId = safe.Id
  869. msgVM.DataType = 1
  870. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", safe.Code, safe.InspectionDetail)
  871. msgVM.Title = title
  872. msg = append(msg, msgVM)
  873. // 插入消息
  874. _, err = session.Insert(msg)
  875. if err != nil {
  876. session.Rollback()
  877. return err
  878. }
  879. // 增加审批日志
  880. safeAudit := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, Times: safe.Times, AuditId: auditor.AuditId, Status: 2, Progress: auditor.Progress, CreateTime: time.Now(), Opinion: opinion}
  881. _, err = session.Insert(safeAudit)
  882. if err != nil {
  883. session.Rollback()
  884. return err
  885. }
  886. // 更改cm_safe的记录
  887. _, err = session.Exec("update `cm_safe` set status = ? where id = ? ", 5, saveId)
  888. if err != nil {
  889. session.Rollback()
  890. return err
  891. }
  892. } else {
  893. quality := &models.CmQuality{}
  894. _, err = session.ID(saveId).Get(quality)
  895. if err != nil {
  896. session.Rollback()
  897. return err
  898. }
  899. account := &models.CmProjectAccount{Id: quality.Uid}
  900. _, err = d.engine.Get(account)
  901. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  902. msg := make([]models.CmProjectMessage, 0)
  903. for _, item := range approvers {
  904. msgVM := models.CmProjectMessage{}
  905. msgVM.AccountId = item.AuditId
  906. msgVM.BidsectionId = item.BidsectionId
  907. msgVM.ProjectId = item.ProjectId
  908. msgVM.CreateTime = time.Now()
  909. msgVM.DataId = item.DataId
  910. msgVM.DataType = item.DataType
  911. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", quality.Code, quality.InspectionDetail)
  912. msgVM.Title = title
  913. msg = append(msg, msgVM)
  914. }
  915. // 添加巡检拥有者的推送
  916. msgVM := models.CmProjectMessage{}
  917. msgVM.AccountId = quality.Uid
  918. msgVM.BidsectionId = quality.BidsectionId
  919. msgVM.ProjectId = quality.ProjectId
  920. msgVM.CreateTime = time.Now()
  921. msgVM.DataId = quality.Id
  922. msgVM.DataType = 2
  923. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", quality.Code, quality.InspectionDetail)
  924. msgVM.Title = title
  925. msg = append(msg, msgVM)
  926. // 插入消息
  927. _, err = session.Insert(msg)
  928. if err != nil {
  929. session.Rollback()
  930. return err
  931. }
  932. // 增加审批日志
  933. qualityAudit := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, Times: quality.Times, AuditId: auditor.AuditId, Status: 2, Progress: auditor.Progress, CreateTime: time.Now(), Opinion: opinion}
  934. _, err = session.Insert(qualityAudit)
  935. if err != nil {
  936. session.Rollback()
  937. return err
  938. }
  939. // 更改cm_quality的记录
  940. _, err = session.Exec("update `cm_quality` set status = ? where id = ? ", 5, saveId)
  941. if err != nil {
  942. session.Rollback()
  943. return err
  944. }
  945. }
  946. // 更改cm_approver的记录
  947. _, err = session.Exec("update `cm_approver` set status = ? where id = ? ", 3, id)
  948. if err != nil {
  949. session.Rollback()
  950. return err
  951. }
  952. err = session.Commit()
  953. return err
  954. }
  955. // 删除旧的审批流程
  956. func (d *ApproverDao) DeleteOldAuditors(bid int, dataType int, dataId int) error {
  957. data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
  958. _, err := d.engine.Delete(data)
  959. return err
  960. }
  961. func (d *ApproverDao) GetStatusByProjectAndAccount(projectId int, projectAccountId int, status int) []models.CmApprover {
  962. datalist := make([]models.CmApprover, 0)
  963. _ = d.engine.
  964. Where("project_id = ? and audit_id= ? and status=? ", projectId, projectAccountId, status).
  965. Desc("id").
  966. Find(&datalist)
  967. return datalist
  968. }