enterprise/worker/staff.go

215 lines
6.8 KiB
Go

package worker
import (
"enterprise/common/config"
"enterprise/common/dao"
"enterprise/common/global"
"enterprise/common/model"
"enterprise/service"
"fmt"
"git.u8t.cn/open/gosdk/qyweixin"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"strings"
"time"
)
type Staff struct {
}
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))
message = append(message, fmt.Sprintf("基本工资:%s", staff.BaseSalary))
message = append(message, fmt.Sprintf("绩效工资:%s", staff.PerfSalary))
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())
}
}
}
func (s *Staff) SendStaffSalaryBill(month string) {
masterBlank := ""
staffSalarys, err := dao.NewStaffSalaryDao().Query(month, "", &masterBlank)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
for _, staffSalary := range staffSalarys {
if staffSalary.BaseSalary < 0.1 {
continue
}
userConfig, err := dao.NewUserConfigDao().GetByUsername(staffSalary.Username)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
message := make([]string, 0)
message = append(message, fmt.Sprintf("【工资单】[%s][%s]", staffSalary.Username, month))
message = append(message, fmt.Sprintf("基本工资:%.2f", staffSalary.BaseSalary))
message = append(message, fmt.Sprintf("出勤工资:%.2f", staffSalary.AttendSalary))
if staffSalary.AwardSalary >= 0.1 {
message = append(message, fmt.Sprintf("额外工资:%.2f", staffSalary.AwardSalary))
}
//附属工资
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))
}
}
message = append(message, fmt.Sprintf("社保扣除:%.2f", staffSalary.SocialInsurence))
message = append(message, fmt.Sprintf("个税扣除:%.2f", staffSalary.PersonalTax))
message = append(message, fmt.Sprintf("实发工资:%.2f", staffSalary.GetRealSalary()+slaveSalary))
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
if userConfig != nil && cast.ToBool(userConfig.Get(model.StaffSalaryNotify)) == true {
if err := global.SendMessage([]string{userConfig.Username}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
}
}
}
func (s *Staff) SyncStaffInfo() {
cfg := config.GetConfig()
hrAssiant := qyweixin.NewAppHr(&qyweixin.AppConfig{
Corpid: cfg.QyWeixin.Corpid,
Secret: cfg.QyWeixin.EnterpriseSecret,
Agent: cfg.QyWeixin.EnterpriseAgent,
})
userConfigs, err := dao.NewUserConfigDao().Query(model.StaffConfigStatusNormal)
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
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"]
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())
}
}
}
func (s *Staff) SyncStaffSalary(month string) {
if month == "" {
month = time.Now().AddDate(0, -1, 0).Format("2006-01")
}
month = strings.ReplaceAll(month, "-", "")
staffs, err := dao.NewStaffInfoDao().Query(model.StaffInfoStatusNormal)
if err != nil {
log.Errorf("query staff db error :%s", err.Error())
return
}
for _, staff := range staffs {
// 已离职的员工不处理
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
}
}
salary, err := dao.NewStaffSalaryDao().GetBy(staff.Username, month)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
//附属员工直接生成工资单
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
}
}
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())
}
}
}