|
|
package chance
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/astaxie/beego"
|
|
|
"github.com/astaxie/beego/httplib"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
|
|
|
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
"opp/models"
|
|
|
"opp/protocol"
|
|
|
|
|
|
"encoding/json"
|
|
|
)
|
|
|
|
|
|
type SuplusApprove struct {
|
|
|
}
|
|
|
|
|
|
var (
|
|
|
ErrorRequestSuplus = fmt.Errorf("请求素加后台失败")
|
|
|
)
|
|
|
|
|
|
var ApproveSign = "CHANCE"
|
|
|
|
|
|
const (
|
|
|
NewApproveInstance = "/approve-instances/new-approve-instance"
|
|
|
ApproveItem = "/approve-processes/approve-item"
|
|
|
ApproveIntegrate = "/approve-processes/integrate"
|
|
|
)
|
|
|
|
|
|
//新增审批实例
|
|
|
func (s SuplusApprove) NewApproveInstance(header *protocol.RequestHeader, process []*models.AuditFlowProcess) (err error) {
|
|
|
var (
|
|
|
request NewApproveInstanceRequest
|
|
|
response NewApproveInstanceResponse
|
|
|
mapApproves map[int]Approve = make(map[int]Approve)
|
|
|
relativeId int64
|
|
|
userCompany *models.UserCompany
|
|
|
)
|
|
|
if userCompany, err = models.GetUserCompanyByUserId(header.UserId, header.CompanyId); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
request = NewApproveInstanceRequest{
|
|
|
CompanyId: header.CompanyId,
|
|
|
Approver: userCompany.NickName,
|
|
|
Uid: userCompany.Id,
|
|
|
Sign: ApproveSign,
|
|
|
Type: 1,
|
|
|
}
|
|
|
for i := range process {
|
|
|
var (
|
|
|
approve Approve
|
|
|
approveType int //审批类型
|
|
|
approveWay int //审批方式
|
|
|
approver *models.User
|
|
|
)
|
|
|
item := process[i]
|
|
|
if relativeId == 0 {
|
|
|
relativeId = item.ChanceId
|
|
|
}
|
|
|
if _, ok := mapApproves[i]; ok {
|
|
|
approve = mapApproves[i]
|
|
|
} else {
|
|
|
approveType = item.AuditFlowType
|
|
|
if approveType == protocol.AuditBySpecailUser {
|
|
|
approveType = protocol.AuditByUser
|
|
|
}
|
|
|
if item.ActionType == protocol.ActionTypeAnd {
|
|
|
approveWay = 1 // 会签
|
|
|
} else {
|
|
|
approveWay = 2 //或签
|
|
|
}
|
|
|
mapApproves[i] = Approve{
|
|
|
ApproveType: item.AuditFlowType,
|
|
|
ApproveWay: approveWay,
|
|
|
}
|
|
|
}
|
|
|
if approver, err = models.GetUserByUcid(item.Uid); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
approve.ApproveUsers = append(approve.ApproveUsers, ApproveUsers{
|
|
|
Uid: item.Uid,
|
|
|
Name: approver.NickName,
|
|
|
})
|
|
|
}
|
|
|
request.RelativeId = relativeId
|
|
|
for i := 0; i < len(mapApproves); i++ {
|
|
|
if v, ok := mapApproves[i]; ok {
|
|
|
request.Approves = append(request.Approves, v)
|
|
|
}
|
|
|
}
|
|
|
if s.DoRequest(NewApproveInstance, http.MethodPost, request, &response); err != nil {
|
|
|
log.Error(err)
|
|
|
}
|
|
|
log.Info(fmt.Sprintf("success request suplus-approve:%v request:%v response:%v", NewApproveInstance, common.AssertJson(request), common.AssertJson(response)))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//审批操作
|
|
|
func (s SuplusApprove) ApproveItem(header *protocol.RequestHeader, approveType int, chanceId int64) (response ApproveItemResponse, err error) {
|
|
|
var (
|
|
|
request ApproveItemRequest = ApproveItemRequest{
|
|
|
RelativeId: chanceId,
|
|
|
Type: approveType,
|
|
|
Suggestion: "",
|
|
|
Sign: ApproveSign,
|
|
|
ApproveType: 2,
|
|
|
Uid: header.UserId,
|
|
|
}
|
|
|
)
|
|
|
if s.DoRequest(ApproveItem, http.MethodPost, request, &response); err != nil {
|
|
|
log.Error(err)
|
|
|
}
|
|
|
log.Info(fmt.Sprintf("success request suplus-approve:%v request:%v response:%v", ApproveItem, common.AssertJson(request), common.AssertJson(response)))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//审批列表
|
|
|
func (s SuplusApprove) ApproveIntegrate(header *protocol.RequestHeader, chanceId int64) (response ApproveIntegrateResponse, err error) {
|
|
|
var ()
|
|
|
url := fmt.Sprintf("%v?sign=%v&relativeId=%v", ApproveIntegrate, ApproveSign, chanceId)
|
|
|
if s.DoRequest(url, http.MethodGet, nil, &response); err != nil {
|
|
|
log.Error(err)
|
|
|
}
|
|
|
log.Info(fmt.Sprintf("success request suplus-approve:%v response:%v", url, common.AssertJson(response)))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//请求
|
|
|
func (s SuplusApprove) DoRequest(method string, methodType string, request interface{}, response interface{}) (err error) {
|
|
|
var (
|
|
|
httpRsp *http.Response
|
|
|
httpReq *httplib.BeegoHTTPRequest
|
|
|
url = beego.AppConfig.String("suplus_host") + method
|
|
|
data []byte
|
|
|
message protocol.Message
|
|
|
)
|
|
|
if methodType == http.MethodPost {
|
|
|
httpReq = httplib.Post(url)
|
|
|
} else if methodType == http.MethodGet {
|
|
|
httpReq = httplib.Get(url)
|
|
|
}
|
|
|
if request != nil {
|
|
|
httpReq.JSONBody(request)
|
|
|
}
|
|
|
if httpRsp, err = httpReq.DoRequest(); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
data, err = ioutil.ReadAll(httpRsp.Body)
|
|
|
defer httpRsp.Body.Close()
|
|
|
if err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if err = json.Unmarshal(data, &message); err != nil {
|
|
|
log.Debug(url, "response:", string(data))
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
if message.Errno != 0 {
|
|
|
err = fmt.Errorf("request-%v response-code:%v msg:%v", url, message.Errno, message.Errmsg)
|
|
|
}
|
|
|
var responseData string
|
|
|
if len(data) > 500 {
|
|
|
responseData = string(data[:200])
|
|
|
} else {
|
|
|
responseData = string(data)
|
|
|
}
|
|
|
if err = json.Unmarshal(message.Data, &response); err != nil {
|
|
|
log.Error(err)
|
|
|
return
|
|
|
}
|
|
|
log.Debug(fmt.Sprintf("request-%v request:%v rsp:%v", url, request, responseData))
|
|
|
return
|
|
|
}
|
|
|
|
|
|
/*NewApproveInstance 新增审批实例*/
|
|
|
type NewApproveInstanceRequest struct {
|
|
|
Approves []Approve `json:"approves"` //审批节点列表
|
|
|
CompanyId int64 `json:"companyId"` //公司id
|
|
|
Approver string `json:"approver"` //提交人
|
|
|
Uid int64 `json:"uid"` //提交人id
|
|
|
Sign string `json:"sign"` //标识 ORDER-订单 ELSERK-其他入库 CHANCE-机会
|
|
|
Type int `json:"type"` //1-新增 2-编辑
|
|
|
RelativeId int64 `json:"relativeId"` //关联id
|
|
|
|
|
|
}
|
|
|
type NewApproveInstanceResponse struct {
|
|
|
protocol.ErrorCode
|
|
|
Data []string `json:"data"`
|
|
|
}
|
|
|
|
|
|
//审批节点
|
|
|
type Approve struct {
|
|
|
ApproveWay int `json:"approveWay"` //审批方式 1 会签 2 或签 3 发起人从角色成员中自选
|
|
|
ApproveType int `json:"approveType"` //审批类型 1 业务区域负责人 2 指定成员 3 角色
|
|
|
ApproveUsers []ApproveUsers `json:"approveUsers"` //审批人
|
|
|
}
|
|
|
|
|
|
//审批人
|
|
|
type ApproveUsers struct {
|
|
|
Uid int64 `json:"uid"` //用户id
|
|
|
Name string `json:"name"` //用户名
|
|
|
IsDefault int `json:"isDefault"` //1 是默认 0 不是
|
|
|
RoleName string `json:"roleName"` //角色名/负责人
|
|
|
RoleId string `json:"roleId"` //角色id
|
|
|
ParentIds []int `json:"parentIds"`
|
|
|
}
|
|
|
|
|
|
/*ApproveItem 审批操作*/
|
|
|
type ApproveItemRequest struct {
|
|
|
RelativeId int64 `json:"relativeId"` //关联id
|
|
|
Type int `json:"type"` //审批类型 2 同意 3 驳回 4 撤回 5 直接提交
|
|
|
Suggestion string `json:"suggestion"` //原因/建议
|
|
|
Sign string `json:"sign"` //CHANCE
|
|
|
ApproveType int `json:"approveType"` //1-新增(订单用新增) 2-编辑
|
|
|
Uid int64 `json:"uid"` //用户id
|
|
|
}
|
|
|
type ApproveItemResponse struct {
|
|
|
Status int `json:"status"` //状态1-待确认2-已确认3-已驳回4-已撤回
|
|
|
ApproveStatus int `json:"approveStatus"` //审批状态0 待审批 1 审批中 2 同意 3 驳回 4 提交订单 5 已被其他人处理 (0这个状态不存在审批后)
|
|
|
IsApprove int `json:"isApprove"` //是否需要审批 1是 0 否
|
|
|
}
|
|
|
|
|
|
/*ApproveIntegrate 审核列表*/
|
|
|
type ApproveIntegrateRequest struct {
|
|
|
}
|
|
|
type ApproveIntegrateResponse struct {
|
|
|
} |
...
|
...
|
|