enterprise/common/weixin/qyweixin_approve.go

244 lines
7.2 KiB
Go
Raw Normal View History

2023-08-13 21:24:54 +08:00
package weixin
import (
"encoding/json"
butil "enterprise/base/util"
2024-01-07 22:54:33 +08:00
"enterprise/common/model"
2023-08-13 21:24:54 +08:00
"fmt"
log "github.com/sirupsen/logrus"
2023-09-04 19:14:02 +08:00
"github.com/smbrave/goutil"
"github.com/spf13/cast"
2024-01-07 22:54:33 +08:00
"strings"
"time"
2023-08-13 21:24:54 +08:00
)
type Applyer struct {
Userid string `json:"userid"`
Partyid string `json:"partyid"`
}
type Option struct {
Key string `json:"key"`
Value []struct {
Text string `json:"text"`
Lang string `json:"lang"`
} `json:"value"`
}
type Selector struct {
Type string `json:"type"`
Options []*Option `json:"options"`
}
2023-09-04 19:14:02 +08:00
type Vacation struct {
Selector *Selector `json:"selector"`
Attendance struct {
DateRange struct {
NewBegin int64 `json:"new_begin"`
NewEnd int64 `json:"new_end"`
NewDuration int64 `json:"new_duration"`
Type string `json:"type"`
} `json:"date_range"`
} `json:"attendance"`
}
2023-08-13 21:24:54 +08:00
type ApplyValue struct {
Text string `json:"text"`
Selector *Selector `json:"selector"`
Children []interface{} `json:"children"`
Date struct {
Type string `json:"type"`
Timestamp string `json:"s_timestamp"`
} `json:"date"`
NewMoney string `json:"new_Money"`
Files []struct {
FileId string `json:"file_id"`
} `json:"files"`
2024-01-07 18:53:21 +08:00
Vacation *Vacation `json:"vacation"`
PunchCorrection interface{} `json:"punch_correction"`
2023-08-13 21:24:54 +08:00
}
type ApplyContent struct {
Control string `json:"control"`
Title []struct {
Text string `json:"text"`
Lang string `json:"lang"`
} `json:"title"`
Value *ApplyValue `json:"value"`
}
type ApproveDetail struct {
SpNo string `json:"sp_no"`
SpName string `json:"sp_name"`
SpStatus int `json:"sp_status"`
TemplateID string `json:"template_id"`
ApplyTime int64 `json:"apply_time"`
Applyer *Applyer `json:"applyer"`
ApplyData struct {
Contents []*ApplyContent `json:"contents"`
} `json:"apply_data"`
}
type ApproveDetailRsp struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Info *ApproveDetail `json:"info"`
}
type QyWeixinApprove struct {
QyWeixin
}
2023-09-04 19:14:02 +08:00
func (d *ApproveDetail) GetValue(title string) string {
for _, content := range d.ApplyData.Contents {
key := content.Title[0].Text
if key != title {
continue
}
var value string
if content.Control == "Selector" {
value = content.Value.Selector.Options[0].Value[0].Text
} else if content.Control == "Text" || content.Control == "Textarea" {
value = content.Value.Text
} else if content.Control == "Date" {
value = content.Value.Date.Timestamp
} else if content.Control == "Money" {
value = content.Value.NewMoney
} else if content.Control == "File" {
value = content.Value.Files[0].FileId
2024-01-07 22:54:33 +08:00
} else if content.Control == "Vacation" { //请假 请假类型,开始时间,结束时间,请假时长
2023-09-04 19:14:02 +08:00
tp := content.Value.Vacation.Selector.Options[0].Value[0].Text
2024-01-07 22:54:33 +08:00
value = tp + "," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewBegin) +
"," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewEnd) +
"," + cast.ToString(content.Value.Vacation.Attendance.DateRange.NewDuration)
2024-01-07 18:53:21 +08:00
} else if content.Control == "PunchCorrection" { //补卡:日期,时间,状态
mp := cast.ToStringMap(content.Value.PunchCorrection)
ddate := cast.ToString(mp["daymonthyear"])
dtime := cast.ToString(mp["time"])
if ddate == "" {
ddate = dtime
}
value = ddate + "," + dtime + "," + cast.ToString(mp["state"])
2023-09-04 19:14:02 +08:00
}
return value
}
return ""
}
2024-01-07 18:53:21 +08:00
func (d *ApproveDetail) String() string {
return goutil.EncodeJSONIndent(d)
}
2023-09-04 19:14:02 +08:00
func (d *ApproveDetail) GetUserid() string {
return d.Applyer.Userid
}
2024-01-07 22:54:33 +08:00
func (d *ApproveDetail) ToVacation() *model.ApprovalVacation {
vacation := new(model.ApprovalVacation)
vacation.SpNo = d.SpNo
vacation.Username = d.GetUserid()
vacation.ApplyTime = goutil.TimeToDateTime(d.ApplyTime)
fields := strings.SplitN(d.GetValue("请假类型"), ",", 4)
dTime := cast.ToInt64(fields[1])
vacation.VacationType = fields[0]
vacation.VacationDate = goutil.TimeToDate(dTime)
vacation.VacationStartTime = goutil.TimeToDateTime(cast.ToInt64(fields[1]))
vacation.VacationEndTime = goutil.TimeToDateTime(cast.ToInt64(fields[2]))
vacation.VacationDuration = float64(cast.ToInt64(fields[3])) / float64(3600*8)
vacation.VacationRemark = d.GetValue("请假事由")
vacation.Month = time.Unix(dTime, 0).Format("200601")
return vacation
}
func (d *ApproveDetail) ToRefund() *model.ApprovalRefund {
refund := new(model.ApprovalRefund)
refund.SpNo = d.SpNo
refund.Username = d.GetUserid()
refund.ApplyTime = goutil.TimeToDateTime(d.ApplyTime)
refund.Status = model.ApprovalRefundStatusCreated
refund.RefundType = d.GetValue("报销类型")
refundTime := cast.ToInt64(d.GetValue("发生时间"))
refund.Month = time.Unix(refundTime, 0).Format("200601")
refund.RefundDate = goutil.TimeToDateTime(refundTime)
refund.RefundAmount = cast.ToFloat64(d.GetValue("报销费用"))
refund.RefundRemark = d.GetValue("报销说明")
return refund
}
func (d *ApproveDetail) ToCheckin() *model.ApprovalCheckin {
ac := new(model.ApprovalCheckin)
value := strings.SplitN(d.GetValue("补卡"), ",", 3)
ac.SpNo = d.SpNo
ac.Username = d.GetUserid()
ac.ApplyTime = goutil.TimeToDateTime(d.ApplyTime)
ac.CheckinRemark = d.GetValue("补卡事由")
ac.CheckinDate = goutil.TimeToDate(cast.ToInt64(value[0]))
ac.Month = time.Unix(cast.ToInt64(value[0]), 0).Format("200601")
ac.CheckinTime = goutil.TimeToDateTime(cast.ToInt64(value[1]))
ac.CheckinType = value[2]
return ac
}
2023-08-13 21:24:54 +08:00
func NewQyWeixinApprove(corpId, secret, agent string) *QyWeixinApprove {
return &QyWeixinApprove{
QyWeixin: QyWeixin{
CorpId: corpId,
Secret: secret,
Agent: agent,
},
}
}
func (q *QyWeixinApprove) GetDetail(spNo string) (*ApproveDetail, error) {
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token=%s", q.GetToken())
reqParam := fmt.Sprintf(`{"sp_no":"%s"}`, spNo)
rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte(reqParam))
if err != nil {
return nil, err
}
var rsp ApproveDetailRsp
2024-01-07 18:53:21 +08:00
mp := make(map[string]interface{})
json.Unmarshal(rspBody, &mp)
//fmt.Println(goutil.EncodeJSONIndent(mp))
2023-08-13 21:24:54 +08:00
if err := json.Unmarshal(rspBody, &rsp); err != nil {
log.Errorf("get body[%s] json error :%s", string(rspBody), err.Error())
return nil, err
}
return rsp.Info, nil
}
2023-09-04 19:14:02 +08:00
func (q *QyWeixinApprove) GetList(start, end int64, templateId string) ([]string, error) {
reqUrl := fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token=%s", q.GetToken())
reqParam := make(map[string]interface{})
reqParam["starttime"] = cast.ToString(start)
reqParam["endtime"] = cast.ToString(end)
reqParam["new_cursor"] = ""
reqParam["size"] = 100
filters := make([]interface{}, 0)
if templateId != "" {
filters = append(filters, map[string]interface{}{
"key": "template_id",
"value": templateId,
})
}
filters = append(filters, map[string]interface{}{
"key": "sp_status",
"value": "2",
})
reqParam["filters"] = filters
rspBody, err := butil.HttpPostJson(reqUrl, nil, []byte(goutil.EncodeJSON(reqParam)))
if err != nil {
log.Errorf("httpPost error :%s", err.Error())
return nil, err
}
result, err := q.GetResult(rspBody)
if err != nil {
return nil, err
}
return cast.ToStringSlice(result["sp_no_list"]), nil
}