初始
This commit is contained in:
@ -0,0 +1,372 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2025-03-03 10:32:47
|
||||
// 生成路径: internal/app/system/logic/bus_attendance_machine.go
|
||||
// 生成人:gfast
|
||||
// desc:考勤机
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
wxDao "github.com/tiger1103/gfast/v3/internal/app/wxApplet/dao"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"github.com/tiger1103/gfast/v3/third/ws"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterBusAttendanceMachine(New())
|
||||
}
|
||||
|
||||
func New() *sBusAttendanceMachine {
|
||||
return &sBusAttendanceMachine{}
|
||||
}
|
||||
|
||||
type sBusAttendanceMachine struct{}
|
||||
|
||||
func (s *sBusAttendanceMachine) DeleteTheUserBoundToTheAttendanceMachineAndDevice(ctx context.Context, req *system.DeleteTheUserBoundToTheAttendanceMachineAndDeviceReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、将考勤机的用户信息删除掉
|
||||
_, err = ws.DelByUserId(req.Sn, req.UserId)
|
||||
liberr.ErrIsNil(ctx, err, "将用户从考勤机中删除失败!")
|
||||
//2、将数据库绑定的数据删除掉
|
||||
_, err = dao.BusAttendanceMachineUser.Ctx(ctx).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().MachineId, req.MachineId).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().TeamId, req.TeamId).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().UserId, req.UserId).Delete()
|
||||
liberr.ErrIsNil(ctx, err, "将用户与设备的绑定取消失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) ObtainInformationAboutPersonnelAssociatedWithTheAttendanceDevice(ctx context.Context, req *system.ObtainInformationAboutPersonnelAssociatedWithTheAttendanceDeviceReq) (listRes *system.ObtainInformationAboutPersonnelAssociatedWithTheAttendanceDeviceRes, err error) {
|
||||
listRes = new(system.ObtainInformationAboutPersonnelAssociatedWithTheAttendanceDeviceRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、根据班组ID去获取对应的成员,
|
||||
m := dao.BusConstructionUser.Ctx(ctx).WithAll().Fields("openid,user_name as userName,team_id as teamId,pace_photo as pacePhoto")
|
||||
if req.TeamId != "" {
|
||||
m = m.Where(dao.BusConstructionUser.Columns().TeamId, req.TeamId)
|
||||
}
|
||||
if req.UserName != "" {
|
||||
m = m.Where(dao.BusConstructionUser.Columns().UserName+" = ?", req.UserName)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.BusConstructionUser.Columns().CreatedAt+" >=? AND "+dao.BusConstructionUser.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if strings.EqualFold(req.IsPaging, "YES") {
|
||||
listRes.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
m = m.Page(req.PageNum, req.PageSize)
|
||||
}
|
||||
order := "id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var userList []*system.ObtainInformationAboutPersonnelAssociatedWithTheAttendanceDeviceTwo
|
||||
err = m.Order(order).Scan(&userList)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
//2、根据考勤机sn获取设备中存储在的用户ID(openid)
|
||||
all, err := ws.SelectUserAll(req.Sn)
|
||||
liberr.ErrIsNil(ctx, err, "考勤机连接异常!人员信息获取失败!")
|
||||
//3、根据用户ID比对考勤机的ID 以此得到用户是否在考勤机的状态
|
||||
ids := all.Data.UserIds
|
||||
for i, idata := range userList {
|
||||
userList[i].Status = "0"
|
||||
for _, jdata := range ids {
|
||||
if idata.Openid == jdata {
|
||||
userList[i].Status = "1"
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
//4、根据获取到指定班组的id人员,然后对比是否已将人员下发到考勤机了
|
||||
array, err := dao.BusAttendanceMachineUser.Ctx(ctx).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().MachineId, req.MachineId).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().TeamId, req.TeamId).
|
||||
Fields(dao.BusAttendanceMachineUser.Columns().UserId).Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取下发考勤机中存储的人员ID失败!")
|
||||
for i, idata := range userList {
|
||||
userList[i].XFStatus = "0"
|
||||
for _, jdata := range array {
|
||||
if idata.Openid == jdata.String() {
|
||||
userList[i].XFStatus = "1"
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
listRes.UserList = userList
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) SendTheUserInformationToTheAttendanceMachine(ctx context.Context, req *system.SendTheUserInformationToTheAttendanceMachineReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
|
||||
//1、获取下发人员
|
||||
g2 := dao.BusAttendanceMachineUser.Ctx(ctx).Safe()
|
||||
if req.Type == 1 {
|
||||
g2 = g2.Where(dao.BusAttendanceMachineUser.Columns().MachineId, req.MachineId)
|
||||
}
|
||||
if req.Type == 2 || req.Type == 3 {
|
||||
g2 = g2.WhereIn(dao.BusAttendanceMachineUser.Columns().UserId, req.UserId)
|
||||
}
|
||||
array, err2 := g2.Fields(dao.BusAttendanceMachineUser.Columns().UserId).Array()
|
||||
liberr.ErrIsNil(ctx, err2, "获取下发人员!")
|
||||
//2、获取用户得到用户ID、头像、名称、人脸等信息
|
||||
var userList []*model.BusConstructionUserListRes
|
||||
err2 = dao.BusConstructionUser.Ctx(ctx).WhereIn(dao.BusConstructionUser.Columns().Openid, array).Fields("openid,user_name,pace_photo").Scan(&userList)
|
||||
liberr.ErrIsNil(ctx, err2, "获取用户信息失败!")
|
||||
go xiaFaKaoQinJi(userList, req.Sn)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func xiaFaKaoQinJi(userList []*model.BusConstructionUserListRes, sn string) {
|
||||
for _, data := range userList {
|
||||
//赋值有人脸为空的数据
|
||||
if data.PacePhoto != "" {
|
||||
ws.TheSenderInformationOfTheAssemblyPersonnel(sn, data.Openid, data.UserName, coryCommon.GlobalPath+data.PacePhoto)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) BindUserInformationToDevicesInBatches(ctx context.Context, req *system.BindUserInformationToDevicesInBatchesReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var listEntity []*do.BusAttendanceMachineUser
|
||||
for _, data := range req.Bind {
|
||||
count, _ := dao.BusAttendanceMachineUser.Ctx(ctx).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().MachineId, data.MachineId).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().TeamId, data.TeamId).
|
||||
Where(dao.BusAttendanceMachineUser.Columns().UserId, data.UserId).Count()
|
||||
if count == 0 {
|
||||
listEntity = append(listEntity, &do.BusAttendanceMachineUser{
|
||||
MachineId: data.MachineId,
|
||||
TeamId: data.TeamId,
|
||||
UserId: data.UserId,
|
||||
})
|
||||
}
|
||||
}
|
||||
if len(listEntity) > 0 {
|
||||
_, err := g.DB().Insert(ctx, dao.BusAttendanceMachineUser.Table(), listEntity, 50)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) Change(ctx context.Context, req *system.BusAttendanceMachineChangeReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
machine := dao.BusAttendanceMachine
|
||||
_, err = machine.Ctx(ctx).Where(machine.Columns().Sn, req.Sn).Update(do.BusAttendanceMachine{
|
||||
Sn: req.Sn,
|
||||
Status: req.Status,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改考勤设备状态失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) Register(ctx context.Context, req *system.BusAttendanceMachineRegisterReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
count, _ := dao.BusAttendanceMachine.Ctx(ctx).Where(dao.BusAttendanceMachine.Columns().Sn, req.Sn).Count()
|
||||
if count > 0 {
|
||||
err = s.Change(ctx, &system.BusAttendanceMachineChangeReq{
|
||||
Sn: req.Sn,
|
||||
Status: "1",
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
return
|
||||
}
|
||||
_, err := dao.BusAttendanceMachine.Ctx(ctx).WithAll().Data(do.BusAttendanceMachine{
|
||||
Sn: req.Sn,
|
||||
Status: "1",
|
||||
}).Insert()
|
||||
liberr.ErrIsNil(ctx, err, "注册考勤设备失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) List(ctx context.Context, req *system.BusAttendanceMachineSearchReq) (listRes *system.BusAttendanceMachineSearchRes, err error) {
|
||||
listRes = new(system.BusAttendanceMachineSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.BusAttendanceMachine.Ctx(ctx).WithAll().As("a").
|
||||
LeftJoin(dao.SysProject.Table(), "b", "b.id = a.project_id").Fields("a.*,b.project_name as projectName")
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where("a."+dao.BusAttendanceMachine.Columns().ProjectId+" = ?", gconv.Uint64(req.ProjectId))
|
||||
}
|
||||
if req.Sn != "" {
|
||||
m = m.Where("a."+dao.BusAttendanceMachine.Columns().Sn+" = ?", req.Sn)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where("a."+dao.BusAttendanceMachine.Columns().CreatedAt+" >=? AND "+"a."+dao.BusAttendanceMachine.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
array, err := m.Array()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
listRes.Total = len(array)
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "id asc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.BusAttendanceMachineExtendRes
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
for i := range res {
|
||||
//查询设备的班组
|
||||
teamIds := strings.Split(res[i].Teams, ",")
|
||||
teamName := ""
|
||||
for _, id := range teamIds {
|
||||
name, err := wxDao.SysProjectTeam.Ctx(ctx).WherePri(wxDao.SysProjectTeam.Columns().Id, id).Fields("name").Value()
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "班组获取失败!")
|
||||
}
|
||||
teamName = teamName + "," + name.String()
|
||||
}
|
||||
res[i].TeamsName = strings.Trim(teamName, ",")
|
||||
}
|
||||
listRes.List = res
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) GetById(ctx context.Context, id uint64) (res *model.BusAttendanceMachineExtendRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.BusAttendanceMachine.Ctx(ctx).WithAll().Where(dao.BusAttendanceMachine.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) Add(ctx context.Context, req *system.BusAttendanceMachineAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.BusAttendanceMachine.Ctx(ctx).Insert(do.BusAttendanceMachine{
|
||||
ProjectId: req.ProjectId,
|
||||
Sn: req.Sn,
|
||||
Teams: req.Teams,
|
||||
CreateBy: req.CreateBy,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) Edit(ctx context.Context, req *system.BusAttendanceMachineEditReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//1、查询出上一次的数据
|
||||
var att *model.BusAttendanceMachineInfoRes
|
||||
err = dao.BusAttendanceMachine.Ctx(ctx).
|
||||
WherePri(req.Id).WhereNotNull(dao.BusAttendanceMachine.Columns().ProjectId).WhereNotNull(dao.BusAttendanceMachine.Columns().Teams).
|
||||
Fields(dao.BusAttendanceMachine.Columns().ProjectId, dao.BusAttendanceMachine.Columns().Teams).Scan(&att)
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
if att != nil {
|
||||
attProject := att.ProjectId
|
||||
if req.ProjectId != attProject {
|
||||
//删除考勤机绑定用户
|
||||
_, err = dao.BusAttendanceMachineUser.Ctx(ctx).Delete(dao.BusAttendanceMachineUser.Columns().MachineId, req.Id)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
//删除考勤机打卡用户
|
||||
ws.DelAll(req.Sn)
|
||||
} else {
|
||||
attTeams := att.Teams
|
||||
//上一次的班组
|
||||
oneTeamIds := strings.Split(attTeams, ",")
|
||||
//此次要修改的班组
|
||||
twoTeamIds := strings.Split(req.Teams, ",")
|
||||
//查找oneTeamIds和twoTeamIds是否有一致的
|
||||
ids := make([]string, 0)
|
||||
for _, oneTeamId := range oneTeamIds {
|
||||
for _, twoTeamId := range twoTeamIds {
|
||||
if oneTeamId == twoTeamId {
|
||||
ids = append(ids, oneTeamId)
|
||||
}
|
||||
}
|
||||
}
|
||||
columns := dao.BusAttendanceMachineUser.Columns()
|
||||
sqlDel := dao.BusAttendanceMachineUser.Ctx(ctx).Where(columns.MachineId, req.Id).Safe()
|
||||
if len(ids) == 0 {
|
||||
//删除所有
|
||||
_, err = sqlDel.Delete()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
ws.DelAll(req.Sn)
|
||||
}
|
||||
if len(ids) > 0 {
|
||||
//删除部分
|
||||
_, err = sqlDel.WhereNotIn(columns.TeamId, ids).Delete()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
array, err := sqlDel.WhereNotIn(columns.TeamId, ids).Fields(columns.UserId).Array()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
delUserIds := make([]string, 0)
|
||||
for i := range array {
|
||||
delUserIds = append(delUserIds, array[i].String())
|
||||
}
|
||||
if len(delUserIds) > 0 {
|
||||
ws.BatchDelete(req.Sn, delUserIds)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//2、如果twoTeamIds
|
||||
_, err = dao.BusAttendanceMachine.Ctx(ctx).WherePri(req.Id).Update(do.BusAttendanceMachine{
|
||||
ProjectId: req.ProjectId,
|
||||
Teams: req.Teams,
|
||||
Remark: req.Remark,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sBusAttendanceMachine) Delete(ctx context.Context, ids []uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
//查询所有考勤机的sn
|
||||
array, err := dao.BusAttendanceMachine.Ctx(ctx).Where(dao.BusAttendanceMachine.Columns().Id+" in (?)", ids).Fields(dao.BusAttendanceMachine.Columns().Sn).Array()
|
||||
liberr.ErrIsNil(ctx, err)
|
||||
//删除考勤机
|
||||
_, err = dao.BusAttendanceMachine.Ctx(ctx).Delete(dao.BusAttendanceMachine.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
//删除考勤机绑定用户
|
||||
_, err = dao.BusAttendanceMachineUser.Ctx(ctx).Delete(dao.BusAttendanceMachineUser.Columns().MachineId+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
//删除考勤机打卡用户
|
||||
for i := range array {
|
||||
go ws.DelAll(array[i].String())
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user