package pposRequest

import (
	"errors"
	"log"
	"net/url"
	"time"
)

//创建审批工单
func (that *PPOSClient) NewEventsCommonAuditRequest() *EventsCommonAuditRequest {
	return &EventsCommonAuditRequest{
		Request: Request{
			Client:     that,
			apiAddress: "/pub/audit/startCommonAudit",
		},
		bT: time.Now(),
	}
}

//
type EventsCommonAuditRequest struct {
	Request
	publicParamsSlice []string  //必传参数名
	bT                time.Time //耗时时间
}

func (that *EventsCommonAuditRequest) SetUnitId(value string) *EventsCommonAuditRequest {
	that.initParams()
	that.params.Set("unit_id", value) //业务单元id (manage_id)
	return that
}
func (that *EventsCommonAuditRequest) SetRelatedNo(value string) *EventsCommonAuditRequest {
	that.initParams()
	that.params.Set("related_no", value) //关联业务编号(审批的申请id)
	return that
}

func (that *EventsCommonAuditRequest) SetAuditTypeCode(value string) *EventsCommonAuditRequest {
	that.initParams()
	that.params.Set("audit_type_code", value) //审批业务类型关键字(project_requirements/field_application)
	return that
}

//Params ...
func (that *EventsCommonAuditRequest) Params() (url.Values, error) {
	that.initParams()
	_params := that.params
	//
	for _, _v := range that.publicParamsSlice {
		if _params.Get(_v) == "" {
			return nil, errors.New(that.apiAddress + "缺少参数:" + _v)
		}
	}
	return _params, nil
}

//Do ...
func (that *EventsCommonAuditRequest) Do() (bool, error) {
	//返回data
	_, _err := that.Client.GetData(that.apiAddress, that)
	log.Println(that.apiAddress+" 耗时: ", time.Since(that.bT))
	if _err != nil {
		return false, _err
	}
	return true, nil
}
