ferry/pkg/service/workOrderList.go

223 lines
6.6 KiB
Go
Raw Normal View History

2020-07-15 01:40:56 +08:00
package service
import (
"encoding/json"
2020-07-17 01:20:25 +08:00
"ferry/global/orm"
"ferry/models/process"
2020-11-15 20:13:17 +08:00
"ferry/models/system"
2020-07-15 01:40:56 +08:00
"ferry/pkg/pagination"
2020-07-17 01:20:25 +08:00
"ferry/tools"
2020-07-15 01:40:56 +08:00
"fmt"
"github.com/gin-gonic/gin"
)
/*
@Author : lanyulei
2020-07-25 14:20:15 +08:00
@todo: 添加新的处理人时候需要修改先完善功能后续有时间的时候优化一下这部分
2020-07-15 01:40:56 +08:00
*/
2020-07-23 00:42:46 +08:00
type WorkOrder struct {
Classify int
GinObj *gin.Context
}
type workOrderInfo struct {
process.WorkOrderInfo
Principals string `json:"principals"`
StateName string `json:"state_name"`
2020-07-23 00:42:46 +08:00
DataClassify int `json:"data_classify"`
ProcessName string `json:"process_name"`
2020-07-23 00:42:46 +08:00
}
2021-03-10 23:14:28 +08:00
func NewWorkOrder(classify int, c *gin.Context) *WorkOrder {
return &WorkOrder{
Classify: classify,
GinObj: c,
}
}
2020-07-23 00:42:46 +08:00
func (w *WorkOrder) PureWorkOrderList() (result interface{}, err error) {
2020-07-15 01:40:56 +08:00
var (
workOrderInfoList []workOrderInfo
2020-11-22 16:46:16 +08:00
processorInfo system.SysUser
2020-07-15 01:40:56 +08:00
)
personSelectValue := "(JSON_CONTAINS(p_work_order_info.state, JSON_OBJECT('processor', %v)) and JSON_CONTAINS(p_work_order_info.state, JSON_OBJECT('process_method', 'person')))"
roleSelectValue := "(JSON_CONTAINS(p_work_order_info.state, JSON_OBJECT('processor', %v)) and JSON_CONTAINS(p_work_order_info.state, JSON_OBJECT('process_method', 'role')))"
departmentSelectValue := "(JSON_CONTAINS(p_work_order_info.state, JSON_OBJECT('processor', %v)) and JSON_CONTAINS(p_work_order_info.state, JSON_OBJECT('process_method', 'department')))"
2020-11-22 16:46:16 +08:00
2020-07-23 00:42:46 +08:00
title := w.GinObj.DefaultQuery("title", "")
2020-11-22 16:46:16 +08:00
startTime := w.GinObj.DefaultQuery("startTime", "")
endTime := w.GinObj.DefaultQuery("endTime", "")
isEnd := w.GinObj.DefaultQuery("isEnd", "")
processor := w.GinObj.DefaultQuery("processor", "")
priority := w.GinObj.DefaultQuery("priority", "")
creator := w.GinObj.DefaultQuery("creator", "")
2020-11-22 16:46:16 +08:00
db := orm.Eloquent.Model(&process.WorkOrderInfo{}).
Where("p_work_order_info.title like ?", fmt.Sprintf("%%%v%%", title))
2020-11-22 16:46:16 +08:00
if startTime != "" {
db = db.Where("p_work_order_info.create_time >= ?", startTime)
2020-11-22 16:46:16 +08:00
}
if endTime != "" {
db = db.Where("p_work_order_info.create_time <= ?", endTime)
2020-11-22 16:46:16 +08:00
}
if isEnd != "" {
db = db.Where("p_work_order_info.is_end = ?", isEnd)
2020-11-22 16:46:16 +08:00
}
if creator != "" {
db = db.Where("p_work_order_info.creator = ?", creator)
}
2020-11-22 16:46:16 +08:00
if processor != "" && w.Classify != 1 {
err = orm.Eloquent.Model(&processorInfo).
Where("user_id = ?", processor).
Find(&processorInfo).Error
if err != nil {
return
}
db = db.Where(fmt.Sprintf("(%v or %v or %v) and p_work_order_info.is_end = 0",
2020-11-22 16:46:16 +08:00
fmt.Sprintf(personSelectValue, processorInfo.UserId),
fmt.Sprintf(roleSelectValue, processorInfo.RoleId),
fmt.Sprintf(departmentSelectValue, processorInfo.DeptId),
))
}
if priority != "" {
db = db.Where("p_work_order_info.priority = ?", priority)
2020-11-22 16:46:16 +08:00
}
2020-07-15 01:40:56 +08:00
// 获取当前用户信息
2020-07-23 00:42:46 +08:00
switch w.Classify {
2020-07-15 01:40:56 +08:00
case 1:
// 待办工单
// 1. 个人
2020-11-22 16:46:16 +08:00
personSelect := fmt.Sprintf(personSelectValue, tools.GetUserId(w.GinObj))
2020-07-15 01:40:56 +08:00
2020-11-15 14:52:09 +08:00
// 2. 角色
2020-11-22 16:46:16 +08:00
roleSelect := fmt.Sprintf(roleSelectValue, tools.GetRoleId(w.GinObj))
2020-07-15 01:40:56 +08:00
// 3. 部门
2020-11-15 20:13:17 +08:00
var userInfo system.SysUser
err = orm.Eloquent.Model(&system.SysUser{}).
Where("user_id = ?", tools.GetUserId(w.GinObj)).
Find(&userInfo).Error
if err != nil {
return
}
2020-11-22 16:46:16 +08:00
departmentSelect := fmt.Sprintf(departmentSelectValue, userInfo.DeptId)
2020-07-15 01:40:56 +08:00
2020-07-20 23:57:42 +08:00
// 4. 变量会转成个人数据
2020-07-17 01:20:25 +08:00
//db = db.Where(fmt.Sprintf("(%v or %v or %v or %v) and is_end = 0", personSelect, personGroupSelect, departmentSelect, variableSelect))
db = db.Where(fmt.Sprintf("(%v or %v or %v) and p_work_order_info.is_end = 0", personSelect, roleSelect, departmentSelect))
2020-07-15 01:40:56 +08:00
case 2:
// 我创建的
db = db.Where("p_work_order_info.creator = ?", tools.GetUserId(w.GinObj))
2020-07-15 01:40:56 +08:00
case 3:
// 我相关的
db = db.Where(fmt.Sprintf("JSON_CONTAINS(p_work_order_info.related_person, '%v')", tools.GetUserId(w.GinObj)))
2020-07-15 01:40:56 +08:00
case 4:
// 所有工单
default:
return nil, fmt.Errorf("请确认查询的数据类型是否正确")
}
db = db.Joins("left join p_process_info on p_work_order_info.process = p_process_info.id").
Select("p_work_order_info.*, p_process_info.name as process_name")
2020-07-15 01:40:56 +08:00
result, err = pagination.Paging(&pagination.Param{
2020-07-23 00:42:46 +08:00
C: w.GinObj,
2020-07-15 01:40:56 +08:00
DB: db,
}, &workOrderInfoList, map[string]map[string]interface{}{}, "p_process_info")
2020-07-15 01:40:56 +08:00
if err != nil {
err = fmt.Errorf("查询工单列表失败,%v", err.Error())
return
}
2020-07-23 00:42:46 +08:00
return
}
func (w *WorkOrder) WorkOrderList() (result interface{}, err error) {
var (
principals string
StateList []map[string]interface{}
workOrderInfoList []workOrderInfo
minusTotal int
2020-07-23 00:42:46 +08:00
)
result, err = w.PureWorkOrderList()
if err != nil {
return
}
2020-07-15 01:40:56 +08:00
for i, v := range *result.(*pagination.Paginator).Data.(*[]workOrderInfo) {
var (
stateName string
structResult map[string]interface{}
authStatus bool
)
2020-11-15 20:13:17 +08:00
err = json.Unmarshal(v.State, &StateList)
if err != nil {
err = fmt.Errorf("json反序列化失败%v", err.Error())
return
}
2020-07-15 01:40:56 +08:00
if len(StateList) != 0 {
2020-10-09 00:58:14 +08:00
// 仅待办工单需要验证
// todo还需要找最优解决方案
if w.Classify == 1 {
structResult, err = ProcessStructure(w.GinObj, v.Process, v.Id)
if err != nil {
return
}
authStatus, err = JudgeUserAuthority(w.GinObj, v.Id, structResult["workOrder"].(WorkOrderData).CurrentState)
if err != nil {
return
}
if !authStatus {
minusTotal += 1
continue
}
} else {
authStatus = true
}
2020-07-15 01:40:56 +08:00
processorList := make([]int, 0)
if len(StateList) > 1 {
for _, s := range StateList {
for _, p := range s["processor"].([]interface{}) {
if int(p.(float64)) == tools.GetUserId(w.GinObj) {
processorList = append(processorList, int(p.(float64)))
}
}
if len(processorList) > 0 {
stateName = s["label"].(string)
break
}
}
}
if len(processorList) == 0 {
for _, v := range StateList[0]["processor"].([]interface{}) {
processorList = append(processorList, int(v.(float64)))
}
stateName = StateList[0]["label"].(string)
2020-07-15 01:40:56 +08:00
}
principals, err = GetPrincipal(processorList, StateList[0]["process_method"].(string))
if err != nil {
err = fmt.Errorf("查询处理人名称失败,%v", err.Error())
return
}
}
workOrderDetails := *result.(*pagination.Paginator).Data.(*[]workOrderInfo)
workOrderDetails[i].Principals = principals
workOrderDetails[i].StateName = stateName
workOrderDetails[i].DataClassify = v.Classify
if authStatus {
workOrderInfoList = append(workOrderInfoList, workOrderDetails[i])
}
2020-07-15 01:40:56 +08:00
}
result.(*pagination.Paginator).Data = &workOrderInfoList
result.(*pagination.Paginator).TotalCount -= minusTotal
2020-07-15 01:40:56 +08:00
return result, nil
}