safe_service.go 10 KB

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