// ============================================================================
// This is auto-generated by gf cli tool only once. Fill this file as you wish.
// ============================================================================

package wf_run_process

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

type SearchRunProcess struct {
	RunId          uint   `json:"run_id"`
	RunFlow        uint   `json:"run_flow"`
	RunFlowProcess string `json:"run_flow_process"`
	Status         string `json:"status"`
}

type SaveRunProcessData struct {
	Uid               uint   `p:"uid"                 json:"uid"`                 //
	RunId             uint   `p:"run_id"              json:"run_id"`              // 当前流转id
	RunFlow           uint   `p:"run_flow"            json:"run_flow"`            // 属于那个流程的id
	RunFlowProcess    uint   `p:"run_flow_process"    json:"run_flow_process"`    // 当前步骤编号
	ParentFlow        uint   `p:"parent_flow"         json:"parent_flow"`         // 上一步流程
	ParentFlowProcess uint   `p:"parent_flow_process" json:"parent_flow_process"` // 上一步骤号
	RunChild          uint   `p:"run_child"           json:"run_child"`           // 开始转入子流程run_id 如果转入子流程，则在这里也记录
	Remark            string `p:"remark"              json:"remark"`              // 备注
	IsSponsor         uint   `p:"is_sponsor"          json:"is_sponsor"`          // 是否步骤主办人 0否(默认) 1是
	Status            uint   `p:"status"              json:"status"`              // 状态 0为未接收（默认），1为办理中 ,2为已转交,3为已结束4为已打回
	SponsorIds        string `p:"sponsor_ids"         json:"sponsor_ids"`         //
	SponsorText       string `p:"sponsor_text"        json:"sponsor_text"`        //
	AutoPerson        int    `p:"auto_person"         json:"auto_person"`         //
	JsTime            uint   `p:"js_time"             json:"js_time"`             // 接收时间
	Dateline          uint   `p:"dateline"            json:"dateline"`            //
	WfMode            int    `p:"wf_mode"             json:"wf_mode"`             //
	WfAction          string `p:"wf_action"           json:"wf_action"`           //
}

//获取流程运行步骤信息列表
func GetProcessList(where *SearchRunProcess) (list []*Entity, err error) {
	model := Model.Where(Columns.RunId, where.RunId).
		And(Columns.RunFlowProcess+" in (?)  OR FIND_IN_SET("+Columns.RunFlowProcess+",?)", where.RunFlowProcess, where.RunFlowProcess)
	if where.RunFlow != 0 {
		model = model.And(Columns.RunFlow, where.RunFlow)
	}
	if where.Status != "" {
		model = model.And(Columns.Status, gconv.Int(where.Status))
	}
	list, err = model.FindAll()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取流程运行步骤列表信息失败")
		return
	}
	return
}

//获取流程运行步骤信息
func GetProcess(where *SearchRunProcess) (info *Entity, err error) {
	model := Model.Where(Columns.RunId, where.RunId).
		And(Columns.RunFlowProcess+" in (?)  OR FIND_IN_SET("+Columns.RunFlowProcess+",?)", where.RunFlowProcess, where.RunFlowProcess)
	if where.RunFlow != 0 {
		model = model.And(Columns.RunFlow, where.RunFlow)
	}
	if where.Status != "" {
		model = model.And(Columns.Status, gconv.Int(where.Status))
	}
	info, err = model.FindOne()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取流程运行步骤信息失败")
		return
	}
	return
}

func GetProcessByMap(where g.Map) (list []*Entity, err error) {
	list, err = Model.FindAll(where)
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取流程运行步骤失败")
	}
	return
}

//通过id获取步骤运行信息
func GetProcessById(id uint) (info *Entity, err error) {
	info, err = Model.FindOne(id)
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取流程运转信息失败")
	}
	return
}

//获取本流程中小于ID的步骤信息
func GetProcessLtId(where *Entity) (list []*Entity, err error) {
	list, err = Model.Where("id < ?", where.Id).Where("run_flow", where.RunFlow).
		Where("run_id", where.RunId).FindAll()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取小于指定步骤流程运转信息失败")
	}
	return
}

func Add(data *SaveRunProcessData, tx *gdb.TX) error {
	_, err := Model.TX(tx).Save(data)
	if err != nil {
		g.Log().Error(err)
		return gerror.New("保存流程步骤日志失败")
	}
	return nil
}
