|
@@ -0,0 +1,158 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "iot_manager_service/app/multimedia/dao"
|
|
|
+ "iot_manager_service/app/multimedia/model"
|
|
|
+ "iot_manager_service/app/system/service"
|
|
|
+ "iot_manager_service/util/common"
|
|
|
+ "iot_manager_service/util/logger"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+var ProgramService = new(programService)
|
|
|
+
|
|
|
+type programService struct{}
|
|
|
+
|
|
|
+func (s *programService) Get(id int) (*dao.Program, *common.Errors) {
|
|
|
+ Program := &dao.Program{
|
|
|
+ ID: id,
|
|
|
+ }
|
|
|
+ err := Program.Get()
|
|
|
+ if err != nil {
|
|
|
+ return nil, common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ return Program, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *programService) List(tenantId int, searchValue string, current, size int) ([]model.ProgramDetail,
|
|
|
+ *common.Errors) {
|
|
|
+ Program := &dao.Program{
|
|
|
+ TenantId: tenantId,
|
|
|
+ }
|
|
|
+
|
|
|
+ offset := (current - 1) * size
|
|
|
+ limit := size
|
|
|
+
|
|
|
+ if searchValue != "" {
|
|
|
+ Program.Name = searchValue
|
|
|
+ }
|
|
|
+ programs, err := Program.GetPrograms(offset, limit)
|
|
|
+ if err != nil {
|
|
|
+ return nil, common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ var rsp []model.ProgramDetail
|
|
|
+ for _, pro := range programs {
|
|
|
+ rsp = append(rsp, model.ProgramDetail{
|
|
|
+ Program: pro,
|
|
|
+ ResolutionName: service.DictService.GetResolutionName(tenantId, pro.Resolution),
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return rsp, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *programService) Remove(userId int64, tenantId int, id int) *common.Errors {
|
|
|
+ relation := &dao.ProgramRelation{
|
|
|
+ ProgramId: id,
|
|
|
+ TenantId: tenantId,
|
|
|
+ IsDeleted: 1,
|
|
|
+ }
|
|
|
+ err := relation.Delete()
|
|
|
+ if err != nil {
|
|
|
+ return common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ program := &dao.Program{
|
|
|
+ ID: id,
|
|
|
+ IsDeleted: 1,
|
|
|
+ UpdateUser: userId,
|
|
|
+ UpdateTime: time.Now(),
|
|
|
+ }
|
|
|
+ err = program.Delete()
|
|
|
+ if err != nil {
|
|
|
+ return common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeInfoBar,
|
|
|
+ common.DeviceTypeDefault, "", common.OperationSuccess)
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *programService) GetList(tenantId int, name string) ([]dao.Program, *common.Errors) {
|
|
|
+ Program := &dao.Program{
|
|
|
+ TenantId: tenantId,
|
|
|
+ Name: name,
|
|
|
+ }
|
|
|
+ libraries, err := Program.GetAll()
|
|
|
+ if err != nil {
|
|
|
+ return nil, common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ return libraries, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *programService) Submit(tenantId int, userId int64, req model.ReqProgramSubmit) *common.Errors {
|
|
|
+ program := &dao.Program{
|
|
|
+ Name: req.Name,
|
|
|
+ Resolution: req.Resolution,
|
|
|
+ Duration: req.Duration,
|
|
|
+ FileSize: req.FileSize,
|
|
|
+ ImgDuration: req.ImgDuration,
|
|
|
+ Remarks: req.Remarks,
|
|
|
+ TenantId: tenantId,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ CreateUser: userId,
|
|
|
+ UpdateTime: time.Now(),
|
|
|
+ UpdateUser: userId,
|
|
|
+ IsDeleted: 0,
|
|
|
+ }
|
|
|
+ if err := program.Create(); err != nil {
|
|
|
+ logger.Logger.Errorf("Create err = %s \n", err.Error())
|
|
|
+ return common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ libraryIds := common.StringToIntArray(req.LibraryIds)
|
|
|
+ for index, libraryId := range libraryIds {
|
|
|
+ relation := &dao.ProgramRelation{
|
|
|
+ ProgramId: program.ID,
|
|
|
+ LibraryId: libraryId,
|
|
|
+ OrderNo: index + 1,
|
|
|
+ TenantId: tenantId,
|
|
|
+ IsDeleted: 0,
|
|
|
+ }
|
|
|
+ err := relation.Save()
|
|
|
+ if err != nil {
|
|
|
+ logger.Logger.Errorf("relation save err = %s \n", err.Error())
|
|
|
+ _ = program.Remove()
|
|
|
+ _ = relation.Remove()
|
|
|
+ return common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeInfoBar,
|
|
|
+ common.DeviceTypeDefault, program.Name, common.OperationSuccess)
|
|
|
+ return common.SuccessResponse(common.Succeeded, nil)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *programService) GetLibraryList(tenantId, programId int) ([]dao.Library, *common.Errors) {
|
|
|
+ relation := &dao.ProgramRelation{
|
|
|
+ ProgramId: programId,
|
|
|
+ TenantId: tenantId,
|
|
|
+ }
|
|
|
+ relations, err := relation.GetByProgram()
|
|
|
+ if err != nil {
|
|
|
+ logger.Logger.Errorf("relation BatchCreate err = %s \n", err.Error())
|
|
|
+ return nil, common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ var libraryIds []int
|
|
|
+ for _, relation := range relations {
|
|
|
+ libraryIds = append(libraryIds, relation.LibraryId)
|
|
|
+ }
|
|
|
+
|
|
|
+ library := &dao.Library{TenantId: tenantId}
|
|
|
+ libraries, err := library.GetLibrariesByIds(libraryIds)
|
|
|
+ if err != nil {
|
|
|
+ logger.Logger.Errorf("GetLibrariesByIds err = %s \n", err.Error())
|
|
|
+ return nil, common.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ return libraries, nil
|
|
|
+}
|