123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package v1
- import (
- "access-control-monitor/consts"
- "access-control-monitor/errors"
- "access-control-monitor/model/v1"
- param_v1 "access-control-monitor/param/v1"
- "access-control-monitor/utils"
- "google.golang.org/grpc/status"
- "github.com/jinzhu/gorm"
- "github.com/jaryhe/gopkgs/database"
- )
- // 注册员工
- func Register(body param_v1.RegisterBody, projectId int64) error {
- staff := &model.Staff{}
- utils.StructCopy(staff, &body, "json")
- staff.Status = consts.StaffNew
- staff.ProjectId = projectId
- switch {
- case staff.Name == "":
- return status.Error(10003, "参数错误,姓名为空")
- case staff.WorkNo == "":
- return status.Error(10003, "参数错误,工号为空")
- case staff.IdCert == "":
- return status.Error(10003, "参数错误,身份证号为空")
- case staff.StaffType == 0:
- return status.Error(10003, "参数错误,工种为空")
- case staff.WorkType == 0:
- return status.Error(10003, "参数错误,员工类型为空")
- }
- err := staff.Insert(database.DB())
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- // 设备主动推送删除员工
- func StaffDelete(sn string, work_nos []string) error {
- staff := &model.Staff{}
- filter := map[string]interface{}{"sn": sn}
- db := database.DB().Begin()
- for _, work_no := range work_nos {
- filter["work_no"] = work_no
- err := staff.Delete(db, filter)
- if err != nil {
- db.Rollback()
- return errors.DataBaseError
- }
- }
- db.Commit()
- return nil
- }
- // 设备被动删除接口----------------------------------------------------------------------------------------
- // 获取删除员工列表
- func GetDelete(sn string) ([]string, error) {
- staff := &model.Staff{}
- staffs := []model.Staff{}
- out := []string{}
- db := database.DB().Begin()
- filter := map[string]interface{}{"sn": sn, "status": consts.StaffStayDelete}
- err := staff.QueryAll(db, filter, &staffs)
- if err != nil {
- return nil, err
- }
- for _, v := range staffs {
- out = append(out, v.WorkNo)
- filed := map[string]interface{}{"status": consts.StaffStayDelete}
- err := v.UpdateSome(db, filed)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- }
- // 无待删除人员
- if len(out) == 0 {
- return nil, errors.NoDeleteError
- }
- db.Commit()
- return out, nil
- }
- // 获取批量下载
- func GetBatchDownload(sn string) ([]param_v1.BatchDownload, error) {
- batchDownloads := []param_v1.BatchDownload{}
- staff := &model.Staff{}
- staffs := []model.Staff{}
- db := database.DB().Begin()
- filter := map[string]interface{}{"sn": sn, "status": consts.StaffNew}
- err := staff.QueryAll(db, filter, &staffs)
- if err != nil {
- if err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- } else {
- return nil, errors.NoDownloadError
- }
- }
- for _, v := range staffs {
- batchDownload := param_v1.BatchDownload{Name: v.Name,
- WorkNo: v.WorkNo,
- IdCert: v.IdCert,
- Photo: v.Photo}
- batchDownloads = append(batchDownloads, batchDownload)
- filed := map[string]interface{}{"status": consts.StaffStayDownload}
- err := v.UpdateSome(db, filed)
- if err != nil {
- db.Rollback()
- return nil, errors.DataBaseError
- }
- }
- // 无待下发人员
- if len(batchDownloads) == 0 {
- return nil, errors.NoDownloadError
- }
- db.Commit()
- return batchDownloads, nil
- }
- // 删除员工
- func Delete(sn string) error {
- staff := &model.Staff{}
- filter := map[string]interface{}{"sn": sn, "status": consts.StaffStayDelete}
- err := staff.Delete(database.DB(), filter)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- // 设置下发状态
- func SetDownloadStatus(sn string, status int) error {
- staff := &model.Staff{}
- fileds := map[string]interface{}{"status": status}
- filter := map[string]interface{}{"status": consts.StaffStayDownload, "sn": sn}
- err := staff.UpdateColumn(database.DB(), filter, fileds)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
|