2023-08-31 22:20:40 +08:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"enterprise/common/config"
|
|
|
|
"enterprise/common/dao"
|
2023-10-08 21:22:49 +08:00
|
|
|
"enterprise/common/global"
|
2023-08-31 22:20:40 +08:00
|
|
|
"enterprise/common/model"
|
2024-11-04 23:00:55 +08:00
|
|
|
"enterprise/service"
|
2023-10-08 21:22:49 +08:00
|
|
|
"fmt"
|
2024-08-09 13:57:03 +08:00
|
|
|
"git.u8t.cn/open/gosdk/qyweixin"
|
2023-08-31 22:20:40 +08:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-09-01 20:01:30 +08:00
|
|
|
"github.com/smbrave/goutil"
|
2023-08-31 22:20:40 +08:00
|
|
|
"github.com/spf13/cast"
|
2023-09-01 20:01:30 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-08-31 22:20:40 +08:00
|
|
|
)
|
|
|
|
|
2024-01-07 17:17:33 +08:00
|
|
|
type Staff struct {
|
|
|
|
}
|
|
|
|
|
2024-09-21 18:46:51 +08:00
|
|
|
func (s *Staff) MontorWorkAge() {
|
|
|
|
staffs, err := dao.NewStaffInfoDao().Query(model.StaffInfoStatusNormal)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nowDate := time.Now()
|
|
|
|
for _, staff := range staffs {
|
|
|
|
//离职的忽略
|
|
|
|
if staff.LeaveDate != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
entryDate, _ := time.ParseInLocation("2006-01-02", staff.EntryDate, time.Local)
|
|
|
|
spanMonth := (nowDate.Year()-entryDate.Year())*12 + int(nowDate.Month()) - int(entryDate.Month())
|
|
|
|
|
|
|
|
log.Infof("staff[%s] entryDate[%s] spanMonth[%.1f]", staff.Username, staff.EntryDate, float64(spanMonth)/12.0)
|
|
|
|
if nowDate.Day() != entryDate.Day() || spanMonth%6 != 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
message := make([]string, 0)
|
|
|
|
message = append(message, fmt.Sprintf("【员工半年提醒】[%s]", staff.Realname))
|
|
|
|
message = append(message, fmt.Sprintf("入职时间:%s", staff.EntryDate))
|
|
|
|
message = append(message, fmt.Sprintf("入职年限:%.1f", float64(spanMonth)/12))
|
2024-11-04 23:00:55 +08:00
|
|
|
message = append(message, fmt.Sprintf("基本工资:%s", staff.BaseSalary))
|
|
|
|
message = append(message, fmt.Sprintf("绩效工资:%s", staff.PerfSalary))
|
2024-09-21 18:46:51 +08:00
|
|
|
message = append(message, fmt.Sprintf("身份证号:%s", staff.Idno))
|
|
|
|
|
|
|
|
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
|
|
|
|
log.Errorf("send message error :%s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 17:17:33 +08:00
|
|
|
func (s *Staff) SendStaffSalaryBill(month string) {
|
2024-11-05 12:44:09 +08:00
|
|
|
masterBlank := ""
|
|
|
|
staffSalarys, err := dao.NewStaffSalaryDao().Query(month, "", &masterBlank)
|
2023-10-08 21:22:49 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, staffSalary := range staffSalarys {
|
|
|
|
if staffSalary.BaseSalary < 0.1 {
|
|
|
|
continue
|
|
|
|
}
|
2024-04-07 17:08:39 +08:00
|
|
|
userConfig, err := dao.NewUserConfigDao().GetByUsername(staffSalary.Username)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2023-10-08 21:22:49 +08:00
|
|
|
message := make([]string, 0)
|
|
|
|
message = append(message, fmt.Sprintf("【工资单】[%s][%s]", staffSalary.Username, month))
|
|
|
|
message = append(message, fmt.Sprintf("基本工资:%.2f", staffSalary.BaseSalary))
|
2024-04-02 12:14:23 +08:00
|
|
|
message = append(message, fmt.Sprintf("出勤工资:%.2f", staffSalary.AttendSalary))
|
2023-10-08 21:22:49 +08:00
|
|
|
if staffSalary.AwardSalary >= 0.1 {
|
|
|
|
message = append(message, fmt.Sprintf("额外工资:%.2f", staffSalary.AwardSalary))
|
|
|
|
}
|
2024-11-05 12:44:09 +08:00
|
|
|
|
|
|
|
//附属工资
|
|
|
|
var slaveSalary float64 = 0
|
|
|
|
staff, _ := dao.NewStaffInfoDao().GetByUsername(staffSalary.Username)
|
|
|
|
if staff != nil {
|
|
|
|
slaveSalary = service.NewStaff(staff, nil).GetSlaveSalary(month)
|
|
|
|
if slaveSalary > 0.1 {
|
|
|
|
message = append(message, fmt.Sprintf("附属工资:%.2f", slaveSalary))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-08 21:22:49 +08:00
|
|
|
message = append(message, fmt.Sprintf("社保扣除:%.2f", staffSalary.SocialInsurence))
|
|
|
|
message = append(message, fmt.Sprintf("个税扣除:%.2f", staffSalary.PersonalTax))
|
2024-11-05 12:44:09 +08:00
|
|
|
message = append(message, fmt.Sprintf("实发工资:%.2f", staffSalary.GetRealSalary()+slaveSalary))
|
2023-10-08 21:22:49 +08:00
|
|
|
|
|
|
|
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
|
|
|
|
log.Errorf("send message error :%s", err.Error())
|
|
|
|
}
|
2024-04-07 17:08:39 +08:00
|
|
|
if userConfig != nil && cast.ToBool(userConfig.Get(model.StaffSalaryNotify)) == true {
|
2024-05-06 10:35:03 +08:00
|
|
|
if err := global.SendMessage([]string{userConfig.Username}, strings.Join(message, "\n")); err != nil {
|
2024-04-07 17:08:39 +08:00
|
|
|
log.Errorf("send message error :%s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2023-10-08 21:22:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 17:17:33 +08:00
|
|
|
func (s *Staff) SyncStaffInfo() {
|
2023-08-31 22:20:40 +08:00
|
|
|
cfg := config.GetConfig()
|
2024-01-22 23:21:37 +08:00
|
|
|
hrAssiant := qyweixin.NewAppHr(&qyweixin.AppConfig{
|
|
|
|
Corpid: cfg.QyWeixin.Corpid,
|
2024-01-24 16:48:04 +08:00
|
|
|
Secret: cfg.QyWeixin.EnterpriseSecret,
|
|
|
|
Agent: cfg.QyWeixin.EnterpriseAgent,
|
2024-01-22 23:21:37 +08:00
|
|
|
})
|
2023-08-31 22:20:40 +08:00
|
|
|
|
2024-11-04 23:00:55 +08:00
|
|
|
userConfigs, err := dao.NewUserConfigDao().Query(model.StaffConfigStatusNormal)
|
2023-08-31 22:20:40 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("query staff db error :%s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, user := range userConfigs {
|
|
|
|
staffInfo, err := hrAssiant.GetStaffInfo(user.Username)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("getstaff info username[%s] error :%s", user.Username, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
staff, err := dao.NewStaffInfoDao().GetByUsername(user.Username)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if staff == nil {
|
|
|
|
staff = new(model.StaffInfo)
|
|
|
|
}
|
|
|
|
staff.Username = staffInfo.UserName
|
|
|
|
staff.Realname = staffInfo.RealName
|
2024-11-04 23:00:55 +08:00
|
|
|
staff.Idno = staffInfo.Extra["11015"]
|
|
|
|
staff.StaffType = staffInfo.Extra["12003"]
|
|
|
|
staff.BaseSalary = staffInfo.Extra["20001"]
|
|
|
|
staff.PerfSalary = staffInfo.Extra["20005"]
|
|
|
|
staff.EntryDate = time.Unix(cast.ToInt64(staffInfo.Extra["12018"]), 0).Format("2006-01-02")
|
|
|
|
staff.OfficialDate = time.Unix(cast.ToInt64(staffInfo.Extra["12023"]), 0).Format("2006-01-02")
|
|
|
|
staff.BirthDate = time.Unix(cast.ToInt64(staffInfo.Extra["11005"]), 0).Format("2006-01-02")
|
|
|
|
staff.BankName = staffInfo.Extra["13001"]
|
|
|
|
staff.BankCard = staffInfo.Extra["13002"]
|
|
|
|
staff.AlipayUid = staffInfo.Extra["20004"]
|
2023-08-31 22:20:40 +08:00
|
|
|
|
|
|
|
if staff.Id == 0 {
|
|
|
|
_, err = dao.NewStaffInfoDao().Create(staff)
|
|
|
|
} else {
|
|
|
|
err = dao.NewStaffInfoDao().Update(staff)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 20:01:30 +08:00
|
|
|
|
2024-01-07 17:17:33 +08:00
|
|
|
func (s *Staff) SyncStaffSalary(month string) {
|
2023-09-04 19:14:02 +08:00
|
|
|
if month == "" {
|
|
|
|
month = time.Now().AddDate(0, -1, 0).Format("2006-01")
|
|
|
|
}
|
|
|
|
month = strings.ReplaceAll(month, "-", "")
|
2024-01-31 20:49:43 +08:00
|
|
|
|
2024-01-31 22:54:06 +08:00
|
|
|
staffs, err := dao.NewStaffInfoDao().Query(model.StaffInfoStatusNormal)
|
2024-01-31 20:49:43 +08:00
|
|
|
if err != nil {
|
2024-01-31 22:54:06 +08:00
|
|
|
log.Errorf("query staff db error :%s", err.Error())
|
2024-01-31 20:49:43 +08:00
|
|
|
return
|
|
|
|
}
|
2023-09-01 20:01:30 +08:00
|
|
|
for _, staff := range staffs {
|
2024-07-04 21:04:50 +08:00
|
|
|
|
|
|
|
// 已离职的员工不处理
|
2024-11-04 23:00:55 +08:00
|
|
|
if staff.LeaveDate != "" {
|
|
|
|
leaveTime, _ := time.ParseInLocation("2006-01-02", staff.LeaveDate, time.Local)
|
|
|
|
isLeaveMonth := goutil.If(cast.ToInt(leaveTime.Format("200601")) == cast.ToInt(month), true, false)
|
|
|
|
if !isLeaveMonth {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-01 20:01:30 +08:00
|
|
|
}
|
2024-01-31 22:54:06 +08:00
|
|
|
|
2024-11-04 23:00:55 +08:00
|
|
|
salary, err := dao.NewStaffSalaryDao().GetBy(staff.Username, month)
|
2024-01-31 20:49:43 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2024-11-01 13:19:45 +08:00
|
|
|
|
2024-11-05 12:44:09 +08:00
|
|
|
//附属员工直接生成工资单
|
|
|
|
if staff.Master != "" {
|
|
|
|
staffService := service.NewStaff(staff, nil)
|
|
|
|
salary, err = staffService.CalcSlaveSalary(salary, month)
|
|
|
|
} else {
|
|
|
|
config, err := dao.NewUserConfigDao().GetByUsername(staff.Username)
|
|
|
|
if err != nil || config == nil {
|
|
|
|
log.Errorf("username[%s] not config,skip!!", staff.Username)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
staffService := service.NewStaff(staff, config)
|
|
|
|
salary, err = staffService.CalcSalary(salary, month)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("username[%s] staffService.CalcSalary error :%s", staff.Username, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
2024-01-31 22:54:06 +08:00
|
|
|
}
|
2023-09-01 20:01:30 +08:00
|
|
|
|
|
|
|
if salary.Id == 0 {
|
|
|
|
_, err = dao.NewStaffSalaryDao().Create(salary)
|
|
|
|
} else {
|
|
|
|
err = dao.NewStaffSalaryDao().Update(salary)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("db error :%s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|