123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package v1
- import (
- "access-control-monitor/errors"
- "access-control-monitor/model/v1"
- param_v1 "access-control-monitor/param/v1"
- "access-control-monitor/utils"
- "context"
- "go.uber.org/zap"
- "strings"
- "github.com/jinzhu/gorm"
- "github.com/jaryhe/gopkgs/database"
- "github.com/jaryhe/gopkgs/logger"
- "github.com/jaryhe/gopkgs/tasker/transtasker"
- "access-control-monitor/pb"
- pb_v1 "access-control-monitor/pb/v1"
- "access-control-monitor/parser"
- )
- // 添加考勤数据至月表
- func addAttendanceToMonth(db *gorm.DB, content param_v1.AttendanceBody, projectId int64, code string, provincialId string) error {
- attendance := &model.WorkerAttendanceInfo{}
- dayTime := strings.Split(content.Datetime, " ")[0]
- month := utils.GetMonthFromDate(dayTime)
- if month < 1 {
- logger.Error("addAttendanceToMonth",
- zap.String("param", dayTime),
- zap.String("error", "get month failed"))
- return errors.ServiceError
- }
- attendance.TabType = month
- return addAttendance(db, content, projectId, code, attendance, provincialId)
- }
- // 添加考勤数据至当天表
- func addAttendanceToDay(db *gorm.DB, content param_v1.AttendanceBody, projectId int64, code string, provincialId string) error {
- attendance := &model.WorkerAttendanceInfo{
- TabType: model.AttendanceTabDay,
- }
- return addAttendance(db, content, projectId, code, attendance, provincialId)
- }
- func addAttendance(db *gorm.DB, content param_v1.AttendanceBody, projectId int64, code string, attendance *model.WorkerAttendanceInfo, provincialId string) error {
- attendance.Date = utils.ConvertDateTime(content.Datetime)
- attendance.TeamSysNo = content.TeamSysNo
- attendance.TeamName = content.TeamName
- attendance.WorkNo = content.WorkNo
- attendance.ProjectId = projectId
- attendance.Code = code
- attendance.WorkName = content.WorkName
- attendance.IDCardNumber = content.IDCardNumber
- attendance.IDCardType = content.IDCardType
- attendance.Direction = content.Direction
- attendance.Sn = content.Sn
- attendance.ProvincialVerifyID = provincialId
- err := attendance.Insert(database.DB())
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- func convertDate(date string) string {
- return utils.ConvertDateTime(date).Format("20060102150405")
- }
- func PostStaffAttendanceToProvincial(body param_v1.AttendanceBody, code string, oriCert string) (string, error) {
- mreq := pb_v1.ProvincialPostStaffAttendanceRequest{
- Code: code,
- TeamSysNo: int64(body.TeamSysNo),
- TeamName: body.TeamName,
- WorkerName:body.WorkName,
- IdCardType:int32(body.IDCardType),
- Direction:int32(body.Direction),
- Date:convertDate(body.Datetime),
- }
- //todo
- var err error
- mreq.IdCardNumber, err = utils.ProvincialEncrypt([]byte(body.IDCardNumber), []byte(parser.Conf.ThirdParty.Provincial.AppSecret))
- if err != nil {
- logger.Error("ProvincialEncrypt",
- zap.String("err", err.Error()))
- }
- mreply, err := pb.Provincial.ProvincialPostStaffAttendance(context.Background(), &mreq)
- if err != nil {
- logger.Error("ProvincialPostStaffAttendance",
- zap.String("err", err.Error()))
- return "", err
- }
- return mreply.Id, nil
- }
- // 添加考勤数据
- func AddAttendance(content param_v1.AttendanceBody, projectId int64, code string) error {
- provincialId := ""
- if parser.Conf.UseProvincial {
- provincialId, _ = PostStaffAttendanceToProvincial(content, code, content.IDCardNumber)
- }
- content.IDCardNumber = certEncrypt(content.IDCardNumber)
- dayTask := func(db *gorm.DB) error {
- return addAttendanceToDay(db, content, projectId, code, provincialId)
- }
- monthTask := func(db *gorm.DB) error {
- return addAttendanceToMonth(db, content, projectId, code, provincialId)
- }
- err := transtasker.Exec(database.DB(),
- transtasker.NewTask(dayTask),
- transtasker.NewTask(monthTask))
- return err
- }
|