123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- // Copyright 2019 autocareai.com. All rights reserved.
- // Use of this source code is governed by autocareai.com.
- package v1
- import (
- "access-control-monitor/errors"
- "access-control-monitor/model/v1"
- param_v1 "access-control-monitor/param/v1"
- "access-control-monitor/pb"
- pb_v1 "access-control-monitor/pb/v1"
- "context"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jinzhu/gorm"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "access-control-monitor/parser"
- "time"
- )
- // 班组注册
- func TeamIn(db *gorm.DB, body param_v1.TeamInfoBody, projectId int64, code string, now time.Time) error {
- p := &model.TeamInoutInfo{
- ProjectId: projectId,
- Code: code,
- TeamSysNo: body.TeamSysNo,
- OccurTime: now,
- InOut: 1,
- }
- if err := p.Insert(db); err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func TeamOut(db *gorm.DB, teamSysNo int, now time.Time, projectId int64, code string) error {
- p := &model.TeamInoutInfo{
- ProjectId: projectId,
- Code: code,
- TeamSysNo: teamSysNo,
- OccurTime: now,
- InOut: 2,
- }
- if err := p.Insert(db); err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func PostTeamToProvincial(body param_v1.TeamInfoBody, code string) (string, error) {
- mreq := pb_v1.ProvincialPostTeamRequest{
- Code:code,
- TeamLeaderPhone:body.TeamLeaderPhone,
- TeamLeaderName:body.TeamLeaderName,
- TeamSysNo:int64(body.TeamSysNo),
- TeamName:body.TeamName,
- CorpName:body.CorpName,
- CorpCode:body.CorpCode,
- }
- mreply, err := pb.Provincial.ProvincialPostTeam(context.Background(), &mreq)
- if err != nil {
- logger.Error("ProvincialPostTeam",
- zap.String("err", err.Error()))
- return "", err
- }
- return mreply.Id, nil
- }
- func TeamInfoRegister(body param_v1.TeamInfoBody, projectId int64, code string) error {
- timeNow := time.Now()
- p := &model.TeamMasterInfo{
- Code: code,
- ProjectId: projectId,
- CorpName: body.CorpName,
- CorpCode: body.CorpCode,
- TeamLeaderName: body.TeamLeaderName,
- TeamLeaderPhone: body.TeamLeaderPhone,
- TeamName: body.TeamName,
- TeamSysNo: body.TeamSysNo,
- Remark: body.Remark,
- CreatedAt: timeNow,
- UpdatedAt: timeNow,
- Sn: body.Sn,
- }
- old := model.TeamMasterInfo{}
- where := map[string]interface{}{
- "ProjectId": projectId,
- }
- or := map[string]interface{}{
- "TeamSysNo": body.TeamSysNo,
- "TeamName":body.TeamName,
- }
- err := old.FindOr(database.DB(), where, or)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if old.ID > 0 {
- return status.Error(10002, "项目中班组(班组号或班组名)已存在")
- }
- // TODO
- if parser.Conf.UseProvincial {
- provincialId, err := PostTeamToProvincial(body, code)
- if err != nil {
- return err
- }
- p.ProvincialVerifyID = provincialId
- }
- db := database.DB().Begin()
- err = p.Insert(db)
- if err != nil {
- db.Rollback()
- return errors.DataBaseError
- }
- err = TeamIn(db, body, projectId, code, timeNow)
- if err != nil {
- db.Rollback()
- return errors.DataBaseError
- }
- db.Commit()
- return nil
- return nil
- }
- func TeamDelete(teamSysNo []int, projectId int64, code string) error {
- old := model.TeamMasterInfo{}
- where := map[string]interface{}{
- "TeamSysNo in": teamSysNo,
- "ProjectId": projectId,
- }
- db := database.DB().Begin()
- infos, err := model.GetTeamSysNo(db, projectId, teamSysNo)
- if err != nil {
- db.Rollback()
- return errors.DataBaseError
- }
- tmp := model.ProjectWorkInfo{}
- err = tmp.Find(db, where)
- if err != nil && err != gorm.ErrRecordNotFound {
- db.Rollback()
- return errors.DataBaseError
- }
- if tmp.Id > 0 {
- db.Rollback()
- return status.Error(10002, "请先删除班组下的人员")
- }
- err = old.Delete(db, where)
- if err != nil {
- db.Rollback()
- return errors.DataBaseError
- }
- for _, v := range infos {
- err = TeamOut(database.DB(), v.TeamSysNo, time.Now(), projectId, code)
- if err != nil {
- db.Rollback()
- return errors.DataBaseError
- }
- }
- db.Commit()
- return nil
- }
- func TeamUpdate(body param_v1.TeamInfoBody, projectId int64, code string) error {
- p := &model.TeamMasterInfo{}
- old := model.TeamMasterInfo{}
- where := map[string]interface{}{
- "TeamSysNo": body.TeamSysNo,
- "ProjectId": projectId,
- }
- err := old.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- // 不存在
- if old.ID == 0 {
- return status.Error(10002, "没有找到该班组")
- }
- if old.TeamName != body.TeamName {
- tmp := model.TeamMasterInfo{}
- where := map[string]interface{}{
- "TeamName": body.TeamName,
- "ProjectId": projectId,
- "TeamSysNo !=": body.TeamSysNo,
- }
- err := tmp.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- //
- if tmp.ID > 0 {
- return status.Error(10002, "班组名称重复")
- }
- }
- // 已存在则更新
- where["ID"] = old.ID
- values := map[string]interface{}{
- "CorpName": body.CorpName,
- "CorpCode": body.CorpCode,
- "TeamLeaderName": body.TeamLeaderName,
- "TeamLeaderPhone": body.TeamLeaderPhone,
- "TeamName": body.TeamName,
- "Remark": body.Remark,
- }
- err = p.Update(database.DB(), where, values)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
|