| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 | package servicesimport (	"fmt"	"log"	"strconv"	"time"	"github.com/kataras/iris/v12"	"github.com/shopspring/decimal"	"go.mod/comm"	"go.mod/conf"	"go.mod/dao"	"go.mod/datasource"	"go.mod/models"	"go.mod/web/viewmodels")type SafeService interface {	Get(id int, pid int, pageNo int, pageSize int) ([]viewmodels.SafeList, int64)	Post(data models.CmSafe) error	Del(id int) error	GetDetail(id int, pid int) viewmodels.SafeDetail	GetSurvey(projectId int, bidsectionId int) map[string]interface{}	GetPending(projectId int, projectAccountId int) []viewmodels.ApproverMessage	ValidRule(ctx iris.Context) (viewmodels.Safe, error)}// //返回service操作类type safeService struct {	daoSafe           *dao.SafeDao	daoQuality        *dao.QualityDao	daoSafeAudit      *dao.SafeAuditDao	daoProjectAccount *dao.ProjectAccountDao	daoAnnex          *dao.AnnexDao	daoRule           *dao.RuleDao	daoApprover       *dao.ApproverDao	daoTree           *dao.TreeDao	daoBidsection     *dao.BidsectionDao	validDetail       string}//创建项目用户servicefunc NewSafeService() SafeService {	return &safeService{		validDetail:       "/api/safe/detail",		daoSafe:           dao.NewSafeDao(datasource.InstanceDbMaster()),		daoQuality:        dao.NewQualityDao(datasource.InstanceDbMaster()),		daoAnnex:          dao.NewAnnexDao(datasource.InstanceDbMaster()),		daoSafeAudit:      dao.NewSafeAuditDao(datasource.InstanceDbMaster()),		daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),		daoApprover:       dao.NewApproverDao(datasource.InstanceDbMaster()),		daoTree:           dao.NewTreeDao(datasource.InstanceDbMaster()),		daoBidsection:     dao.NewBidsectionDao(datasource.InstanceDbMaster()),	}}func (s *safeService) Get(id int, pid int, pageNo int, pageSize int) ([]viewmodels.SafeList, int64) {	datalist, total := s.daoSafe.GetListByBid(id, pageNo, pageSize)	safeList := make([]viewmodels.SafeList, 0)	for _, item := range datalist {		safeVM := viewmodels.SafeList{}		safeVM.Code = item.Code		account := s.daoProjectAccount.Get(item.Uid, pid)		safeVM.AuditName = account.Name		safeVM.CreateTime = item.CreateTime.Format(conf.SysTimeform)		safeVM.Demand = item.Demand		id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)		safeVM.Id = id		safeVM.Inspection = item.Inspection		safeVM.InspectionDetail = item.InspectionDetail		safeVM.Position = item.Position		safeVM.Status = item.Status		counts, _ := s.daoAnnex.GetCount(3, item.Id)		safeVM.FileCounts = counts		safeList = append(safeList, safeVM)	}	return safeList, total}// post请求,插入单条数据func (s *safeService) Post(data models.CmSafe) error {	// has, err := s.daoSafe.FindByCode(data.Code)	// if err != nil {	// 	return err	// }	// if has {	// 	return errors.New("该编号已存在!")	// }	// _, err = s.daoSafe.InsertRecord(data)	// if err != nil {	// 	return err	// }	// counts, err := s.daoSafe.GetCountsByBid(data.BidsectionId, data.Status, true)	// err = s.daoTree.UpdateCounts("safe_total", counts, data.BidsectionId)	// if err != nil {	// 	return err	// }	// return nil	err := s.daoSafe.CreateSafe(data)	return err}// delete请求,删除数据func (s *safeService) Del(id int) error {	err := s.daoSafe.DeleteRecord(id)	return err}// 详情页数据拼装func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {	safeData := s.daoSafe.FindById(id)	// fmt.Println(safeData)	safeId, _ := comm.AesEncrypt(strconv.Itoa(safeData.Id), conf.SignSecret)	bid, _ := comm.AesEncrypt(strconv.Itoa(safeData.BidsectionId), conf.SignSecret)	uid, _ := comm.AesEncrypt(strconv.Itoa(safeData.Uid), conf.SignSecret)	data := viewmodels.SafeDetail{}	data.Id = safeId	data.BidsectionId = bid	data.Uid = uid	data.Code = safeData.Code	data.Inspection = safeData.Inspection	data.InspectionDetail = safeData.InspectionDetail	data.Demand = safeData.Demand	account := s.daoProjectAccount.Get(safeData.Uid, pid)	data.AuditName = account.Name	data.CreateTime = safeData.CreateTime	data.Times = safeData.Times	data.Status = safeData.Status	fileList, total := s.daoAnnex.GetList(3, safeData.Id, 1, conf.PageSize)	// 加密id	fileArr := make([]viewmodels.AnnexListView, 0)	for _, item := range fileList {		fileListVM := viewmodels.AnnexListView{}		fileId, _ := comm.AesEncrypt(item.Id, conf.SignSecret)		fileListVM.Id = fileId		uid, _ := comm.AesEncrypt(item.AccountId, conf.SignSecret)		fileListVM.AccountId = uid		fileListVM.AccountName = item.AccountName		fileListVM.CreateTime = item.CreateTime		fileListVM.FileName = item.FileName		fileListVM.FilePath = item.FilePath		fileArr = append(fileArr, fileListVM)	}	fileVM := viewmodels.FileStruct{}	fileVM.FileList = fileArr	fileVM.Total = total	data.File = fileVM	auditors := s.daoApprover.GetAuditorsWithOwner(safeData.BidsectionId, int(1), safeData.Id, account.Id)	encryptAuditors := make([]viewmodels.Auditors, 0)	for _, item := range auditors {		auditorVM := viewmodels.Auditors{}		// if item.Id != "" {		// }		id, _ := comm.AesEncrypt(item.Id, conf.SignSecret)		auditorVM.Id = id		auditId, _ := comm.AesEncrypt(item.AuditId, conf.SignSecret)		auditorVM.AuditId = auditId		auditorVM.Name = item.Name		auditorVM.Position = item.Position		auditorVM.Mobile = item.Mobile		auditorVM.AuditOrder = item.AuditOrder		auditorVM.AccountGroup = item.AccountGroup		auditorVM.Progress = item.Progress		auditorVM.Company = item.Company		auditorVM.Status = item.Status		encryptAuditors = append(encryptAuditors, auditorVM)	}	auditHistory := s.daoSafeAudit.GetAuditHistory(safeData.Id, safeData.Times)	data.AuditHistory = auditHistory	// 整改单	rectifiedInfo, _ := s.daoSafeAudit.GetRectifications(safeData.BidsectionId, safeData.ProjectId, safeData.Id, 1)	data.RectifiedInfo = rectifiedInfo	// 最新审批人信息	latestAuditor := s.daoApprover.GetLastedAuditor(safeData.BidsectionId, 1, safeData.Id)	data.LatestdAuditor = latestAuditor	data.Auditors = encryptAuditors	return data}// 安全概况func (s *safeService) GetSurvey(projectId int, bidsectionId int) map[string]interface{} {	// 1.获得安全巡检	year := time.Now().Year()	safeList := s.daoSafe.GetTypeYear(bidsectionId, year)	// 2.初始化	rectifylist := make([]viewmodels.SafeSurveyList, 0)	rectifyTotal := 0	approvalTotal := 0	rectifyedTotal := 0	columnarData := make([]map[string]interface{}, 0)	lineData := columnarData	for i := 1; i <= 12; i++ {		item := map[string]interface{}{			"name":  "rectifyed",			"month": fmt.Sprintf("%d-%02d", year, i),			"count": 0,		}		columnarData = append(columnarData, item)		item = map[string]interface{}{			"name":  "submit",			"month": fmt.Sprintf("%d-%02d", year, i),			"count": 0,		}		columnarData = append(columnarData, item)		item = map[string]interface{}{			"month":      fmt.Sprintf("%d-%02d", year, i),			"percentage": 0,		}		lineData = append(lineData, item)	}	// 3.当年数据初始化	submitData := map[string]int{		fmt.Sprintf("%d-01", year): 0,		fmt.Sprintf("%d-02", year): 0,		fmt.Sprintf("%d-03", year): 0,		fmt.Sprintf("%d-04", year): 0,		fmt.Sprintf("%d-05", year): 0,		fmt.Sprintf("%d-06", year): 0,		fmt.Sprintf("%d-07", year): 0,		fmt.Sprintf("%d-08", year): 0,		fmt.Sprintf("%d-09", year): 0,		fmt.Sprintf("%d-10", year): 0,		fmt.Sprintf("%d-11", year): 0,		fmt.Sprintf("%d-12", year): 0,	}	rectifyedData := map[string]int{		fmt.Sprintf("%d-01", year): 0,		fmt.Sprintf("%d-02", year): 0,		fmt.Sprintf("%d-03", year): 0,		fmt.Sprintf("%d-04", year): 0,		fmt.Sprintf("%d-05", year): 0,		fmt.Sprintf("%d-06", year): 0,		fmt.Sprintf("%d-07", year): 0,		fmt.Sprintf("%d-08", year): 0,		fmt.Sprintf("%d-09", year): 0,		fmt.Sprintf("%d-10", year): 0,		fmt.Sprintf("%d-11", year): 0,		fmt.Sprintf("%d-12", year): 0,	}	for _, item := range safeList {		if item.Status == 2 {			// 单独去查审批表 -TODO			idInt, _ := strconv.Atoi(item.Id)			detail := s.daoApprover.GetDataIdByProgress(idInt, 1, 1)			id, _ := comm.AesEncrypt(item.Id, conf.SignSecret)			item.Id = id			item.UpdateTime = detail.UpdateTime			rectifylist = append(rectifylist, item)			rectifyTotal++		}		if item.Status == 1 {			approvalTotal++		}		// approvalTotal++		if item.Status == 4 {			rectifyedTotal++		}		// for index, columnar := range columnarData {		// 	rectifyedCount := 0		// 	if columnar["month"] == item.CreateTime.Format(conf.SysTimeformMonth) {		// 		if item.Status == 4 && columnar["name"] == "rectifyed" {		// 			rectifyedCount++		// 		}		// 		columnarData[index]["count"] = columnarData[index]["count"].(int) + 1		// 		columnarData[index]["count"] = rectifyedCount		// 		// if item.Status == 0 && columnar["name"] == "submit" {		// 		// }		// 	}		// }		index := item.CreateTime.Format(conf.SysTimeformMonth)		submitData[index] = submitData[index] + 1		// if item.Status == 0 {		// 	submitData[item.CreateTime.Format(conf.SysTimeformMonth)] = submitData[item.CreateTime.Format(conf.SysTimeformMonth)] + 1		// }		if item.Status == 4 {			rectifyedData[index] = rectifyedData[index] + 1		}	}	for index, columnar := range columnarData {		if columnar["name"] == "rectifyed" {			columnarData[index]["count"] = rectifyedData[columnar["month"].(string)]		}		if columnar["name"] == "submit" {			columnarData[index]["count"] = submitData[columnar["month"].(string)]		}	}	for index, line := range lineData {		rectifyedCount := 0		submitCount := 0		for _, columnar := range columnarData {			if line["month"] == columnar["month"] {				if columnar["name"] == "rectifyed" {					rectifyedCount = columnar["count"].(int)				}				if columnar["name"] == "submit" {					submitCount = columnar["count"].(int)				}			}		}		lineData[index]["percentage"] = 0.00		if rectifyedCount != 0 && submitCount != 0 {			decimal.DivisionPrecision = 2			percentage, _ := decimal.NewFromFloat(float64(rectifyedCount)).Div(decimal.NewFromFloat(float64(submitCount))).Float64()			lineData[index]["percentage"] = percentage * 100		}	}	// 整改占总数比例 - 完成整改/提交巡检	surveryData := map[string]interface{}{		"rectifylist":    rectifylist,		"rectifyTotal":   rectifyTotal,		"approvalTotal":  approvalTotal,		"rectifyedTotal": rectifyedTotal,		"columnarData":   columnarData,		"lineData":       lineData,		// "submitData":     submitData,		// "rectifyedData":  rectifyedData,	}	return surveryData}// 获得账号下需要审批的巡检func (s *safeService) GetPending(projectId int, projectAccountId int) []viewmodels.ApproverMessage {	// 1.获得审批列表	approverData := s.daoApprover.GetStatusByProjectAndAccount(projectId, projectAccountId, 1)	// 2.构建数据ID	safeIds := []int{}	qualityIds := []int{}	for _, item := range approverData {		if item.DataType == 1 {			safeIds = append(safeIds, item.DataId)		} else if item.DataType == 2 {			qualityIds = append(qualityIds, item.DataId)		}	}	// safeInId := strings.Join(safeIds, ",")	// qualityInId := strings.Join(qualityIds, ",")	safeList := make([]viewmodels.SafeList, 0)	qualityList := make([]viewmodels.QualityList, 0)	if len(safeIds) != 0 {		safeList = s.daoSafe.GetInIdJoinAccount(safeIds)	}	if len(qualityIds) != 0 {		qualityList = s.daoQuality.GetInIdJoinAccount(qualityIds)	}	// data := s.daoSafe.GetStatusByProjectAndAccount(projectId, projectAccountId, 1)	list := make([]viewmodels.ApproverMessage, 0)	for _, item := range approverData {		approverVM := viewmodels.ApproverMessage{}		Id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)		// BidsectionId, _ := comm.AesEncrypt(strconv.Itoa(item.BidsectionId), conf.SignSecret)		// AuditId, _ := comm.AesEncrypt(item.AuditId, conf.SignSecret)		// ProjectId, _ := comm.AesEncrypt(strconv.Itoa(item.ProjectId), conf.SignSecret)		DataId, _ := comm.AesEncrypt(strconv.Itoa(item.DataId), conf.SignSecret)		// ProjectId, _ := comm.AesEncrypt(strconv.Itoa(item.ProjectId), conf.SignSecret)		approverVM.Id = Id		// approverVM.ProjectId = ProjectId		// approverVM.BidsectionId = BidsectionId		approverVM.DataType = item.DataType		approverVM.DataId = DataId		approverVM.Status = item.Status		dataIdString := strconv.Itoa(item.DataId)		// 安全巡检相关		if item.DataType == 1 {			for _, data := range safeList {				if dataIdString == data.Id {					bidSectionDetail := s.daoBidsection.Get(data.BidsectionId, projectId)					approverVM.Code = data.Code					approverVM.Inspection = data.Inspection					approverVM.InspectionDetail = data.InspectionDetail					approverVM.Name = data.AuditName					approverVM.Position = data.Position					approverVM.Inspection = data.Inspection					approverVM.BidsectionName = bidSectionDetail.Name					approverVM.CreateTime = data.CreateTime					approverVM.UpdateTime = item.UpdateTime					break				}			}		} else if item.DataType == 2 { // 质量巡检			for _, data := range qualityList {				if dataIdString == data.Id {					bidSectionDetail := s.daoBidsection.Get(data.BidsectionId, projectId)					approverVM.Code = data.Code					approverVM.InspectionDetail = data.InspectionDetail					approverVM.Name = data.AuditName					approverVM.Position = data.Position					approverVM.Inspection = data.Inspection					approverVM.BidsectionName = bidSectionDetail.Name					approverVM.CreateTime = data.CreateTime					approverVM.UpdateTime = item.UpdateTime					break				}			}		}		list = append(list, approverVM)	}	return list}// 规则校验func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {	safeVaild := viewmodels.Safe{}	// fmt.Println("---------------------------safeVaild", safeVaild)	if ctx.Method() == "GET" {		err := ctx.ReadForm(&safeVaild)		if err != nil {			log.Println("safe-ValidRule-ReadForm转换异常, error=", err)			return safeVaild, err		}		if ctx.Path() == s.validDetail {			// 一样要传id,所以用delete的方法判断			err = safeVaild.ValidateDelete()		} else {			err = safeVaild.ValidateList()		}		return safeVaild, err	}	if ctx.Method() == "POST" {		err := ctx.ReadJSON(&safeVaild)		if err != nil {			log.Println("safe-ValidRule-ReadJson转换异常, error=", err)			return safeVaild, err		}		err = safeVaild.ValidateCreate()		return safeVaild, err		// if ctx.Path() == s.validCreate {		// }		// if ctx.Path() == s.validFile {		// 	err = safeVaild.ValidateFile()		// 	return safeVaild, err		// }	}	if ctx.Method() == "PUT" {		err := ctx.ReadForm(&safeVaild)		if err != nil {			log.Println("safe-ValidRule-ReadForm转换异常, error=", err)			return safeVaild, err		}		err = safeVaild.ValidateDelete()		return safeVaild, err	}	return safeVaild, nil}
 |