approver_dao.go 34 KB

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