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

package implments

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

/******************************************************************************
 *
 * @Description              : PackOrderItemlst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPackOrderItemlst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackOrderItemlst_PackOrderId.UrlParameterName:   grmi.NewConditionItem(meta.PackOrderItemlst_PackOrderId, grmi.Equal, true),
		meta.PackOrderItemlst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.PackOrderItemlst_Pos, grmi.Equal, true),
		meta.PackOrderItemlst_PosCode.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderItemlst_PosCode, grmi.Equal, false),
		meta.PackOrderItemlst_MaterialId.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_MaterialId, grmi.Equal, false),
		meta.PackOrderItemlst_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.PackOrderItemlst_SerialOrderId, grmi.Equal, false),
		meta.PackOrderItemlst_PlanQty.UrlParameterName:       grmi.NewConditionItem(meta.PackOrderItemlst_PlanQty, grmi.Equal, false),
		meta.PackOrderItemlst_ActQty.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderItemlst_ActQty, grmi.Equal, false),
		meta.PackOrderItemlst_Status.UrlParameterName:        grmi.NewConditionItem(meta.PackOrderItemlst_Status, grmi.Equal, false),
		meta.PackOrderItemlst_Seq.UrlParameterName:           grmi.NewConditionItem(meta.PackOrderItemlst_Seq, grmi.Equal, false),
		meta.PackOrderItemlst_PKOICustC1.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_PKOICustC1, grmi.Equal, false),
		meta.PackOrderItemlst_PKOICustC2.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_PKOICustC2, grmi.Equal, false),
		meta.PackOrderItemlst_PKOICustI1.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_PKOICustI1, grmi.Equal, false),
		meta.PackOrderItemlst_PKOICustI2.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_PKOICustI2, grmi.Equal, false),
		meta.PackOrderItemlst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_LastModify, grmi.Approximate, false),
		meta.PackOrderItemlst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.PackOrderItemlst_LastUser, grmi.Equal, false),
		meta.PackOrderItemlst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.PackOrderItemlst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackOrderItemlstService.InsertOne
 *
 ******************************************************************************/
func (impl *PackOrderItemlstServiceImplement) InsertOne(user *models.Usertab, entity *model.PackOrderItemlst) error {

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "InsertOnePackOrderItemlst", "插入一个PackOrderItemlst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderItemlstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PackOrderId, 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/log/PackOrderItemlstService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackOrderItemlstServiceImplement) DeleteOne(user *models.Usertab, packOrderId string, pos int) error {

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "DeleteOnePackOrderItemlst", "删除一个PackOrderItemlst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackOrderItemlstService.SelectOne
 *
 ******************************************************************************/
func (impl *PackOrderItemlstServiceImplement) SelectOne(user *models.Usertab, packOrderId string, pos int) (*model.PackOrderItemlst, error) {

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "SelectOnePackOrderItemlst", "查询一个PackOrderItemlst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackOrderItemlstService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackOrderItemlstServiceImplement) UpdateOne(user *models.Usertab, entity *model.PackOrderItemlst) error {

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "UpdateOnePackOrderItemlst", "修改一个PackOrderItemlst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackOrderItemlstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PackOrderId, 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/log/PackOrderItemlstService.Insert
 *
 ******************************************************************************/
func (impl *PackOrderItemlstServiceImplement) Insert(user *models.Usertab, entities *[]model.PackOrderItemlst) error {

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "InsertPackOrderItemlst", "插入多个PackOrderItemlst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PackOrderId, 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/log/PackOrderItemlstService.Delete
 *
 ******************************************************************************/
func (impl *PackOrderItemlstServiceImplement) Delete(user *models.Usertab, entities *[]model.PackOrderItemlst) error {

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "DeletePackOrderItemlst", "删除多个PackOrderItemlst")

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

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

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "SelectPackOrderItemlst", "查询PackOrderItemlst")

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

	grmi.Log(user, "/services/log/implments/LOG_PackOrderItemlst.service.impl.go", "UpdatePackOrderItemlst", "修改多个PackOrderItemlst")

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

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

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