// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/pln"
	common "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/pln"
	"LAPP_ACURA_MOM_BACKEND/utils"
	"fmt"
	"github.com/go-xorm/xorm"
	"time"
	"xorm.io/core"
)

/******************************************************************************
 *
 * @Struct Name              : CustOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CustOrder的数据访问对象实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
type CustOrderDAOImplement struct {
	session *xorm.Session
	meta    *grmi.Entity
	plantNr int
	userid  string
}

/******************************************************************************
 *
 * @Function Name            : NewCustOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CustOrderDAOImplement实例
 *
 * @Function Parameters      : xorm会话
 *
 * @Function Parameters      : 基本主键
 *
 * @Return Value             : CustOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
func NewCustOrderDAOImplement(session *xorm.Session, plantNr int, userid string) *CustOrderDAOImplement {
	return &CustOrderDAOImplement{session, &meta.CustOrder, plantNr, userid}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.InsertOne
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) InsertOne(entity *model.CustOrder) error {
	entity.PlantNr = impl.plantNr
	entity.LastUser = impl.userid
	_, err := impl.session.Table(impl.meta.TableName).Insert(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.DeleteOne
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) DeleteOne(custOrderId string) error {
	_, err := impl.session.Table(impl.meta.TableName).ID(core.PK{impl.plantNr, custOrderId}).Delete(impl.meta.Indicator)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectOne
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectOne(custOrderId string) (*model.CustOrder, error) {
	//var data model.CustOrder
	//ok, err := impl.session.Table(impl.meta.TableName).ID(core.PK{impl.plantNr, custOrderId}).Get(&data)
	//if err != nil {
	//	return nil, grmi.NewDataBaseError(err)
	//}
	//if !ok {
	//	return nil, nil
	//}
	var data model.CustOrderData
	condition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	where := fmt.Sprintf("%s = ? and %s = ?", meta.CustOrder_PlantNr.ColumnName, meta.CustOrder_CustOrderId.ColumnName)
	parameters := []interface{}{impl.plantNr, custOrderId}
	ok, err := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, condition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition).Where(where, parameters...).Get(&data)
	if err != nil {
		return nil, err
	}
	if !ok {
		return nil, nil
	}
	var custOrder model.CustOrder
	custOrder = data.CustOrder
	custOrder.OrderStatus = data.CustOrderStatus
	custOrder.OrderQty = data.CustOrderQty
	////查询订单状态
	//var dataStatus model.CustOrderStatus
	//_, err = impl.session.Table(meta.CustOrderStatus.TableName).Where(meta.CustOrderStatus_CustOrderId.Name+"=?", data.CustOrderId).Get(&dataStatus)
	//if err != nil {
	//	return nil, grmi.NewDataBaseError(err)
	//}
	//data.OrderStatus = dataStatus
	return &custOrder, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.UpdateOne
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) UpdateOne(entity *model.CustOrder) error {
	entity.PlantNr = impl.plantNr
	entity.LastUser = impl.userid
	_, err := impl.session.Table(impl.meta.TableName).Cols(impl.meta.UpdatingMembers...).ID(entity.GetKey()).Update(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.Insert
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) Insert(entities *[]model.CustOrder) error {
	for i := 0; i < len(*entities); i++ {
		entity := &(*entities)[i]
		entity.PlantNr = impl.plantNr
		entity.LastUser = impl.userid
	}
	_, err := impl.session.Table(impl.meta.TableName).Insert(entities)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.Delete
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) Delete(entities *[]model.CustOrder) error {
	for _, entity := range *entities {
		entity.PlantNr = impl.plantNr
		_, err := impl.session.Table(impl.meta.TableName).ID(entity.GetKey()).Delete(impl.meta.Indicator)
		if err != nil {
			return grmi.NewDataBaseError(err)
		}
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.DeleteWhere
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) DeleteWhere(predicates []grmi.Predicate) error {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	_, err := session.Delete(impl.meta.Indicator)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.Select
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) Select(predicates []grmi.Predicate, orderByFields []grmi.Field) ([]model.CustOrder, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	session = session.OrderBy(meta.CustOrder_PlantNr.ColumnName)
	if orderByFields != nil {
		for _, field := range orderByFields {
			session = session.OrderBy(field.ColumnName)
		}
	}

	data := make([]model.CustOrder, 0, 10)
	err := session.Find(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	return data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectAndPaging
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectAndPaging(paging *grmi.Paging, predicates []grmi.Predicate, orderByFields []grmi.Field) (grmi.PagingResult, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	count, err := session.Count(impl.meta.Indicator)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}

	orderBy := " order by " + meta.CustOrder_PlantNr.ColumnName
	if orderByFields != nil {
		for _, field := range orderByFields {
			if orderBy == " order by " {
				orderBy += field.ColumnName
			} else {
				orderBy += ", " + field.ColumnName
			}
		}
	}
	parameters = append(parameters, paging.Offset(), paging.Size)
	data := make([]model.CustOrder, 0, 10)
	where = " where " + where
	err = impl.session.Table(impl.meta.TableName).SQL("select * from ["+impl.meta.TableName+"]"+where+orderBy+" offset ? row fetch next ? row only", parameters...).Find(&data)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	//查询状态
	var orderId []string
	for _, v := range data {
		orderId = append(orderId, v.CustOrderId)
	}
	//查询
	var statusData []model.CustOrderStatus
	err = impl.session.Table(meta.CustOrderStatus.TableName).In(meta.CustOrderStatus_CustOrderId.Name, orderId).Find(&statusData)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	statusMap := make(map[string]model.CustOrderStatus)
	for _, v := range statusData {
		statusMap[v.CustOrderId] = v
	}
	for k, v := range data {
		info, ok := statusMap[v.CustOrderId]
		if ok {
			data[k].OrderStatus = info
		}
	}
	return grmi.PagingResult{Records: data, Count: count, PageNumber: paging.Number, PageSize: paging.Size}, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.Update
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) Update(entities *[]model.CustOrder) error {
	for i := 0; i < len(*entities); i++ {
		entity := &(*entities)[i]
		entity.PlantNr = impl.plantNr
		entity.LastUser = impl.userid
		_, err := impl.session.Table(impl.meta.TableName).Cols(impl.meta.UpdatingMembers...).ID(entity.GetKey()).Update(entity)
		if err != nil {
			return grmi.NewDataBaseError(err)
		}
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.UpdateWhere
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) UpdateWhere(predicates []grmi.Predicate, entity *model.CustOrder, columns ...string) error {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	columns = append(columns, meta.CustOrder_LastUser.Name)
	entity.LastUser = impl.userid
	_, err := session.Cols(columns...).Update(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectOneWithStatus
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectOneWithStatus(custOrderId string) (*model.CustOrderWithStatus, error) {
	var data model.CustOrderWithStatus
	condition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	ok, err := impl.session.Table(impl.meta.TableName).Join("LEFT", meta.CustOrderStatus.TableName, condition).Where(fmt.Sprintf("%s = ?", meta.CustOrder_CustOrderId.ColumnName), custOrderId).Get(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	if !ok {
		return nil, nil
	}
	return &data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectWorkLineTaskBeyondStatus
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectWorkLineTaskBeyondStatus(startTime time.Time, endTime time.Time, workLineId string, status int) (custOrderLi []model.CustOrder, err error) {
	start := utils.TimeFormat(startTime, "yyyy-MM-dd HH:mm:ss")
	end := utils.TimeFormat(endTime, "yyyy-MM-dd HH:mm:ss")
	condition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	data := make([]model.CustOrderData, 0)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, condition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	where := fmt.Sprintf("%s = ? and %s = ? and %s >= ? and %s < ? and ((%s >= ? and %s <= ? ) or (%s >= ? and %s <= ?))",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_WorkLineId.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName)
	parameters := []interface{}{impl.plantNr, workLineId, status, common.WO_STATUS_FINISHED, start, end, start, end}
	err = session.Where(where, parameters...).Asc(meta.CustOrder_PlanStartTime.SortColumnName).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, item := range data {
		var custOrder model.CustOrder
		custOrder = item.CustOrder
		custOrder.OrderStatus = item.CustOrderStatus
		custOrder.OrderQty = item.CustOrderQty
		result = append(result, custOrder)
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectWorkLineTaskBeyondStatus
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectWorkLineTaskEqualStatus(startTime time.Time, endTime time.Time, workLineId string, status int) (custOrderLi []model.CustOrder, err error) {
	start := utils.TimeFormat(startTime, "yyyy-MM-dd HH:mm:ss")
	end := utils.TimeFormat(endTime, "yyyy-MM-dd HH:mm:ss")
	condition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	data := make([]model.CustOrderData, 0)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, condition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	where := fmt.Sprintf("%s = ? and %s = ? and %s = ? and ((%s >= ? and %s <= ? ) or (%s >= ? and %s <= ?))",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_WorkLineId.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName)
	parameters := []interface{}{impl.plantNr, workLineId, status, start, end, start, end}
	err = session.Where(where, parameters...).Asc(meta.CustOrder_PlanStartTime.SortColumnName).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, item := range data {
		var custOrder model.CustOrder
		custOrder = item.CustOrder
		custOrder.OrderStatus = item.CustOrderStatus
		custOrder.OrderQty = item.CustOrderQty
		result = append(result, custOrder)
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectUnPlannedCustOrder
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectUnPlannedCustOrder(workLineId string) (custOrderLi []model.CustOrder, err error) {
	condition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	data := make([]model.CustOrderData, 0)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, condition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	where := fmt.Sprintf("%s = ? and %s = ? and %s = ? ",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_WorkLineId.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
	)
	parameters := []interface{}{impl.plantNr, workLineId, common.WO_STATUS_UNPLANNED}
	err = session.Where(where, parameters...).Asc(meta.CustOrder_CreateTime.SortColumnName).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, item := range data {
		var custOrder model.CustOrder
		custOrder = item.CustOrder
		custOrder.OrderStatus = item.CustOrderStatus
		custOrder.OrderQty = item.CustOrderQty
		result = append(result, custOrder)
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectWaitReleaseCustOrder
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectWaitReleaseCustOrder(workLineId string) ([]model.CustOrder, error) {
	data := make([]model.CustOrderData, 0)
	session := impl.session.Table(impl.meta.TableName)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session = session.Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("LEFT", meta.CustOrderQty.TableName, qtyCondition)
	where := fmt.Sprintf("%s = ? and %s = ? and (%s = ? or %s = ? or %s = ?)",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_WorkLineId.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrderStatus_Status.ColumnName)
	err := session.Where(where, impl.plantNr, workLineId, common.WO_STATUS_LOCKED, common.WO_STATUS_RELEASED, common.WO_STATUS_RUNNING).Asc(meta.CustOrder_PlanStartTime.ColumnName).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, item := range data {
		var custOrder model.CustOrder
		custOrder = item.CustOrder
		custOrder.OrderStatus = item.CustOrderStatus
		custOrder.OrderQty = item.OrderQty
		result = append(result, custOrder)
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.UpdateOne
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) UpdateOneInfo(entity *model.CustOrder) error {
	entity.PlantNr = impl.plantNr
	entity.LastUser = impl.userid
	_, err := impl.session.Table(impl.meta.TableName).ID(entity.GetKey()).Update(entity)
	if err != nil {
		return grmi.NewDataBaseError(err)
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.SelectPageData
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) SelectPageData(paging *grmi.Paging, predicates []grmi.Predicate, orderByFields []grmi.Field) (grmi.PagingResult, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)

	session := impl.session.Table(impl.meta.TableName)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	count, err := session.Count(impl.meta.Indicator)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}

	orderBy := " order by " + meta.CustOrder_PlantNr.ColumnName
	if orderByFields != nil {
		for _, field := range orderByFields {
			if orderBy == " order by " {
				orderBy += field.ColumnName
			} else {
				orderBy += ", " + field.ColumnName
			}
		}
	}
	parameters = append(parameters, paging.Offset(), paging.Size)
	data := make([]model.CustOrder, 0, 10)
	where = " where " + where
	err = impl.session.Table(impl.meta.TableName).SQL("select * from ["+impl.meta.TableName+"]"+where+orderBy+" offset ? row fetch next ? row only", parameters...).Find(&data)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	//查询状态
	var orderId []string
	for _, v := range data {
		orderId = append(orderId, v.CustOrderId)
	}
	//查询
	var statusData []model.CustOrderStatus
	err = impl.session.Table(meta.CustOrderStatus.TableName).In(meta.CustOrderStatus_CustOrderId.Name, orderId).Find(&statusData)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	statusMap := make(map[string]model.CustOrderStatus)
	for _, v := range statusData {
		statusMap[v.CustOrderId] = v
	}
	for k, v := range data {
		info, ok := statusMap[v.CustOrderId]
		if ok {
			data[k].OrderStatus = info
		}
	}
	return grmi.PagingResult{Records: data, Count: count, PageNumber: paging.Number, PageSize: paging.Size}, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.PageSelect
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) PageSelect(custOrderId string, workLineId string, status int, projectId string, productFamilyId string, start string, end string, pageSize int64, pageNumber int64) (grmi.PagingResult, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	session = session.Where(fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName), impl.plantNr)
	if custOrderId != "" {
		session = session.Where(fmt.Sprintf("%s = ?", meta.CustOrder_CustOrderId.ColumnName), custOrderId)
		where += fmt.Sprintf(" and %s = ? ", meta.CustOrder_CustOrderId.ColumnName)
		parameters = append(parameters, custOrderId)
	}
	if workLineId != "" {
		session = session.Where(fmt.Sprintf("%s = ?", meta.CustOrder_WorkLineId.ColumnName), workLineId)
		where += fmt.Sprintf(" and %s = ? ", meta.CustOrder_WorkLineId.ColumnName)
		parameters = append(parameters, workLineId)
	}
	if status == 0 {
		session.Where(fmt.Sprintf("%s < ?", meta.CustOrderStatus_Status.ColumnName), common.WO_STATUS_FINISHED)
		where += fmt.Sprintf(" and %s < ? ", meta.CustOrderStatus_Status.ColumnName)
		parameters = append(parameters, common.WO_STATUS_FINISHED)
	} else {
		session.Where(fmt.Sprintf("%s = ?", meta.CustOrderStatus_Status.ColumnName), status)
		where += fmt.Sprintf(" and %s = ? ", meta.CustOrderStatus_Status.ColumnName)
		parameters = append(parameters, status)
	}
	if projectId != "" {
		session.Where(fmt.Sprintf("%s = ?", meta.CustOrder_ProjectId.ColumnName), projectId)
		where += fmt.Sprintf(" and %s = ? ", meta.CustOrder_ProjectId.ColumnName)
		parameters = append(parameters, projectId)
	}
	if productFamilyId != "" {
		session.Where(fmt.Sprintf("%s = ?", meta.CustOrder_ProductFamilyId.ColumnName), productFamilyId)
		where += fmt.Sprintf(" and %s = ? ", meta.CustOrder_ProductFamilyId.ColumnName)
		parameters = append(parameters, productFamilyId)
	}
	if start != "" {
		session.Where(fmt.Sprintf("%s >= ?", meta.CustOrder_PlanStartTime.ColumnName), start)
		where += fmt.Sprintf(" and %s >= ? ", meta.CustOrder_PlanStartTime.ColumnName)
		parameters = append(parameters, start)
		if end != "" {
			session.Where(fmt.Sprintf("%s <= ?", meta.CustOrder_PlanEndTime.ColumnName), end)
			where += fmt.Sprintf(" and %s <= ? ", meta.CustOrder_PlanStartTime.ColumnName)
			parameters = append(parameters, end)
		}
	}
	count, err := session.Count(impl.meta.Indicator)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}

	orderBy := " order by " + meta.CustOrder_CreateTime.SortColumnName + " desc "
	parameters = append(parameters, pageSize*(pageNumber-1), pageSize)
	data := make([]model.CustOrderData, 0, 10)
	where = " where " + where
	joinStr := fmt.Sprintf(" inner join %s on %s inner join %s on %s ",
		meta.CustOrderStatus.TableName,
		statusCondition,
		meta.CustOrderQty.TableName,
		qtyCondition)
	err = impl.session.Table(impl.meta.TableName).SQL("select * from ["+impl.meta.TableName+"]"+joinStr+where+orderBy+" offset ? row fetch next ? row only", parameters...).Find(&data)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, item := range data {
		custOrder := item.CustOrder
		custOrder.OrderStatus = item.CustOrderStatus
		custOrder.OrderQty = item.CustOrderQty
		result = append(result, custOrder)
	}
	return grmi.PagingResult{Records: result, Count: count, PageNumber: pageNumber, PageSize: pageSize}, nil
}


/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/dao/pln/CustOrderDAO.JoinSelect
 *
 ******************************************************************************/
