safe_service.go 13 KB

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