// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package project import ( "context" "github.com/jaryhe/gopkgs/database" "github.com/jaryhe/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "smart-site-management/errors" "smart-site-management/model" "smart-site-management/pb/v1" "time" ) // 添加工程进度 func ProjectProgressAdd(ctx context.Context, req *v1.ProjectProgressAddRequest) (reply *v1.ProjectProgressAddReply, err error) { reply = &v1.ProjectProgressAddReply{} if req.ProjectId == 0 || req.Name == "" || req.ExpectStartTime == 0 || req.ExpectEndTime == 0 || req.ExpectEndTime < req.ExpectStartTime{ return nil, errors.ParamsError } project := &model.TProject{} where := map[string]interface{}{ "id":req.ProjectId, } err = project.Find(database.DB(), where) if err != nil { logger.Error("ProjectProgressAdd", zap.String("err", err.Error())) return nil, errors.DataBaseError } if project.Id == 0 { return nil, errors.NoRecordError } if req.ExpectStartTime < project.StartDay.Unix() { return nil, status.Error(10003, "进度开始时间不得小于工程开工时间") } if req.ExpectEndTime > project.EndDay.Unix() { return nil, status.Error(10003, "进度结束时间不得大于工程竣工时间") } p := &model.TProjectSchedule{} where = map[string]interface{}{ "project_id":req.ProjectId, "name":req.Name, } err = p.Find(database.DB(), where, nil) if err != nil { logger.Error("ProjectProgressAdd", zap.String("err", err.Error())) return nil, errors.DataBaseError } if p.ID > 0 { return nil, errors.ProjectProgressExistError } now := time.Now() p.Name = req.Name p.ProjectId = req.ProjectId p.Status = 0 p.ExpectStartTime = time.Unix(req.ExpectStartTime, 0) p.ExpectEndTime = time.Unix(req.ExpectEndTime, 0) p.UpdatedAt = now p.CreatedAt = now err = p.Insert(database.DB()) if err != nil { logger.Error("ProjectProgressAdd", zap.String("err", err.Error())) return nil, errors.DataBaseError } reply.Id = p.ID return reply, nil }