safe_service.go 11 KB

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