// 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              : ProdOrderQtyRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderQtyRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProdOrderQtyRecLst_ProdOrderId.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrderQtyRecLst_ProdOrderId, grmi.Equal, true),
		meta.ProdOrderQtyRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.ProdOrderQtyRecLst_Pos, grmi.Equal, true),
		meta.ProdOrderQtyRecLst_ActQty.UrlParameterName:               grmi.NewConditionItem(meta.ProdOrderQtyRecLst_ActQty, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_OrderQtyType.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrderQtyRecLst_OrderQtyType, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_RecordTime.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderQtyRecLst_RecordTime, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrderQtyRecLst_TriggerEvent, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.ProdOrderQtyRecLst_TriggerObjectId, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.ProdOrderQtyRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.ProdOrderQtyRecLst_OutputEvent, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.ProdOrderQtyRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrderQtyRecLst_OutputObjectId, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.ProdOrderQtyRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.ProdOrderQtyRecLst_Remark1, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.ProdOrderQtyRecLst_Remark2, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderQtyRecLst_LastModify, grmi.Approximate, false),
		meta.ProdOrderQtyRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrderQtyRecLst_LastUser, grmi.Equal, false),
		meta.ProdOrderQtyRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderQtyRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderQtyRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) InsertOne(user *models.Usertab, entity *model.ProdOrderQtyRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "InsertOneProdOrderQtyRecLst", "插入一个ProdOrderQtyRecLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderQtyRecLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId, entity.Pos)
	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/ProdOrderQtyRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) DeleteOne(user *models.Usertab, prodOrderId string, pos int) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "DeleteOneProdOrderQtyRecLst", "删除一个ProdOrderQtyRecLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderQtyRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) SelectOne(user *models.Usertab, prodOrderId string, pos int) (*model.ProdOrderQtyRecLst, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "SelectOneProdOrderQtyRecLst", "查询一个ProdOrderQtyRecLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderQtyRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ProdOrderQtyRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "UpdateOneProdOrderQtyRecLst", "修改一个ProdOrderQtyRecLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderQtyRecLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId, entity.Pos)
	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/ProdOrderQtyRecLstService.Insert
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) Insert(user *models.Usertab, entities *[]model.ProdOrderQtyRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "InsertProdOrderQtyRecLst", "插入多个ProdOrderQtyRecLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProdOrderId, entity.Pos)
		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/ProdOrderQtyRecLstService.Delete
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) Delete(user *models.Usertab, entities *[]model.ProdOrderQtyRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "DeleteProdOrderQtyRecLst", "删除多个ProdOrderQtyRecLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProdOrderId, entity.Pos)
		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/ProdOrderQtyRecLstService.Select
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "SelectProdOrderQtyRecLst", "查询ProdOrderQtyRecLst")

	condition := DefaultConditionOfProdOrderQtyRecLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewProdOrderQtyRecLstDAO(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/ProdOrderQtyRecLstService.Update
 *
 ******************************************************************************/
func (impl *ProdOrderQtyRecLstServiceImplement) Update(user *models.Usertab, entities *[]model.ProdOrderQtyRecLst) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderQtyRecLst.service.impl.go", "UpdateProdOrderQtyRecLst", "修改多个ProdOrderQtyRecLst")

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

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

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