Files
ArmedPolice/app/model/work_instance.go

258 lines
10 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"ArmedPolice/app/common/model"
"ArmedPolice/serve/orm"
"fmt"
"strings"
"time"
)
type WorkInstance struct {
*model.WorkInstance
}
type (
// WorkInstanceInfo 基本信息
WorkInstanceInfo struct {
ID uint64 `json:"-"`
Title string `json:"title"`
OrderNo string `json:"order_no"`
Kind model.WorkInstanceKind `json:"kind"`
EquipmentID uint64 `json:"-"`
EquipmentCode string `json:"equipment_code"`
EquipmentTitle string `json:"equipment_title"`
Breakdown string `json:"-"`
BreakdownTitle string `json:"breakdown_title"`
ScheduleID uint64 `json:"-"`
ScheduleTitle string `json:"schedule_title"`
SupplierName string `json:"-"`
Priority int `json:"priority"`
Distribution string `json:"distribution"`
PlateNumber string `json:"plate_number"`
Remark string `json:"remark"`
Status int `json:"status"`
Username string `json:"username"`
CreatedAt time.Time `json:"created_at"`
}
// WorkInstanceStaticInfo 基本统计信息
WorkInstanceStaticInfo struct {
Count int64 `json:"count"`
Amount float64 `json:"amount"`
Date string `json:"date"`
}
)
// WorkbenchCondition 工作台条件
type WorkbenchCondition struct {
UID string
RoleIDs []string
WorkSchedule []uint64
}
func (m *WorkbenchCondition) roleInfo() string {
if len(m.RoleIDs) <= 0 {
return "''"
}
return strings.Join(m.RoleIDs, ",")
}
func (m *WorkbenchCondition) workScheduleInfo() string {
if len(m.WorkSchedule) <= 0 {
return "''"
}
out := make([]string, 0)
for _, v := range m.WorkSchedule {
out = append(out, fmt.Sprintf("%d", v))
}
return strings.Join(out, ",")
}
// Detail 详细信息
func (m *WorkInstance) Detail(id uint64) (*WorkInstanceInfo, error) {
db := orm.GetDB().Table(m.TableName()+" AS w").
Select("w.id", "w.kind", "w.title", "w.equipment_id", "e.code AS equipment_code", "e.title AS equipment_title", "w.priority",
"w.breakdown",
//"(SELECT GROUP_CONCAT(s_b.title) FROM ( SELECT id, title FROM sys_breakdown) AS s_b WHERE FIND_IN_SET( s_b.id, w.breakdown )) AS breakdown_title",
"w.schedule AS schedule_id", "s.title AS schedule_title", "supplier.name AS supplier_name", "w.distribution", "w.plate_number", "w.remark", "w.status", "w.created_at").
Joins(fmt.Sprintf("LEFT JOIN %s AS e ON w.equipment_id = e.id", model.NewManageEquipment().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS s ON w.schedule = s.id", model.NewWorkSchedule().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS supplier ON w.supplier_id = supplier.id", model.NewManageSupplier().TableName())).
Where("w.id = ?", id)
out := new(WorkInstanceInfo)
if err := db.Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
// Instances 基本信息
func (m *WorkInstance) Instances(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*WorkInstanceInfo, error) {
db := orm.GetDB().Table(m.TableName()+" AS w").
Select("w.id", "w.order_no", "w.title", "e.code AS equipment_code", "e.title AS equipment_title", "w.priority",
"w.breakdown",
//"(SELECT GROUP_CONCAT(s_b.title) FROM ( SELECT id, title FROM sys_breakdown) AS s_b WHERE FIND_IN_SET( s_b.id, w.breakdown )) AS breakdown_title",
"s.title AS schedule_title", "w.status", "u.name AS username", "w.created_at").
Joins(fmt.Sprintf("LEFT JOIN %s AS e ON w.equipment_id = e.id", model.NewManageEquipment().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS s ON w.schedule = s.id", model.NewWorkSchedule().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS u ON w.uid = u.uuid", model.NewSysUser().TableName())).
Where("w.is_deleted = ?", model.DeleteStatusForNot)
if len(where) > 0 {
for _, wo := range where {
db = db.Where(wo.Condition, wo.Value)
}
}
out := make([]*WorkInstanceInfo, 0)
if err := db.Count(count).Error; err != nil {
return nil, err
}
if err := db.Order("w.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
// Persons 个人信息
func (m *WorkInstance) Persons(uid uint64, page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*WorkInstanceInfo, error) {
db := orm.GetDB().Table(m.TableName()+" AS w").
Select("w.id", "w.order_no", "w.title", "e.code AS equipment_code", "e.title AS equipment_title", "w.priority",
"w.breakdown",
//"(SELECT GROUP_CONCAT(s_b.title) FROM ( SELECT id, title FROM sys_breakdown) AS s_b WHERE FIND_IN_SET( s_b.id, w.breakdown )) AS breakdown_title",
"s.title AS schedule_title", "w.status", "w.created_at").
Joins(fmt.Sprintf("LEFT JOIN %s AS e ON w.equipment_id = e.id", model.NewManageEquipment().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS s ON w.schedule = s.id", model.NewWorkSchedule().TableName())).
Where("w.uid = ? AND w.is_deleted = ?", uid, model.DeleteStatusForNot)
if len(where) > 0 {
for _, wo := range where {
db = db.Where(wo.Condition, wo.Value)
}
}
out := make([]*WorkInstanceInfo, 0)
if err := db.Count(count).Error; err != nil {
return nil, err
}
if err := db.Order("w.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
// Workbench 个人信息
func (m *WorkInstance) Workbench(condition *WorkbenchCondition, limit int, where ...*model.ModelWhere) ([]*WorkInstanceInfo, error) {
mWorkSchedule := model.NewWorkSchedule()
// _condition := fmt.Sprintf(`CASE %s
//WHEN %d THEN
//(SELECT GROUP_CONCAT(a.id) FROM (SELECT id FROM %s WHERE target = %d AND target_value IN (%s)) AS a)
//WHEN %d THEN
//(SELECT GROUP_CONCAT(a.id) FROM (SELECT id FROM %s WHERE target = %d AND target_value IN (%s)) AS a)
//ELSE "" END`, "s.target",
// model.WorkScheduleTargetForPerson,
// mWorkSchedule.TableName(), model.WorkScheduleTargetForPerson, condition.UID,
// model.WorkScheduleTargetForRole,
// mWorkSchedule.TableName(), model.WorkScheduleTargetForRole, condition.roleInfo())
db := orm.GetDB().Table(m.TableName()+" AS w").
Select("w.id", "w.order_no", "w.title", "e.code AS equipment_code", "e.title AS equipment_title", "w.priority",
"w.breakdown",
// TODO因mysql与sqlite3中Find_In_Set不兼容故只能将其注释更改为组合查询
//"(SELECT GROUP_CONCAT(s_b.title) FROM (SELECT id, title FROM sys_breakdown) AS s_b WHERE FIND_IN_SET( s_b.id, w.breakdown )) AS breakdown_title",
"s.title AS schedule_title", "w.status", "u.name AS username", "w.created_at").
Joins(fmt.Sprintf("LEFT JOIN %s AS e ON w.equipment_id = e.id", model.NewManageEquipment().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS s ON w.schedule = s.id", mWorkSchedule.TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS u ON w.uid = u.uuid", model.NewSysUser().TableName())).
Where("w.is_deleted = ?", model.DeleteStatusForNot).
Where("w.status = ?", model.WorkInstanceStatusForOngoing).
//Where(fmt.Sprintf("w.schedule IN (%s)", _condition))
Where("w.schedule IN (?)", condition.WorkSchedule)
if len(where) > 0 {
for _, wo := range where {
db = db.Where(wo.Condition, wo.Value)
}
}
out := make([]*WorkInstanceInfo, 0)
if err := db.Order("w.id " + model.OrderModeToDesc).Limit(limit).Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
// Workbenchs 待办信息
func (m *WorkInstance) Workbenchs(condition *WorkbenchCondition, page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*WorkInstanceInfo, error) {
mWorkSchedule := model.NewWorkSchedule()
// _condition := fmt.Sprintf(`CASE %s
//WHEN %d THEN
//(SELECT GROUP_CONCAT(a.id) FROM (SELECT id FROM %s WHERE target = %d AND target_value IN (%s)) AS a)
//WHEN %d THEN
//(SELECT GROUP_CONCAT(a.id) FROM (SELECT id FROM %s WHERE target = %d AND target_value IN (%s)) AS a)
//ELSE "" END`, "s.target",
// model.WorkScheduleTargetForPerson,
// mWorkSchedule.TableName(), model.WorkScheduleTargetForPerson, condition.UID,
// model.WorkScheduleTargetForRole,
// mWorkSchedule.TableName(), model.WorkScheduleTargetForRole, condition.roleInfo())
db := orm.GetDB().Table(m.TableName()+" AS w").
Select("w.id", "w.order_no", "w.title", "e.code AS equipment_code", "e.title AS equipment_title", "w.priority",
"w.breakdown",
//"(SELECT GROUP_CONCAT(s_b.title) FROM (SELECT id, title FROM sys_breakdown) AS s_b WHERE FIND_IN_SET( s_b.id, w.breakdown )) AS breakdown_title",
"s.title AS schedule_title", "w.status", "u.name AS username", "w.created_at").
Joins(fmt.Sprintf("LEFT JOIN %s AS e ON w.equipment_id = e.id", model.NewManageEquipment().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS s ON w.schedule = s.id", mWorkSchedule.TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS u ON w.uid = u.uuid", model.NewSysUser().TableName())).
Where("w.is_deleted = ?", model.DeleteStatusForNot).
Where("w.status = ?", model.WorkInstanceStatusForOngoing).
//Where(fmt.Sprintf("w.schedule IN (%s)", _condition))
Where("w.schedule IN (?)", condition.WorkSchedule)
if len(where) > 0 {
for _, wo := range where {
db = db.Where(wo.Condition, wo.Value)
}
}
out := make([]*WorkInstanceInfo, 0)
if err := db.Count(count).Error; err != nil {
return nil, err
}
if err := db.Order("w.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
func (m *WorkInstance) Static(where ...*model.ModelWhere) ([]*WorkInstanceStaticInfo, error) {
db := orm.GetDB().Table(m.TableName()+" AS w").
Select("COUNT(w.id) AS count", "SUM(m.price * w_m.material_number) AS amount",
"SUBSTRING(w.created_at, 1, 10) AS date").
Joins(fmt.Sprintf("LEFT JOIN %s AS w_m ON w.id = w_m.work_id", model.NewWorkMaterial().TableName())).
Joins(fmt.Sprintf("LEFT JOIN %s AS m ON w_m.material_id = m.id", model.NewManageMaterial().TableName())).
Where("w.is_deleted = ?", model.DeleteStatusForNot)
if len(where) > 0 {
for _, wo := range where {
db = db.Where(wo.Condition, wo.Value)
}
}
out := make([]*WorkInstanceStaticInfo, 0)
if err := db.Group("SUBSTRING(w.created_at, 1, 10)").Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
func NewWorkInstance() *WorkInstance {
return &WorkInstance{model.NewWorkInstance()}
}