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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/om"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/om"
	model "LAPP_LF_MOM_BACKEND/models/om"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : ProdOrder的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrder = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProdOrder_ProdOrderId.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrder_ProdOrderId, grmi.Equal, true),
		meta.ProdOrder_ArtId.UrlParameterName:                grmi.NewConditionItem(meta.ProdOrder_ArtId, grmi.Equal, false),
		meta.ProdOrder_CustArtId.UrlParameterName:            grmi.NewConditionItem(meta.ProdOrder_CustArtId, grmi.Equal, false),
		meta.ProdOrder_RouteTemplateId.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrder_RouteTemplateId, grmi.Equal, false),
		meta.ProdOrder_OrderType.UrlParameterName:            grmi.NewConditionItem(meta.ProdOrder_OrderType, grmi.Equal, false),
		meta.ProdOrder_OrderInfo.UrlParameterName:            grmi.NewConditionItem(meta.ProdOrder_OrderInfo, grmi.Equal, false),
		meta.ProdOrder_CustOrderId.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrder_CustOrderId, grmi.Equal, false),
		meta.ProdOrder_ErpOrderId.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_ErpOrderId, grmi.Equal, false),
		meta.ProdOrder_ParentOrderId.UrlParameterName:        grmi.NewConditionItem(meta.ProdOrder_ParentOrderId, grmi.Equal, false),
		meta.ProdOrder_Priority.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrder_Priority, grmi.Equal, false),
		meta.ProdOrder_Status.UrlParameterName:               grmi.NewConditionItem(meta.ProdOrder_Status, grmi.Equal, false),
		meta.ProdOrder_PlanResourceGroupId.UrlParameterName:  grmi.NewConditionItem(meta.ProdOrder_PlanResourceGroupId, grmi.Equal, false),
		meta.ProdOrder_PlanResourceId.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrder_PlanResourceId, grmi.Equal, false),
		meta.ProdOrder_UsedResourceId.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrder_UsedResourceId, grmi.Equal, false),
		meta.ProdOrder_PlanQty.UrlParameterName:              grmi.NewConditionItem(meta.ProdOrder_PlanQty, grmi.Equal, false),
		meta.ProdOrder_QtyUomId.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrder_QtyUomId, grmi.Equal, false),
		meta.ProdOrder_RatePerHourToggle.UrlParameterName:    grmi.NewConditionItem(meta.ProdOrder_RatePerHourToggle, grmi.Equal, false),
		meta.ProdOrder_TimePerItemToggle.UrlParameterName:    grmi.NewConditionItem(meta.ProdOrder_TimePerItemToggle, grmi.Equal, false),
		meta.ProdOrder_TimePerBatchToggle.UrlParameterName:   grmi.NewConditionItem(meta.ProdOrder_TimePerBatchToggle, grmi.Equal, false),
		meta.ProdOrder_BatchTimeFieldToggle.UrlParameterName: grmi.NewConditionItem(meta.ProdOrder_BatchTimeFieldToggle, grmi.Equal, false),
		meta.ProdOrder_OpTimePerItem.UrlParameterName:        grmi.NewConditionItem(meta.ProdOrder_OpTimePerItem, grmi.Equal, false),
		meta.ProdOrder_BatchTime.UrlParameterName:            grmi.NewConditionItem(meta.ProdOrder_BatchTime, grmi.Equal, false),
		meta.ProdOrder_QuantityPerHour.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrder_QuantityPerHour, grmi.Equal, false),
		meta.ProdOrder_MidBatchQuantity.UrlParameterName:     grmi.NewConditionItem(meta.ProdOrder_MidBatchQuantity, grmi.Equal, false),
		meta.ProdOrder_MidBatchTime.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrder_MidBatchTime, grmi.Approximate, false),
		meta.ProdOrder_BatchingMethod.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrder_BatchingMethod, grmi.Equal, false),
		meta.ProdOrder_PlanStartDate.UrlParameterName:        grmi.NewConditionItem(meta.ProdOrder_PlanStartDate, grmi.Approximate, false),
		meta.ProdOrder_PlanEndDate.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrder_PlanEndDate, grmi.Approximate, false),
		meta.ProdOrder_PlanStartTime.UrlParameterName:        grmi.NewConditionItem(meta.ProdOrder_PlanStartTime, grmi.Approximate, false),
		meta.ProdOrder_PlanEndTime.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrder_PlanEndTime, grmi.Approximate, false),
		meta.ProdOrder_ActStartTime.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrder_ActStartTime, grmi.Approximate, false),
		meta.ProdOrder_ActEndTime.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_ActEndTime, grmi.Approximate, false),
		meta.ProdOrder_ProjectId.UrlParameterName:            grmi.NewConditionItem(meta.ProdOrder_ProjectId, grmi.Equal, false),
		meta.ProdOrder_CustomerId.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_CustomerId, grmi.Equal, false),
		meta.ProdOrder_CustomerName.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrder_CustomerName, grmi.Equal, false),
		meta.ProdOrder_BackflushStatus.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrder_BackflushStatus, grmi.Equal, false),
		meta.ProdOrder_BackflushTime.UrlParameterName:        grmi.NewConditionItem(meta.ProdOrder_BackflushTime, grmi.Approximate, false),
		meta.ProdOrder_PackStatus.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_PackStatus, grmi.Equal, false),
		meta.ProdOrder_PackTime.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrder_PackTime, grmi.Approximate, false),
		meta.ProdOrder_ShipStatus.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_ShipStatus, grmi.Equal, false),
		meta.ProdOrder_ShipTime.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrder_ShipTime, grmi.Approximate, false),
		meta.ProdOrder_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_LastModify, grmi.Approximate, false),
		meta.ProdOrder_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrder_LastUser, grmi.Equal, false),
		meta.ProdOrder_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrder_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProdOrder的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderAndPaging = grmi.NewCondition(
	DefaultConditionOfProdOrder.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : ProdOrderServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : ProdOrder的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type ProdOrderServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProdOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProdOrderDAOImplement实例
 *
 * @Return Value             : ProdOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewProdOrderServiceImplement() *ProdOrderServiceImplement {
	return &ProdOrderServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.InsertOne
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) InsertOne(user *models.Usertab, entity *model.ProdOrder) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "InsertOneProdOrder", "插入一个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) DeleteOne(user *models.Usertab, prodOrderId string) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "DeleteOneProdOrder", "删除一个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(prodOrderId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(prodOrderId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.SelectOne
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) SelectOne(user *models.Usertab, prodOrderId string) (*model.ProdOrder, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "SelectOneProdOrder", "查询一个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(prodOrderId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) UpdateOne(user *models.Usertab, entity *model.ProdOrder) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "UpdateOneProdOrder", "修改一个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.Insert
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) Insert(user *models.Usertab, entities *[]model.ProdOrder) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "InsertProdOrder", "插入多个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProdOrderId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

	err := dao.Insert(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.Delete
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) Delete(user *models.Usertab, entities *[]model.ProdOrder) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "DeleteProdOrder", "删除多个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProdOrderId)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Delete(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.Select
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "SelectProdOrder", "查询ProdOrder")

	condition := DefaultConditionOfProdOrderAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderService.Update
 *
 ******************************************************************************/
func (impl *ProdOrderServiceImplement) Update(user *models.Usertab, entities *[]model.ProdOrder) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrder.service.impl.go", "UpdateProdOrder", "修改多个ProdOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProdOrderId)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Update(entities)
	if err != nil {
		return err
	}
	return nil
}
