enterprise/worker/staff.go

189 lines
6.0 KiB
Go

package worker
import (
"enterprise/common/dao"
"enterprise/common/global"
"enterprise/common/model"
"enterprise/service"
"fmt"
log "github.com/sirupsen/logrus"
"strings"
"time"
)
type Staff struct {
}
func NewStaff() *Staff {
return &Staff{}
}
func (s *Staff) MontorWorkAge(corpId int64) {
staffs, _, err := dao.NewStaffUserDao().Query(1, -1, corpId, model.StaffUserStatusOnline, "", "", "", "")
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)
OfficialDate, _ := time.ParseInLocation("2006-01-02", staff.OfficialDate, time.Local)
entryMonth := (nowDate.Year()-entryDate.Year())*12 + int(nowDate.Month()) - int(entryDate.Month())
officalDay := (OfficialDate.Unix() - nowDate.Unix()) / 86400
log.Infof("staff[%s] entryDate[%s] spanMonth[%.1f]", staff.Username, staff.EntryDate, float64(entryMonth)/12.0)
salary := staff.GetSalary()
if nowDate.Day() == 1 && entryMonth%6 == 0 {
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(entryMonth)/12))
message = append(message, fmt.Sprintf("基本工资:%s", salary.Base))
message = append(message, fmt.Sprintf("绩效工资:%s", salary.Target))
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())
}
}
if officalDay > 0 && officalDay <= 30 && officalDay%7 == 0 {
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("转正时间:%s", staff.OfficialDate))
message = append(message, fmt.Sprintf("基本工资:%s", salary.Base))
message = append(message, fmt.Sprintf("绩效工资:%s", salary.Target))
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(corpId int64, month string) {
//staffSalarys, err := dao.NewStaffSalaryDao().QueryAll(corpId, month, model.StaffSalaryStatusPayed)
staffSalarys, err := dao.NewStaffSalaryDao().QueryAll(corpId, month, 0)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
for _, staffSalary := range staffSalarys {
if staffSalary.Salary < 0.1 {
continue
}
staffUser, err := dao.NewStaffUserDao().Get(staffSalary.UserId)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
message := make([]string, 0)
message = append(message, fmt.Sprintf("【工资单】[%s][%s]", staffUser.Username, month))
message = append(message, fmt.Sprintf("实发工资:%.2f", staffSalary.GetRealSalary()))
message = append(message, fmt.Sprintf(`<a href="%s">查看明细</a>`, staffSalary.GetBillUrl()))
if err := global.SendMessage([]string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
}
}
func (s *Staff) PayStaffSalary(corpId int64, month string) {
staffSalarys, err := dao.NewStaffSalaryDao().QueryAll(corpId, month, model.StaffSalaryStatusWait)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
corp, err := dao.NewCorpDao().Get(corpId)
if err != nil {
log.Errorf("db error :%s", err.Error())
return
}
for _, staffSalary := range staffSalarys {
if staffSalary.Salary < 0.1 {
continue
}
staffUser, err := dao.NewStaffUserDao().Get(staffSalary.UserId)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
message := make([]string, 0)
message = append(message, fmt.Sprintf("【工资发放】[%s]", staffUser.Realname))
message = append(message, fmt.Sprintf("实发工资:%.2f", staffSalary.GetRealSalary()))
message = append(message, fmt.Sprintf(`<a href="%s">查看明细</a>`, staffSalary.GetBillUrl()))
if staffUser.Username == "jiangyong" {
err = service.NewPay().Pay(corp, staffUser, model.StaffSalaryPaymentAlipay, "", int64(100*staffSalary.GetRealSalary()))
if err == nil {
staffSalary.Status = model.StaffSalaryStatusPayed
message = append(message, "发放成功")
} else {
staffSalary.SetExtra("pay_error", err.Error())
message = append(message, fmt.Sprintf("发动失败:%.2f", err.Error()))
}
dao.NewStaffSalaryDao().Update(staffSalary)
}
if err := global.SendCorpMessage(staffSalary.CorpId, []string{"jiangyong"}, strings.Join(message, "\n")); err != nil {
log.Errorf("send message error :%s", err.Error())
}
}
}
func (s *Staff) SyncStaffSalary(corpId int64, month string) {
if month == "" {
month = time.Now().AddDate(0, -1, 0).Format("2006-01")
}
month = strings.ReplaceAll(month, "-", "")
staffs, _, err := dao.NewStaffUserDao().Query(1, -1, corpId, 0, "", "", "", "")
if err != nil {
log.Errorf("query staff db error :%s", err.Error())
return
}
for _, staff := range staffs {
salary, err := dao.NewStaffSalaryDao().GetBy(corpId, staff.Id, month)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
staffService := service.NewStaffSalary(staff)
salary, err = staffService.CalcSalary(salary, month, "")
if err != nil {
log.Errorf("username[%s] staffService.CalcSalary error :%s", staff.Username, err.Error())
continue
}
if salary == nil {
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())
}
}
}