|
|
@@ -1,6 +1,18 @@
|
|
|
package equipment
|
|
|
|
|
|
-import "lims_adapter/dao"
|
|
|
+import (
|
|
|
+ "dashoo.cn/micro_libary/request"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/frame/g"
|
|
|
+ "github.com/gogf/gf/os/gtime"
|
|
|
+ "github.com/gogf/gf/util/gconv"
|
|
|
+ "lims_adapter/dao"
|
|
|
+ "lims_adapter/model"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
|
|
|
// Service 设备
|
|
|
type Service struct {
|
|
|
@@ -12,3 +24,290 @@ type Service struct {
|
|
|
func NewSrv(tenant string) Service {
|
|
|
return Service{Dao: dao.NewBaseEquipmentQualificationDao(tenant), Tenant: tenant}
|
|
|
}
|
|
|
+
|
|
|
+// AppointmentQualificationList 预约资格分页查询
|
|
|
+func (s Service) AppointmentQualificationList(req *model.QualificationListReq) ([]model.UserQualification, int, error) {
|
|
|
+ var current = req.Current
|
|
|
+ var size = req.Size
|
|
|
+ var enabled = req.Enabled
|
|
|
+ var instrumentId = req.InstrumentId
|
|
|
+ var reqType = req.Type
|
|
|
+ entity := s.Dao.DB.Model("base_user a")
|
|
|
+ var userInfo []model.UserQualification
|
|
|
+ where := "a.Enabled = " + strconv.Itoa(enabled) + " and b.EquipmentId = " + strconv.Itoa(instrumentId)
|
|
|
+
|
|
|
+ if reqType == 1 { // 预约资格查询条件
|
|
|
+ where += " and (b.Qualification = 1 or b.Qualification = 2)"
|
|
|
+ } else if reqType == 2 { // 优先预约权查询条件
|
|
|
+ where += " and b.Qualification = 3"
|
|
|
+ }
|
|
|
+ entity.LeftJoin("base_equipment_qualification b", "a.id = b.UserId").
|
|
|
+ Where(where)
|
|
|
+ count, _:= entity.Count()
|
|
|
+ err := entity.Fields("a.Id, a.UserName, a.RealName, a.Mobile, b.Qualification, b.AuthorizeTime").
|
|
|
+ Page(current, size).Scan(&userInfo)
|
|
|
+ if err != nil {
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ return userInfo, count, nil
|
|
|
+}
|
|
|
+
|
|
|
+// AdvanceList 优先预约权资格列表查询
|
|
|
+func (s Service) AdvanceList(req *model.QualificationListReq) ([]model.UserQualification, int, error) {
|
|
|
+ var current = req.Current
|
|
|
+ var size = req.Size
|
|
|
+ var enabled = req.Enabled
|
|
|
+ var instrumentId = req.InstrumentId
|
|
|
+ entity := s.Dao.DB.Model("base_user a")
|
|
|
+ var userInfo []model.UserQualification
|
|
|
+ entity.LeftJoin("base_equipment_qualification b", "a.id = b.UserId").
|
|
|
+ Where("a.Enabled = " + strconv.Itoa(enabled) + " and b.EquipmentId = " + strconv.Itoa(instrumentId))
|
|
|
+ count, _ := entity.Count()
|
|
|
+ err := entity.Fields("a.Id, a.UserName, a.RealName, a.Mobile, b.AuthorizeTime").
|
|
|
+ Page(current, size).Scan(&userInfo)
|
|
|
+ if err != nil {
|
|
|
+ return nil, 0, err
|
|
|
+ }
|
|
|
+ return userInfo, count, nil
|
|
|
+}
|
|
|
+
|
|
|
+// AddOrUpdateQualification 新增、修改预约资格名单;新增优先预约权名单
|
|
|
+func (s Service) AddOrUpdateQualification(req *model.AddOrUpdateQualification, userInfo request.UserInfo) (err error) {
|
|
|
+ entity := s.Dao.M
|
|
|
+ var instrumentId = req.InstrumentId
|
|
|
+ var ids = req.Ids
|
|
|
+ var qualification = 1
|
|
|
+ if req.QualificationType == "normal" {
|
|
|
+ qualification = 1
|
|
|
+ } else if req.QualificationType == "senior" {
|
|
|
+ qualification = 2
|
|
|
+ } else if req.QualificationType == "advance" {
|
|
|
+ qualification = 3
|
|
|
+ }
|
|
|
+ //if qualification == 1 || qualification == 2 { // 预约资格
|
|
|
+ for _, v := range ids {
|
|
|
+ where := "EquipmentId = " + strconv.Itoa(instrumentId) + " and UserId = " +
|
|
|
+ strconv.Itoa(v)
|
|
|
+ if qualification == 1 || qualification == 2 { // 预约资格
|
|
|
+ where += " and Qualification != 3"
|
|
|
+ } else if qualification ==3 { // 优先预约权
|
|
|
+ // 先判断是否有预约资格,没有则不更新操作
|
|
|
+ count, _ := s.Dao.DB.Model("base_equipment_qualification").Where("EquipmentId = " +
|
|
|
+ strconv.Itoa(instrumentId) + " and Qualification != 0" + " and UserId = " +
|
|
|
+ strconv.Itoa(v)).Count()
|
|
|
+ if count == 0 {
|
|
|
+
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ where += " and Qualification = 3"
|
|
|
+ }
|
|
|
+ var qualificationInfo model.BaseEquipmentQualification
|
|
|
+ entity.Where(where).Scan(&qualificationInfo)
|
|
|
+ entity.Data(g.Map{"Id": qualificationInfo.Id,"EquipmentId": instrumentId, "UserId": v, "Qualification": qualification, "CreateOn": gtime.Now(),
|
|
|
+ "AuthorizeTime": gtime.Now(), "CreateUserId": userInfo.Id, "CreateBy": userInfo.RealName}).Save()
|
|
|
+
|
|
|
+ }
|
|
|
+ //} else if qualification ==3 { // 优先预约权
|
|
|
+ // for _, v := range ids {
|
|
|
+ // var qualificationInfo model.BaseEquipmentQualification
|
|
|
+ // entity.Where("EquipmentId = " + strconv.Itoa(instrumentId) + " and UserId = " +
|
|
|
+ // strconv.Itoa(v) ).Scan(&qualificationInfo)
|
|
|
+ // entity.Data(g.Map{"Id": qualificationInfo.Id,"EquipmentId": instrumentId, "UserId": v, "CreateOn": gtime.Now(),
|
|
|
+ // "AuthorizeTime": gtime.Now(), "CreateUserId": userInfo.Id, "CreateBy": userInfo.RealName}).Save()
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+// DeleteQualification 删除预约资格
|
|
|
+func (s Service) DeleteQualification(req *model.IdReq, info request.UserInfo) error {
|
|
|
+ _, err := s.Dao.M.Update(g.Map{"Qualification": 0, "ModifiedOn": gtime.Now(),
|
|
|
+ "ModifiedUserId": info.Id, "ModifiedBy": info.RealName}, "UserId = " + strconv.Itoa(req.UserId) +
|
|
|
+ " and EquipmentId = " + strconv.Itoa(req.EquipmentId) + " and Qualification != 3")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// DeleteAdvance 删除优先预约权人员
|
|
|
+func (s Service) DeleteAdvance(req *model.IdReq) error {
|
|
|
+ _, err := s.Dao.M.Delete("UserId = " + strconv.Itoa(req.UserId) +
|
|
|
+ " and EquipmentId = " + strconv.Itoa(req.EquipmentId) + " and Qualification = 3")
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// SearchAdvanceTime 查看设备优先预约时间段
|
|
|
+func (s Service) SearchAdvanceTime(req *model.QualificationListReq) (advanceTime []model.BaseEquipmentAdvanceTime, count int, err error) {
|
|
|
+ entity := s.Dao.DB.Model("base_equipment_advance_time")
|
|
|
+ err = entity.Where("EquipmentId = " + strconv.Itoa(req.InstrumentId)).Scan(&advanceTime)
|
|
|
+ count, _ = entity.CountColumn("1")
|
|
|
+ err = entity.Fields("Week, StartTime,EndTime").Order("Week").Page(req.Current, req.Size).Scan(&advanceTime)
|
|
|
+ if err != nil {
|
|
|
+ return advanceTime, count, err
|
|
|
+ }
|
|
|
+ return advanceTime, count, nil
|
|
|
+}
|
|
|
+
|
|
|
+// AddAdvanceTime 添加设备优先预约时间段
|
|
|
+func (s Service) AddAdvanceTime(req *model.AdvanceTimeReq, info request.UserInfo) error {
|
|
|
+ //for _, v := range domains {
|
|
|
+ // count, err := s.Dao.DB.Model("base_equipment_advance_time").Where("Week = '" + v.Week +
|
|
|
+ // "' and StartTime = '" + v.StartTime + "' and EndTime = '" + v.EndTime + "'").Count()
|
|
|
+ // if err != nil{
|
|
|
+ // return err
|
|
|
+ // }
|
|
|
+ // if count == 0 {
|
|
|
+ // week, _ := strconv.Atoi(v.Week)
|
|
|
+ // saveEntity := model.BaseEquipmentAdvanceTime{
|
|
|
+ // EquipmentId: equipmentId,
|
|
|
+ // Week: week,
|
|
|
+ // StartTime: v.StartTime,
|
|
|
+ // EndTime: v.EndTime,
|
|
|
+ // CreateOn: gtime.Now(),
|
|
|
+ // CreateUserId: gconv.Int(info.Id),
|
|
|
+ // CreateBy: info.RealName,
|
|
|
+ // }
|
|
|
+ // s.Dao.DB.Model("base_equipment_advance_time").Insert(saveEntity)
|
|
|
+ // }
|
|
|
+ //}
|
|
|
+ count, err := s.Dao.DB.Model("base_equipment_advance_time").Where("EquipmentId = " +
|
|
|
+ strconv.Itoa(req.EquipmentId) + " and Week = " + req.Week +
|
|
|
+ " and StartTime = '" + req.StartTime + "' and EndTime = '" + req.EndTime + "'").Count()
|
|
|
+ if err != nil{
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ if count > 0 {
|
|
|
+ return errors.New("该时间段已存在,请重新输入!")
|
|
|
+ }
|
|
|
+ week, _ := strconv.Atoi(req.Week)
|
|
|
+ saveEntity := model.BaseEquipmentAdvanceTime{
|
|
|
+ EquipmentId: req.EquipmentId,
|
|
|
+ Week: week,
|
|
|
+ StartTime: req.StartTime,
|
|
|
+ EndTime: req.EndTime,
|
|
|
+ CreateOn: gtime.Now(),
|
|
|
+ CreateUserId: gconv.Int(info.Id),
|
|
|
+ CreateBy: info.RealName,
|
|
|
+ }
|
|
|
+ _, err = s.Dao.DB.Model("base_equipment_advance_time").Insert(saveEntity)
|
|
|
+ if err != nil{
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// DeleteAdvanceTime 删除设备预约时间段
|
|
|
+func (s Service) DeleteAdvanceTime(id int) error {
|
|
|
+ _, err := s.Dao.DB.Model("base_equipment_advance_time").Delete("Id = " + strconv.Itoa(id))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// AppointmentTime 设备预约时间段
|
|
|
+func (s Service) AppointmentTime(req *model.IdReq) error{
|
|
|
+ // 先查该人员的预约资格、优先使用权
|
|
|
+ var qualificationInfo []model.BaseEquipmentQualification
|
|
|
+ s.Dao.DB.Model("base_equipment_qualification").Where("EquipmentId = " +
|
|
|
+ strconv.Itoa(req.EquipmentId)).Order("Qualification").Scan(&qualificationInfo)
|
|
|
+ // 如果是普通预约资格,筛选出周末时间段
|
|
|
+ if qualificationInfo[0].Qualification == "1" {
|
|
|
+ // TODO 时间获取
|
|
|
+ }
|
|
|
+ // 如果有预约资格但没有优先使用权,查询优先使用权时间段
|
|
|
+ if len(qualificationInfo) ==2 && qualificationInfo[1].Qualification == "3"{
|
|
|
+ var appointmentInfo []model.BaseEquipmentAdvanceTime
|
|
|
+ s.Dao.DB.Model("base_equipment_advance_time").Where("EquipmentId = " +
|
|
|
+ strconv.Itoa(req.EquipmentId) + " and UserId").Scan(&appointmentInfo)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// SearchNoAppointment 查看不能预约时间段信息
|
|
|
+func (s Service) SearchNoAppointment(req *model.IdReq, info request.UserInfo) ([]model.NoReservationInfo, error) {
|
|
|
+ // 普通资格限制周末时间
|
|
|
+ var qualificationInfo []model.BaseEquipmentQualification
|
|
|
+ var noAppointmentInfos []model.NoReservationInfo
|
|
|
+ s.Dao.DB.Model("base_equipment_qualification").Where("EquipmentId = " +
|
|
|
+ strconv.Itoa(req.EquipmentId) + " and UserId = " +
|
|
|
+ strconv.Itoa(gconv.Int(info.Id))).Order("Qualification").Scan(&qualificationInfo)
|
|
|
+ if len(qualificationInfo) >= 1 && qualificationInfo[0].Qualification == "1" { // 只有普通预约资格,限制周末预约时间段
|
|
|
+ // 查询设备可预约时间段
|
|
|
+ var equipmentInfo model.Instrument
|
|
|
+ s.Dao.DB.Model("instrument").Fields("BeginAt, EndAt").
|
|
|
+ Where("Id = " + strconv.Itoa(req.EquipmentId)).Scan(&equipmentInfo)
|
|
|
+ startTime := equipmentInfo.BeginAt
|
|
|
+ endTime := equipmentInfo.EndAt
|
|
|
+ // 设备当天预约时间总长度h
|
|
|
+ timeLong := endTime.Sub(startTime).Hours()
|
|
|
+ saturdayTime := noReservationInfo(startTime, timeLong, req.Date, 6)
|
|
|
+ sundayTime := noReservationInfo(startTime, timeLong, req.Date, 7)
|
|
|
+ noAppointmentInfos = append(saturdayTime, sundayTime...)
|
|
|
+ }
|
|
|
+ if len(qualificationInfo) == 1 && qualificationInfo[0].Qualification != "3" { // 没有优先预约权,限制优先预约时间段
|
|
|
+ var advanceTimeList []model.BaseEquipmentAdvanceTime
|
|
|
+ s.Dao.DB.Model("base_equipment_advance_time").
|
|
|
+ Where("EquipmentId = " + strconv.Itoa(req.EquipmentId)).Scan(&advanceTimeList)
|
|
|
+ for _, v := range advanceTimeList {
|
|
|
+ // TODO 遍历获取不能预约时间段
|
|
|
+ startTime, _ := gtime.StrToTime(strings.Trim(v.StartTime, " "), "H:i")
|
|
|
+ endTime, _ := gtime.StrToTime(strings.Trim(v.EndTime, " "), "H:i")
|
|
|
+ timeLong := endTime.Sub(startTime).Hours()
|
|
|
+ saturdayTime := noReservationInfo(startTime, timeLong, req.Date, v.Week)
|
|
|
+ noAppointmentInfos = append(noAppointmentInfos, saturdayTime...)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return noAppointmentInfos, nil
|
|
|
+}
|
|
|
+
|
|
|
+// ChangeInstrStauts 修改设备所属
|
|
|
+func (s Service) ChangeBelongs(req *model.IdReq) error{
|
|
|
+ _, err := s.Dao.DB.Model("Instrument").
|
|
|
+ Update("Belongs = " + req.Belongs, "Id = "+strconv.Itoa(req.EquipmentId))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// noReservationInfo 不可预约时间段处理 startTime开始时间 timeLong 时间间隔 Date 前端传入日期 week 星期几
|
|
|
+func noReservationInfo(startTime *gtime.Time, timeLong float64, Date string, week int) (noAppointmentInfo []model.NoReservationInfo) {
|
|
|
+ date, _ := gtime.StrToTime(Date, "Y-m-d")
|
|
|
+ // 获取当前星期对应的日期
|
|
|
+ day := GetDate(date, week)
|
|
|
+ grids := int(timeLong * 2)
|
|
|
+ for i := 0; i < grids; i++ {
|
|
|
+ // 开始时间
|
|
|
+ start_time := startTime.Add(time.Duration(i) * 30 * time.Minute)
|
|
|
+ //fmt.Println("day:", day, "week:", week, "start_time", fmt.Sprintf("%02d", start_time.Hour()) + ":"+fmt.Sprintf("%02d", start_time.Minute()), "status:", status)
|
|
|
+ noAppointmentInfo = append(noAppointmentInfo, model.NoReservationInfo{
|
|
|
+ Day : day,
|
|
|
+ Week : week,
|
|
|
+ StartTime : fmt.Sprintf("%02d", start_time.Hour()) + ":"+fmt.Sprintf("%02d", start_time.Minute()),
|
|
|
+ Status : 3,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return noAppointmentInfo
|
|
|
+}
|
|
|
+
|
|
|
+// 获取当前星期对应的日期
|
|
|
+func GetDate(Date *gtime.Time, week int) int{
|
|
|
+ weekday := Date.Weekday().String()
|
|
|
+ var targetDay *gtime.Time
|
|
|
+ switch weekday {
|
|
|
+ case "Monday": targetDay = Date.AddDate(0, 0, week - 1)
|
|
|
+ case "Tuesday": targetDay = Date.AddDate(0, 0, week - 2)
|
|
|
+ case "Wednesday": targetDay = Date.AddDate(0, 0, week - 3)
|
|
|
+ case "Thursday": targetDay = Date.AddDate(0, 0, week - 4)
|
|
|
+ case "Friday": targetDay = Date.AddDate(0, 0, week - 5)
|
|
|
+ case "Saturday": targetDay = Date.AddDate(0, 0, week - 6)
|
|
|
+ case "Sunday": targetDay = Date.AddDate(0, 0, week - 7)
|
|
|
+ }
|
|
|
+ _, _, day := targetDay.Date()
|
|
|
+ return day
|
|
|
+}
|