safe_service.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package services
  2. import (
  3. "fmt"
  4. "log"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/kataras/iris/v12"
  9. "github.com/shopspring/decimal"
  10. "go.mod/comm"
  11. "go.mod/conf"
  12. "go.mod/dao"
  13. "go.mod/datasource"
  14. "go.mod/models"
  15. "go.mod/web/viewmodels"
  16. )
  17. type SafeService interface {
  18. Get(id int, pid int, pageNo int, pageSize int) ([]viewmodels.SafeList, int64)
  19. Post(data models.CmSafe) error
  20. Del(id int) error
  21. GetDetail(id int, pid int) viewmodels.SafeDetail
  22. GetSurvey(projectId int, bidsectionId int) map[string]interface{}
  23. GetPending(projectId int, projectAccountId int) []viewmodels.ApproverMessage
  24. ValidRule(ctx iris.Context) (viewmodels.Safe, error)
  25. }
  26. // //返回service操作类
  27. type safeService struct {
  28. daoSafe *dao.SafeDao
  29. daoQuality *dao.QualityDao
  30. daoSafeAudit *dao.SafeAuditDao
  31. daoProjectAccount *dao.ProjectAccountDao
  32. daoAnnex *dao.AnnexDao
  33. daoRule *dao.RuleDao
  34. daoApprover *dao.ApproverDao
  35. daoTree *dao.TreeDao
  36. daoBidsection *dao.BidsectionDao
  37. validDetail string
  38. }
  39. //创建项目用户service
  40. func NewSafeService() SafeService {
  41. return &safeService{
  42. validDetail: "/api/safe/detail",
  43. daoSafe: dao.NewSafeDao(datasource.InstanceDbMaster()),
  44. daoQuality: dao.NewQualityDao(datasource.InstanceDbMaster()),
  45. daoAnnex: dao.NewAnnexDao(datasource.InstanceDbMaster()),
  46. daoSafeAudit: dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
  47. daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
  48. daoApprover: dao.NewApproverDao(datasource.InstanceDbMaster()),
  49. daoTree: dao.NewTreeDao(datasource.InstanceDbMaster()),
  50. daoBidsection: dao.NewBidsectionDao(datasource.InstanceDbMaster()),
  51. }
  52. }
  53. func (s *safeService) Get(id int, pid int, pageNo int, pageSize int) ([]viewmodels.SafeList, int64) {
  54. datalist, total := s.daoSafe.GetListByBid(id, pageNo, pageSize)
  55. safeList := make([]viewmodels.SafeList, 0)
  56. for _, item := range datalist {
  57. safeVM := viewmodels.SafeList{}
  58. safeVM.Code = item.Code
  59. account := s.daoProjectAccount.Get(item.Uid, pid)
  60. safeVM.AuditName = account.Name
  61. safeVM.CreateTime = item.CreateTime.Format(conf.SysTimeform)
  62. safeVM.Demand = item.Demand
  63. id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)
  64. safeVM.Id = id
  65. safeVM.Inspection = item.Inspection
  66. safeVM.InspectionDetail = item.InspectionDetail
  67. safeVM.Position = item.Position
  68. safeVM.Status = item.Status
  69. counts, _ := s.daoAnnex.GetCount(3, item.Id)
  70. safeVM.FileCounts = counts
  71. safeList = append(safeList, safeVM)
  72. }
  73. return safeList, total
  74. }
  75. // post请求,插入单条数据
  76. func (s *safeService) Post(data models.CmSafe) error {
  77. // has, err := s.daoSafe.FindByCode(data.Code)
  78. // if err != nil {
  79. // return err
  80. // }
  81. // if has {
  82. // return errors.New("该编号已存在!")
  83. // }
  84. // _, err = s.daoSafe.InsertRecord(data)
  85. // if err != nil {
  86. // return err
  87. // }
  88. // counts, err := s.daoSafe.GetCountsByBid(data.BidsectionId, data.Status, true)
  89. // err = s.daoTree.UpdateCounts("safe_total", counts, data.BidsectionId)
  90. // if err != nil {
  91. // return err
  92. // }
  93. // return nil
  94. err := s.daoSafe.CreateSafe(data)
  95. return err
  96. }
  97. // delete请求,删除数据
  98. func (s *safeService) Del(id int) error {
  99. err := s.daoSafe.DeleteRecord(id)
  100. return err
  101. }
  102. // 详情页数据拼装
  103. func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {
  104. safeData := s.daoSafe.FindById(id)
  105. // fmt.Println(safeData)
  106. safeId, _ := comm.AesEncrypt(strconv.Itoa(safeData.Id), conf.SignSecret)
  107. bid, _ := comm.AesEncrypt(strconv.Itoa(safeData.BidsectionId), conf.SignSecret)
  108. uid, _ := comm.AesEncrypt(strconv.Itoa(safeData.Uid), conf.SignSecret)
  109. data := viewmodels.SafeDetail{}
  110. data.Id = safeId
  111. data.BidsectionId = bid
  112. data.Uid = uid
  113. data.Code = safeData.Code
  114. data.Inspection = safeData.Inspection
  115. data.InspectionDetail = safeData.InspectionDetail
  116. data.Demand = safeData.Demand
  117. account := s.daoProjectAccount.Get(safeData.Uid, pid)
  118. data.AuditName = account.Name
  119. data.CreateTime = safeData.CreateTime
  120. data.Times = safeData.Times
  121. data.Status = safeData.Status
  122. fileList, total := s.daoAnnex.GetList(3, safeData.Id, 1, conf.PageSize)
  123. // 加密id
  124. fileArr := make([]viewmodels.AnnexListView, 0)
  125. for _, item := range fileList {
  126. fileListVM := viewmodels.AnnexListView{}
  127. fileId, _ := comm.AesEncrypt(item.Id, conf.SignSecret)
  128. fileListVM.Id = fileId
  129. uid, _ := comm.AesEncrypt(item.AccountId, conf.SignSecret)
  130. fileListVM.AccountId = uid
  131. fileListVM.AccountName = item.AccountName
  132. fileListVM.CreateTime = item.CreateTime
  133. fileListVM.FileName = item.FileName
  134. fileListVM.FilePath = item.FilePath
  135. fileArr = append(fileArr, fileListVM)
  136. }
  137. fileVM := viewmodels.FileStruct{}
  138. fileVM.FileList = fileArr
  139. fileVM.Total = total
  140. data.File = fileVM
  141. auditors := s.daoApprover.GetAuditorsWithOwner(safeData.BidsectionId, int(1), safeData.Id, account.Id)
  142. encryptAuditors := make([]viewmodels.Auditors, 0)
  143. for _, item := range auditors {
  144. auditorVM := viewmodels.Auditors{}
  145. // if item.Id != "" {
  146. // }
  147. id, _ := comm.AesEncrypt(item.Id, conf.SignSecret)
  148. auditorVM.Id = id
  149. auditId, _ := comm.AesEncrypt(item.AuditId, conf.SignSecret)
  150. auditorVM.AuditId = auditId
  151. auditorVM.Name = item.Name
  152. auditorVM.Position = item.Position
  153. auditorVM.Mobile = item.Mobile
  154. auditorVM.AuditOrder = item.AuditOrder
  155. auditorVM.AccountGroup = item.AccountGroup
  156. auditorVM.Progress = item.Progress
  157. auditorVM.Company = item.Company
  158. auditorVM.Status = item.Status
  159. encryptAuditors = append(encryptAuditors, auditorVM)
  160. }
  161. auditHistory := s.daoSafeAudit.GetAuditHistory(safeData.Id, safeData.Times)
  162. data.AuditHistory = auditHistory
  163. // 整改单
  164. rectifiedInfo, _ := s.daoSafeAudit.GetRectifications(safeData.BidsectionId, safeData.ProjectId, safeData.Id, 1)
  165. data.RectifiedInfo = rectifiedInfo
  166. // 最新审批人信息
  167. latestAuditor := s.daoApprover.GetLastedAuditor(safeData.BidsectionId, 1, safeData.Id)
  168. data.LatestdAuditor = latestAuditor
  169. data.Auditors = encryptAuditors
  170. return data
  171. }
  172. // 安全概况
  173. func (s *safeService) GetSurvey(projectId int, bidsectionId int) map[string]interface{} {
  174. // 1.获得安全巡检
  175. year := time.Now().Year()
  176. safeList := s.daoSafe.GetTypeYear(bidsectionId, year)
  177. // 2.初始化
  178. rectifylist := make([]viewmodels.SafeSurveyList, 0)
  179. rectifyTotal := 0
  180. approvalTotal := 0
  181. rectifyedTotal := 0
  182. columnarData := make([]map[string]interface{}, 0)
  183. lineData := columnarData
  184. for i := 1; i <= 12; i++ {
  185. item := map[string]interface{}{
  186. "name": "rectifyed",
  187. "month": fmt.Sprintf("%d-%02d", year, i),
  188. "count": 0,
  189. }
  190. columnarData = append(columnarData, item)
  191. item = map[string]interface{}{
  192. "name": "submit",
  193. "month": fmt.Sprintf("%d-%02d", year, i),
  194. "count": 0,
  195. }
  196. columnarData = append(columnarData, item)
  197. item = map[string]interface{}{
  198. "month": fmt.Sprintf("%d-%02d", year, i),
  199. "percentage": 0,
  200. }
  201. lineData = append(lineData, item)
  202. }
  203. // 3.当年数据初始化
  204. submitData := map[string]int{
  205. fmt.Sprintf("%d-01", year): 0,
  206. fmt.Sprintf("%d-02", year): 0,
  207. fmt.Sprintf("%d-03", year): 0,
  208. fmt.Sprintf("%d-04", year): 0,
  209. fmt.Sprintf("%d-05", year): 0,
  210. fmt.Sprintf("%d-06", year): 0,
  211. fmt.Sprintf("%d-07", year): 0,
  212. fmt.Sprintf("%d-08", year): 0,
  213. fmt.Sprintf("%d-09", year): 0,
  214. fmt.Sprintf("%d-10", year): 0,
  215. fmt.Sprintf("%d-11", year): 0,
  216. fmt.Sprintf("%d-12", year): 0,
  217. }
  218. rectifyedData := map[string]int{
  219. fmt.Sprintf("%d-01", year): 0,
  220. fmt.Sprintf("%d-02", year): 0,
  221. fmt.Sprintf("%d-03", year): 0,
  222. fmt.Sprintf("%d-04", year): 0,
  223. fmt.Sprintf("%d-05", year): 0,
  224. fmt.Sprintf("%d-06", year): 0,
  225. fmt.Sprintf("%d-07", year): 0,
  226. fmt.Sprintf("%d-08", year): 0,
  227. fmt.Sprintf("%d-09", year): 0,
  228. fmt.Sprintf("%d-10", year): 0,
  229. fmt.Sprintf("%d-11", year): 0,
  230. fmt.Sprintf("%d-12", year): 0,
  231. }
  232. for _, item := range safeList {
  233. if item.Status == 2 {
  234. id, _ := comm.AesEncrypt(item.Id, conf.SignSecret)
  235. item.Id = id
  236. rectifylist = append(rectifylist, item)
  237. rectifyTotal++
  238. }
  239. // if item.Status == 1 {
  240. // approvalTotal++
  241. // }
  242. approvalTotal++
  243. if item.Status == 4 {
  244. rectifyedTotal++
  245. }
  246. // for index, columnar := range columnarData {
  247. // rectifyedCount := 0
  248. // if columnar["month"] == item.CreateTime.Format(conf.SysTimeformMonth) {
  249. // if item.Status == 4 && columnar["name"] == "rectifyed" {
  250. // rectifyedCount++
  251. // }
  252. // columnarData[index]["count"] = columnarData[index]["count"].(int) + 1
  253. // columnarData[index]["count"] = rectifyedCount
  254. // // if item.Status == 0 && columnar["name"] == "submit" {
  255. // // }
  256. // }
  257. // }
  258. index := item.UpdateTime.Format(conf.SysTimeformMonth)
  259. submitData[index] = submitData[index] + 1
  260. // if item.Status == 0 {
  261. // submitData[item.CreateTime.Format(conf.SysTimeformMonth)] = submitData[item.CreateTime.Format(conf.SysTimeformMonth)] + 1
  262. // }
  263. if item.Status == 4 {
  264. rectifyedData[index] = rectifyedData[index] + 1
  265. }
  266. }
  267. for index, columnar := range columnarData {
  268. if columnar["name"] == "rectifyed" {
  269. columnarData[index]["count"] = rectifyedData[columnar["month"].(string)]
  270. }
  271. if columnar["name"] == "submit" {
  272. columnarData[index]["count"] = submitData[columnar["month"].(string)]
  273. }
  274. }
  275. for index, line := range lineData {
  276. rectifyedCount := 0
  277. submitCount := 0
  278. for _, columnar := range columnarData {
  279. if line["month"] == columnar["month"] {
  280. if columnar["name"] == "rectifyed" {
  281. rectifyedCount = columnar["count"].(int)
  282. }
  283. if columnar["name"] == "submit" {
  284. submitCount = columnar["count"].(int)
  285. }
  286. }
  287. }
  288. lineData[index]["percentage"] = 0.00
  289. if rectifyedCount != 0 && submitCount != 0 {
  290. decimal.DivisionPrecision = 2
  291. percentage, _ := decimal.NewFromFloat(float64(rectifyedCount)).Div(decimal.NewFromFloat(float64(submitCount))).Float64()
  292. lineData[index]["percentage"] = percentage * 100
  293. }
  294. }
  295. // 整改占总数比例 - 完成整改/提交巡检
  296. surveryData := map[string]interface{}{
  297. "rectifylist": rectifylist,
  298. "rectifyTotal": rectifyTotal,
  299. "approvalTotal": approvalTotal,
  300. "rectifyedTotal": rectifyedTotal,
  301. "columnarData": columnarData,
  302. "lineData": lineData,
  303. // "submitData": submitData,
  304. // "rectifyedData": rectifyedData,
  305. }
  306. return surveryData
  307. }
  308. // 获得账号下需要审批的巡检
  309. func (s *safeService) GetPending(projectId int, projectAccountId int) []viewmodels.ApproverMessage {
  310. // 1.获得审批列表
  311. approverData := s.daoApprover.GetStatusByProjectAndAccount(projectId, projectAccountId, 1)
  312. // 2.构建数据ID
  313. safeIds := []string{}
  314. qualityIds := []string{}
  315. for _, item := range approverData {
  316. if item.DataType == 1 {
  317. safeIds = append(safeIds, strconv.Itoa(item.DataId))
  318. } else if item.DataType == 2 {
  319. qualityIds = append(qualityIds, strconv.Itoa(item.DataId))
  320. }
  321. }
  322. safeInId := strings.Join(safeIds, ",")
  323. qualityInId := strings.Join(qualityIds, ",")
  324. safeList := make([]viewmodels.SafeList, 0)
  325. qualityList := make([]viewmodels.QualityList, 0)
  326. if safeInId != "" {
  327. safeList = s.daoSafe.GetInIdJoinAccount(safeInId)
  328. }
  329. if qualityInId != "" {
  330. qualityList = s.daoQuality.GetInIdJoinAccount(qualityInId)
  331. }
  332. // data := s.daoSafe.GetStatusByProjectAndAccount(projectId, projectAccountId, 1)
  333. list := make([]viewmodels.ApproverMessage, 0)
  334. for _, item := range approverData {
  335. approverVM := viewmodels.ApproverMessage{}
  336. Id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)
  337. // BidsectionId, _ := comm.AesEncrypt(strconv.Itoa(item.BidsectionId), conf.SignSecret)
  338. // AuditId, _ := comm.AesEncrypt(item.AuditId, conf.SignSecret)
  339. // ProjectId, _ := comm.AesEncrypt(strconv.Itoa(item.ProjectId), conf.SignSecret)
  340. DataId, _ := comm.AesEncrypt(strconv.Itoa(item.DataId), conf.SignSecret)
  341. // ProjectId, _ := comm.AesEncrypt(strconv.Itoa(item.ProjectId), conf.SignSecret)
  342. approverVM.Id = Id
  343. // approverVM.ProjectId = ProjectId
  344. // approverVM.BidsectionId = BidsectionId
  345. approverVM.DataType = item.DataType
  346. approverVM.DataId = DataId
  347. approverVM.Status = item.Status
  348. dataIdString := strconv.Itoa(item.DataId)
  349. // 安全巡检相关
  350. if item.DataType == 1 {
  351. for _, data := range safeList {
  352. if dataIdString == data.Id {
  353. bidSectionDetail := s.daoBidsection.Get(data.BidsectionId, projectId)
  354. approverVM.Code = data.Code
  355. approverVM.InspectionDetail = data.InspectionDetail
  356. approverVM.Name = data.AuditName
  357. approverVM.Position = data.Position
  358. approverVM.Inspection = data.Inspection
  359. approverVM.BidsectionName = bidSectionDetail.Name
  360. approverVM.CreateTime = data.CreateTime
  361. approverVM.UpdateTime = item.UpdateTime
  362. break
  363. }
  364. }
  365. } else if item.DataType == 2 { // 质量巡检
  366. for _, data := range qualityList {
  367. if dataIdString == data.Id {
  368. bidSectionDetail := s.daoBidsection.Get(data.BidsectionId, projectId)
  369. approverVM.Code = data.Code
  370. approverVM.InspectionDetail = data.InspectionDetail
  371. approverVM.Name = data.AuditName
  372. approverVM.Position = data.Position
  373. approverVM.Inspection = data.Inspection
  374. approverVM.BidsectionName = bidSectionDetail.Name
  375. approverVM.CreateTime = data.CreateTime
  376. approverVM.UpdateTime = item.UpdateTime
  377. break
  378. }
  379. }
  380. }
  381. list = append(list, approverVM)
  382. }
  383. return list
  384. }
  385. // 规则校验
  386. func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
  387. safeVaild := viewmodels.Safe{}
  388. // fmt.Println("---------------------------safeVaild", safeVaild)
  389. if ctx.Method() == "GET" {
  390. err := ctx.ReadForm(&safeVaild)
  391. if err != nil {
  392. log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
  393. return safeVaild, err
  394. }
  395. if ctx.Path() == s.validDetail {
  396. // 一样要传id,所以用delete的方法判断
  397. err = safeVaild.ValidateDelete()
  398. } else {
  399. err = safeVaild.ValidateList()
  400. }
  401. return safeVaild, err
  402. }
  403. if ctx.Method() == "POST" {
  404. err := ctx.ReadJSON(&safeVaild)
  405. if err != nil {
  406. log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
  407. return safeVaild, err
  408. }
  409. err = safeVaild.ValidateCreate()
  410. return safeVaild, err
  411. // if ctx.Path() == s.validCreate {
  412. // }
  413. // if ctx.Path() == s.validFile {
  414. // err = safeVaild.ValidateFile()
  415. // return safeVaild, err
  416. // }
  417. }
  418. if ctx.Method() == "PUT" {
  419. err := ctx.ReadForm(&safeVaild)
  420. if err != nil {
  421. log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
  422. return safeVaild, err
  423. }
  424. err = safeVaild.ValidateDelete()
  425. return safeVaild, err
  426. }
  427. return safeVaild, nil
  428. }