safe_service.go 14 KB

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