func (impl *CustOrderDAOImplement) JoinSelect(predicates []grmi.Predicate, orderByFields []grmi.Field) ([]model.CustOrder, error) {

	parameters := []interface{}{impl.plantNr}
	where := fmt.Sprintf("%s = ?", meta.CustOrder_PlantNr.ColumnName)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)

	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	if predicates != nil {
		for _, predicate := range predicates {
			where += predicate.Build()
			parameters = append(parameters, predicate.Values...)
		}
		session = session.Where(where, parameters...)
	} else {
		session = session.Where(where, parameters...)
	}

	session = session.OrderBy(meta.CustOrder_PlantNr.ColumnName)
	if orderByFields != nil {
		for _, field := range orderByFields {
			session = session.OrderBy(field.ColumnName)
		}
	}

	data := make([]model.CustOrderData, 0, 10)
	err := session.Find(&data)
	if err != nil {
		return nil, grmi.NewDataBaseError(err)
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, custOrderData := range data {
		custOrder := custOrderData.CustOrder
		custOrder.OrderStatus = custOrderData.CustOrderStatus
		custOrder.OrderQty = custOrderData.CustOrderQty
		result = append(result, custOrder)
	}
	return result, nil
}

func (impl *CustOrderDAOImplement) SelectCustOrderByPlan(start, end string) ([]model.CustOrderData, error) {
	parameters := []interface{}{impl.plantNr, start, end, start, start}
	where := fmt.Sprintf("%s = ? and (%s >= ? and %s <= ?) or (%s < ? and %s > ?) ",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	data := make([]model.CustOrderData, 0)
	err := session.Where(where, parameters...).Find(&data)
	return data, err
}


func (impl *CustOrderDAOImplement) SelectProduceOrder(start, end, workLineId string) ([]model.CustOrderData, error) {
	parameters := []interface{}{impl.plantNr, workLineId, start, end, "1900-01-01 00:00:00", end, common.WO_STATUS_RELEASED, common.WO_STATUS_CANCELED}
	where := fmt.Sprintf("%s = ? and %s = ? and  ( (%s >= ? and %s < ?) or (%s = ? and %s < ?) ) and %s > ? and %s != ?",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_WorkLineId.ColumnName,
		meta.CustOrder_ActEndTime.ColumnName,
		meta.CustOrder_ActEndTime.ColumnName,
		meta.CustOrder_ActEndTime.ColumnName,
		meta.CustOrder_ActStartTime.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
	)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	data := make([]model.CustOrderData, 0)
	err := session.Where(where, parameters...).Find(&data)
	return data, err
}

// SelectCrossDayCustOrder 查询是否有跨天的客户订单
func (impl *CustOrderDAOImplement) SelectCrossDayCustOrder(end string) ([]model.CustOrder, error) {
	parameters := []interface{}{impl.plantNr, end, end}
	where := fmt.Sprintf("%s = ? and %s < ? and %s >= ? ",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_PlanStartTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName,
	)
	data := make([]model.CustOrder, 0)
	err := impl.session.Table(impl.meta.TableName).Where(where, parameters...).Find(&data)
	return data, err
}


// SelectCurrentDayCustOrder 查询当天的客户订单
func (impl *CustOrderDAOImplement) SelectCurrentDayCustOrder(start, end string) ([]model.CustOrder, error) {
	parameters := []interface{}{impl.plantNr, end, start}
	where := fmt.Sprintf("%s = ? and %s < ? and %s > ? ",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName,
		meta.CustOrder_PlanEndTime.ColumnName,
	)
	data := make([]model.CustOrder, 0)
	err := impl.session.Table(impl.meta.TableName).Where(where, parameters...).Desc(meta.CustOrder_PlanEndTime.Name).Find(&data)
	return data, err
}

// SelectRunningCustOrder 通过虚拟产线查询正在生产的客户订单
func (impl *CustOrderDAOImplement) SelectRunningCustOrder(workLineId string) ([]model.CustOrder, error) {
	parameters := []interface{}{impl.plantNr, workLineId, common.WO_STATUS_RUNNING}
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	where := fmt.Sprintf("%s = ? and %s = ? and %s = ?",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrder_WorkLineId.ColumnName,
		meta.CustOrderStatus_Status.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("LEFT", meta.CustOrderStatus.TableName, statusCondition)
	data := make([]model.CustOrder, 0)
	err := session.Where(where, parameters...).Desc(meta.CustOrderStatus_LastModify.SortColumnName).Find(&data)
	return data, err
}


// SelectNeedReleaseCustOrder 查询可生产的客户订单
func (impl *CustOrderDAOImplement) SelectNeedReleaseCustOrder() ([]model.CustOrder, error) {
	parameters := []interface{}{impl.plantNr, common.ORDER_STATUS_LOCKED}
	where := fmt.Sprintf("%s = ? and %s = ?",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_Status.ColumnName)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	data := make([]model.CustOrderData, 0)
	err := session.Where(where, parameters...).Asc(meta.CustOrder_PlanStartTime.SortColumnName).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, custOrderData := range data {
		var custOrder model.CustOrder
		custOrder = custOrderData.CustOrder
		custOrder.OrderStatus = custOrderData.CustOrderStatus
		custOrder.OrderQty = custOrderData.CustOrderQty
		result = append(result, custOrder)
	}
	return result, err
}

func (impl *CustOrderDAOImplement) SelectReleasingOrder() ([]model.CustOrder, error) {
	parameters := []interface{}{impl.plantNr, common.ORDER_STATUS_LOCKED, common.WO_STATUS_RELEASE_FAILED}
	where := fmt.Sprintf("%s = ? and ( %s = ? or %s = ?)",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		meta.CustOrderStatus_Status.ColumnName,
		)
	statusCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderStatus_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderStatus_CustOrderId.ColumnName)
	qtyCondition := fmt.Sprintf("%s = %s and %s = %s",
		meta.CustOrder_PlantNr.ColumnName,
		meta.CustOrderQty_PlantNr.ColumnName,
		meta.CustOrder_CustOrderId.ColumnName,
		meta.CustOrderQty_CustOrderId.ColumnName)
	session := impl.session.Table(impl.meta.TableName).Join("INNER", meta.CustOrderStatus.TableName, statusCondition).Join("INNER", meta.CustOrderQty.TableName, qtyCondition)
	data := make([]model.CustOrderData, 0)
	err := session.Where(where, parameters...).Asc(meta.CustOrder_WorkLineId.SortColumnName).Asc(meta.CustOrder_PlanStartTime.SortColumnName).Find(&data)
	if err != nil {
		return nil, err
	}
	result := make([]model.CustOrder, 0, len(data))
	for _, custOrderData := range data {
		var custOrder model.CustOrder
		custOrder = custOrderData.CustOrder
		custOrder.OrderStatus = custOrderData.CustOrderStatus
		custOrder.OrderQty = custOrderData.CustOrderQty
		result = append(result, custOrder)
	}
	return result, err
}