approver_dao.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. create_time := time.Now().Format(conf.SysTimeform)
  452. _, err = session.Exec("update `cm_approver` set `audit_id` = ? , `create_time` = ? where `bidsection_id` = ? and `project_id` = ? and `data_type` = ? and `data_id` = ? and `audit_order` = ? limit 1",
  453. auditId, create_time, auditor.BidsectionId, auditor.ProjectId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1)
  454. if err != nil {
  455. session.Rollback()
  456. return err
  457. }
  458. } else {
  459. if auditor.Progress == 1 {
  460. // 整改人审批流程
  461. // 修改cm_safe表的status状态为待复查
  462. _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 3, auditor.DataId)
  463. if err != nil {
  464. session.Rollback()
  465. return err
  466. }
  467. newRectifiedTime, _ := time.Parse(conf.SysTimeform, rectifiedTime)
  468. // 将整改单的状态->完成,并且填入整改日期
  469. rectification := &models.CmRectification{
  470. BidsectionId: safe.BidsectionId,
  471. ProjectId: safe.ProjectId,
  472. DataId: safe.Id,
  473. DataType: 1,
  474. AccountId: auditor.AuditId,
  475. RectifiedTime: newRectifiedTime,
  476. CreateTime: time.Now(),
  477. Content: content,
  478. Status: 1,
  479. UpdateTime: time.Now(),
  480. }
  481. _, err = session.Insert(rectification)
  482. if err != nil {
  483. session.Rollback()
  484. return err
  485. }
  486. } else {
  487. // 复查流程
  488. // 查找最后一个审批人
  489. lastAuditor := &models.CmApprover{BidsectionId: auditor.BidsectionId, DataType: auditor.DataType, DataId: auditor.DataId}
  490. _, err = session.Desc("audit_order").Limit(1).Get(lastAuditor)
  491. if err != nil {
  492. session.Rollback()
  493. return err
  494. }
  495. if lastAuditor.Id == auditor.Id {
  496. // 说明审批流程已经走完
  497. _, err = session.Exec("update `cm_safe` set status = ? where id = ?", 4, auditor.DataId)
  498. if err != nil {
  499. session.Rollback()
  500. return err
  501. }
  502. // 将cm_tree的safe_rectification_finish待整改+1
  503. _, 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)
  504. if err != nil {
  505. session.Rollback()
  506. return err
  507. }
  508. }
  509. }
  510. }
  511. } else {
  512. // 质量巡检
  513. quality := &models.CmQuality{}
  514. _, err = session.ID(auditor.DataId).Get(quality)
  515. if err != nil {
  516. session.Rollback()
  517. return err
  518. }
  519. account := &models.CmProjectAccount{Id: quality.Uid}
  520. _, err = d.engine.Get(account)
  521. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  522. msg := make([]models.CmProjectMessage, 0)
  523. for _, item := range approvers {
  524. msgVM := models.CmProjectMessage{}
  525. msgVM.AccountId = item.AuditId
  526. msgVM.BidsectionId = item.BidsectionId
  527. msgVM.ProjectId = item.ProjectId
  528. msgVM.CreateTime = time.Now()
  529. msgVM.DataId = item.DataId
  530. msgVM.DataType = item.DataType
  531. msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, quality.Code, quality.InspectionDetail)
  532. msgVM.Title = title
  533. msg = append(msg, msgVM)
  534. }
  535. // 添加巡检拥有者的推送
  536. msgVM := models.CmProjectMessage{}
  537. msgVM.AccountId = quality.Uid
  538. msgVM.BidsectionId = quality.BidsectionId
  539. msgVM.ProjectId = quality.ProjectId
  540. msgVM.CreateTime = time.Now()
  541. msgVM.DataId = quality.Id
  542. msgVM.DataType = 1
  543. msgVM.Content = fmt.Sprintf("%s|%s|%s", msgContent, quality.Code, quality.InspectionDetail)
  544. msgVM.Title = title
  545. msg = append(msg, msgVM)
  546. // 插入消息
  547. _, err = session.Insert(msg)
  548. if err != nil {
  549. session.Rollback()
  550. return err
  551. }
  552. // 增加审批日志
  553. 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}
  554. _, err = session.Insert(auditReacord)
  555. if err != nil {
  556. session.Rollback()
  557. return err
  558. }
  559. // 改变审批流程中当前审批人以及下一个审批人的审批流程状态
  560. curData := &models.CmApprover{Status: 2}
  561. _, err = session.ID(id).Cols("status").Update(curData)
  562. if err != nil {
  563. session.Rollback()
  564. return err
  565. }
  566. nextData := &models.CmApprover{Status: 1}
  567. _, 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)
  568. if err != nil {
  569. session.Rollback()
  570. return err
  571. }
  572. if auditId != 0 {
  573. // 审批人选择了整改人
  574. // 修改cm_quality表的status状态为待整改
  575. _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 2, auditor.DataId)
  576. if err != nil {
  577. session.Rollback()
  578. return err
  579. }
  580. // 将cm_tree的quality_rectification待整改+1
  581. _, 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)
  582. if err != nil {
  583. session.Rollback()
  584. return err
  585. }
  586. // 改变审批流程中的整改人id
  587. create_time := time.Now().Format(conf.SysTimeform)
  588. _, err = session.Exec("update `cm_approver` set `audit_id` = ?, `create_time` = ? where `bidsection_id` = ? and `project_id` = ? and `data_type` = ? and `data_id` = ? and `audit_order` = ? limit 1", auditId, create_time, auditor.BidsectionId, auditor.ProjectId, auditor.DataType, auditor.DataId, auditor.AuditOrder+1)
  589. if err != nil {
  590. session.Rollback()
  591. return err
  592. }
  593. } else {
  594. if auditor.Progress == 1 {
  595. // 整改人审批流程
  596. // 修改cm_quality表的status状态为待复查
  597. _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 3, auditor.DataId)
  598. if err != nil {
  599. session.Rollback()
  600. return err
  601. }
  602. // 将整改单的状态->完成,并且填入整改日期
  603. newRectifiedTime, err := time.Parse(conf.SysTimeform, rectifiedTime)
  604. if err != nil {
  605. session.Rollback()
  606. return err
  607. }
  608. rectification := &models.CmRectification{
  609. BidsectionId: quality.BidsectionId,
  610. ProjectId: quality.ProjectId,
  611. DataId: quality.Id,
  612. DataType: 2,
  613. AccountId: auditor.AuditId,
  614. CreateTime: time.Now(),
  615. RectifiedTime: newRectifiedTime,
  616. Content: content,
  617. Status: 1,
  618. UpdateTime: time.Now(),
  619. }
  620. _, err = session.Insert(rectification)
  621. if err != nil {
  622. session.Rollback()
  623. return err
  624. }
  625. } else {
  626. // 复查流程
  627. // 查找最后一个审批人
  628. lastAuditor := &models.CmApprover{BidsectionId: auditor.BidsectionId, DataType: auditor.DataType, DataId: auditor.DataId}
  629. _, err = session.Desc("audit_order").Limit(1).Get(lastAuditor)
  630. if err != nil {
  631. session.Rollback()
  632. return err
  633. }
  634. if lastAuditor.Id == auditor.Id {
  635. // 说明审批流程已经走完
  636. _, err = session.Exec("update `cm_quality` set status = ? where id = ?", 4, auditor.DataId)
  637. if err != nil {
  638. session.Rollback()
  639. return err
  640. }
  641. // 将cm_tree的quality_rectification_finish待整改+1
  642. _, 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)
  643. if err != nil {
  644. session.Rollback()
  645. return err
  646. }
  647. }
  648. }
  649. }
  650. }
  651. err = session.Commit()
  652. return err
  653. }
  654. // 审批流程-退回
  655. func (d *ApproverDao) BackHandlerWithId(auditType string, id int, uid int, times int, progress int, opinion string) error {
  656. session := d.engine.NewSession()
  657. defer session.Close()
  658. err := session.Begin()
  659. auditor := &models.CmApprover{}
  660. _, err = session.ID(id).Get(auditor)
  661. // 更改safe/quality表的记录
  662. data := &models.CmApprover{Status: 0}
  663. // 将往后的所有记录的status改为0
  664. _, 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)
  665. if err != nil {
  666. session.Rollback()
  667. return err
  668. }
  669. // 获取改账号的信息
  670. account := &models.CmProjectAccount{Id: auditor.AuditId}
  671. _, err = session.Get(account)
  672. if err != nil {
  673. session.Rollback()
  674. return err
  675. }
  676. // 将当前的记录改为1-待审批
  677. data.Status = 1
  678. _, err = session.ID(id).Cols("status").Update(data)
  679. if err != nil {
  680. session.Rollback()
  681. return err
  682. }
  683. // 消息推送,先删除旧的推送
  684. _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
  685. if err != nil {
  686. session.Rollback()
  687. return err
  688. }
  689. approvers := make([]models.CmApprover, 0)
  690. err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
  691. if err != nil {
  692. session.Rollback()
  693. return err
  694. }
  695. // 增加审批日志
  696. if auditType == "safe" {
  697. safe := &models.CmSafe{}
  698. _, err = session.ID(auditor.DataId).Get(safe)
  699. if err != nil {
  700. session.Rollback()
  701. return err
  702. }
  703. safe.Status = auditor.Progress + 1
  704. _, err = session.Where("id = ? and bidsection_id = ?", safe.Id, safe.BidsectionId).Limit(1).Update(safe)
  705. if err != nil {
  706. session.Rollback()
  707. return err
  708. }
  709. account := &models.CmProjectAccount{Id: safe.Uid}
  710. _, err = d.engine.Get(account)
  711. if err != nil {
  712. session.Rollback()
  713. return err
  714. }
  715. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  716. msg := make([]models.CmProjectMessage, 0)
  717. for _, item := range approvers {
  718. msgVM := models.CmProjectMessage{}
  719. msgVM.AccountId = item.AuditId
  720. msgVM.BidsectionId = item.BidsectionId
  721. msgVM.ProjectId = item.ProjectId
  722. msgVM.CreateTime = time.Now()
  723. msgVM.DataId = item.DataId
  724. msgVM.DataType = item.DataType
  725. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", safe.Code, safe.InspectionDetail, account.Name)
  726. msgVM.Title = title
  727. msg = append(msg, msgVM)
  728. }
  729. // 添加巡检拥有者的推送
  730. msgVM := models.CmProjectMessage{}
  731. msgVM.AccountId = safe.Uid
  732. msgVM.BidsectionId = safe.BidsectionId
  733. msgVM.ProjectId = safe.ProjectId
  734. msgVM.CreateTime = time.Now()
  735. msgVM.DataId = safe.Id
  736. msgVM.DataType = 1
  737. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", safe.Code, safe.InspectionDetail, account.Name)
  738. msg = append(msg, msgVM)
  739. // 插入消息
  740. _, err = session.Insert(msg)
  741. if err != nil {
  742. session.Rollback()
  743. return err
  744. }
  745. safeAudit := &models.CmSafeAudit{BidsectionId: auditor.BidsectionId, SafeId: auditor.DataId, Times: times, AuditId: uid, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
  746. _, err = session.Insert(safeAudit)
  747. if err != nil {
  748. session.Rollback()
  749. return err
  750. }
  751. } else {
  752. quality := &models.CmQuality{}
  753. _, err = session.ID(auditor.DataId).Get(quality)
  754. if err != nil {
  755. session.Rollback()
  756. return err
  757. }
  758. quality.Status = auditor.Progress + 1
  759. _, err = session.Where("id =? and bidsection_id = ?", quality.Id, quality.BidsectionId).Limit(1).Update(quality)
  760. if err != nil {
  761. session.Rollback()
  762. return err
  763. }
  764. account := &models.CmProjectAccount{Id: quality.Uid}
  765. _, err = d.engine.Get(account)
  766. if err != nil {
  767. session.Rollback()
  768. return err
  769. }
  770. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  771. msg := make([]models.CmProjectMessage, 0)
  772. for _, item := range approvers {
  773. msgVM := models.CmProjectMessage{}
  774. msgVM.AccountId = item.AuditId
  775. msgVM.BidsectionId = item.BidsectionId
  776. msgVM.ProjectId = item.ProjectId
  777. msgVM.CreateTime = time.Now()
  778. msgVM.DataId = item.DataId
  779. msgVM.DataType = item.DataType
  780. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", quality.Code, quality.InspectionDetail, account.Name)
  781. msgVM.Title = title
  782. msg = append(msg, msgVM)
  783. }
  784. // 添加巡检拥有者的推送
  785. msgVM := models.CmProjectMessage{}
  786. msgVM.AccountId = quality.Uid
  787. msgVM.BidsectionId = quality.BidsectionId
  788. msgVM.ProjectId = quality.ProjectId
  789. msgVM.CreateTime = time.Now()
  790. msgVM.DataId = quality.Id
  791. msgVM.DataType = 2
  792. msgVM.Content = fmt.Sprintf("%s|%s|审批退回,退回至%s", quality.Code, quality.InspectionDetail, account.Name)
  793. msgVM.Title = title
  794. msg = append(msg, msgVM)
  795. // 插入消息
  796. _, err = session.Insert(msg)
  797. if err != nil {
  798. session.Rollback()
  799. return err
  800. }
  801. qualityAudit := &models.CmQualityAudit{BidsectionId: auditor.BidsectionId, QualityId: auditor.DataId, Times: times, AuditId: uid, Status: 1, Progress: progress, CreateTime: time.Now(), Opinion: opinion}
  802. _, err = session.Insert(qualityAudit)
  803. if err != nil {
  804. session.Rollback()
  805. return err
  806. }
  807. }
  808. curAuditor := &models.CmApprover{}
  809. _, err = session.ID(uid).Get(curAuditor)
  810. if err != nil {
  811. session.Rollback()
  812. return err
  813. }
  814. // 当前审批人是整改或者复查流程的并且退回的是审批流程的
  815. if curAuditor.Progress >= 1 && auditor.Progress == 0 {
  816. var sql string
  817. if auditType == "safe" {
  818. 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 = ?"
  819. } else {
  820. 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 = ?"
  821. }
  822. _, err = session.Exec(sql, auditor.BidsectionId)
  823. if err != nil {
  824. session.Rollback()
  825. return err
  826. }
  827. }
  828. err = session.Commit()
  829. return err
  830. }
  831. func (d *ApproverDao) CloseHandler(auditType string, id int, opinion string, curUid int, saveId int) error {
  832. session := d.engine.NewSession()
  833. defer session.Close()
  834. // add Begin() before any action
  835. err := session.Begin()
  836. auditor, err := d.FindApproverById(id)
  837. if auditor.AuditId != curUid {
  838. return errors.New("该用户没有审批权限!")
  839. }
  840. // 消息推送,先删除旧的推送
  841. _, err = session.Exec("delete from cm_project_message where data_type = ? and data_id = ?", auditor.DataType, auditor.DataId)
  842. if err != nil {
  843. session.Rollback()
  844. return err
  845. }
  846. approvers := make([]models.CmApprover, 0)
  847. err = session.Where("data_type = ? and data_id = ? and audit_id != 0", auditor.DataType, auditor.DataId).Find(&approvers)
  848. if err != nil {
  849. session.Rollback()
  850. return err
  851. }
  852. if auditType == "safe" {
  853. safe := &models.CmSafe{}
  854. _, err = session.ID(saveId).Get(safe)
  855. if err != nil {
  856. session.Rollback()
  857. return err
  858. }
  859. account := &models.CmProjectAccount{Id: safe.Uid}
  860. _, err = d.engine.Get(account)
  861. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  862. msg := make([]models.CmProjectMessage, 0)
  863. for _, item := range approvers {
  864. msgVM := models.CmProjectMessage{}
  865. msgVM.AccountId = item.AuditId
  866. msgVM.BidsectionId = item.BidsectionId
  867. msgVM.ProjectId = item.ProjectId
  868. msgVM.CreateTime = time.Now()
  869. msgVM.DataId = item.DataId
  870. msgVM.DataType = item.DataType
  871. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", safe.Code, safe.InspectionDetail)
  872. msgVM.Title = title
  873. msg = append(msg, msgVM)
  874. }
  875. // 添加巡检拥有者的推送
  876. msgVM := models.CmProjectMessage{}
  877. msgVM.AccountId = safe.Uid
  878. msgVM.BidsectionId = safe.BidsectionId
  879. msgVM.ProjectId = safe.ProjectId
  880. msgVM.CreateTime = time.Now()
  881. msgVM.DataId = safe.Id
  882. msgVM.DataType = 1
  883. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", safe.Code, safe.InspectionDetail)
  884. msgVM.Title = title
  885. msg = append(msg, msgVM)
  886. // 插入消息
  887. _, err = session.Insert(msg)
  888. if err != nil {
  889. session.Rollback()
  890. return err
  891. }
  892. // 增加审批日志
  893. 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}
  894. _, err = session.Insert(safeAudit)
  895. if err != nil {
  896. session.Rollback()
  897. return err
  898. }
  899. // 更改cm_safe的记录
  900. _, err = session.Exec("update `cm_safe` set status = ? where id = ? ", 5, saveId)
  901. if err != nil {
  902. session.Rollback()
  903. return err
  904. }
  905. } else {
  906. quality := &models.CmQuality{}
  907. _, err = session.ID(saveId).Get(quality)
  908. if err != nil {
  909. session.Rollback()
  910. return err
  911. }
  912. account := &models.CmProjectAccount{Id: quality.Uid}
  913. _, err = d.engine.Get(account)
  914. title := fmt.Sprintf("%s - %s", account.Name, account.Position)
  915. msg := make([]models.CmProjectMessage, 0)
  916. for _, item := range approvers {
  917. msgVM := models.CmProjectMessage{}
  918. msgVM.AccountId = item.AuditId
  919. msgVM.BidsectionId = item.BidsectionId
  920. msgVM.ProjectId = item.ProjectId
  921. msgVM.CreateTime = time.Now()
  922. msgVM.DataId = item.DataId
  923. msgVM.DataType = item.DataType
  924. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", quality.Code, quality.InspectionDetail)
  925. msgVM.Title = title
  926. msg = append(msg, msgVM)
  927. }
  928. // 添加巡检拥有者的推送
  929. msgVM := models.CmProjectMessage{}
  930. msgVM.AccountId = quality.Uid
  931. msgVM.BidsectionId = quality.BidsectionId
  932. msgVM.ProjectId = quality.ProjectId
  933. msgVM.CreateTime = time.Now()
  934. msgVM.DataId = quality.Id
  935. msgVM.DataType = 2
  936. msgVM.Content = fmt.Sprintf("%s|%s|已关闭,停止审批流程", quality.Code, quality.InspectionDetail)
  937. msgVM.Title = title
  938. msg = append(msg, msgVM)
  939. // 插入消息
  940. _, err = session.Insert(msg)
  941. if err != nil {
  942. session.Rollback()
  943. return err
  944. }
  945. // 增加审批日志
  946. 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}
  947. _, err = session.Insert(qualityAudit)
  948. if err != nil {
  949. session.Rollback()
  950. return err
  951. }
  952. // 更改cm_quality的记录
  953. _, err = session.Exec("update `cm_quality` set status = ? where id = ? ", 5, saveId)
  954. if err != nil {
  955. session.Rollback()
  956. return err
  957. }
  958. }
  959. // 更改cm_approver的记录
  960. _, err = session.Exec("update `cm_approver` set status = ? where id = ? ", 3, id)
  961. if err != nil {
  962. session.Rollback()
  963. return err
  964. }
  965. err = session.Commit()
  966. return err
  967. }
  968. // 删除旧的审批流程
  969. func (d *ApproverDao) DeleteOldAuditors(bid int, dataType int, dataId int) error {
  970. data := &models.CmApprover{BidsectionId: bid, DataType: dataType, DataId: dataId}
  971. _, err := d.engine.Delete(data)
  972. return err
  973. }
  974. func (d *ApproverDao) GetStatusByProjectAndAccount(projectId int, projectAccountId int, status int) []models.CmApprover {
  975. datalist := make([]models.CmApprover, 0)
  976. _ = d.engine.
  977. Where("project_id = ? and audit_id= ? and status=? ", projectId, projectAccountId, status).
  978. Desc("id").
  979. Find(&datalist)
  980. return datalist
  981. }
  982. func (d *ApproverDao) GetDataIdByProgress(dataId int, dataType int, progress int) *models.CmApprover {
  983. data := &models.CmApprover{}
  984. _, err := d.engine.
  985. Where(" data_id=? and data_type=? and progress=? ", dataId, dataType, progress).
  986. Get(data)
  987. if err != nil {
  988. data.Id = 0
  989. return data
  990. }
  991. return data
  992. }