// 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              : PackType的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPackType = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackType_PackTypeId.UrlParameterName:       grmi.NewConditionItem(meta.PackType_PackTypeId, grmi.Equal, true),
		meta.PackType_Descr.UrlParameterName:            grmi.NewConditionItem(meta.PackType_Descr, grmi.Equal, false),
		meta.PackType_SortWay.UrlParameterName:          grmi.NewConditionItem(meta.PackType_SortWay, grmi.Equal, false),
		meta.PackType_VerifySlotToggle.UrlParameterName: grmi.NewConditionItem(meta.PackType_VerifySlotToggle, grmi.Equal, false),
		meta.PackType_VerifyRule.UrlParameterName:       grmi.NewConditionItem(meta.PackType_VerifyRule, grmi.Equal, false),
		meta.PackType_Length.UrlParameterName:           grmi.NewConditionItem(meta.PackType_Length, grmi.Equal, false),
		meta.PackType_Width.UrlParameterName:            grmi.NewConditionItem(meta.PackType_Width, grmi.Equal, false),
		meta.PackType_Height.UrlParameterName:           grmi.NewConditionItem(meta.PackType_Height, grmi.Equal, false),
		meta.PackType_SizeUom.UrlParameterName:          grmi.NewConditionItem(meta.PackType_SizeUom, grmi.Equal, false),
		meta.PackType_Weight.UrlParameterName:           grmi.NewConditionItem(meta.PackType_Weight, grmi.Equal, false),
		meta.PackType_WeightUom.UrlParameterName:        grmi.NewConditionItem(meta.PackType_WeightUom, grmi.Equal, false),
		meta.PackType_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.PackType_LastModify, grmi.Approximate, false),
		meta.PackType_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.PackType_LastUser, grmi.Equal, false),
		meta.PackType_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.PackType_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTypeService.InsertOne
 *
 ******************************************************************************/
func (impl *PackTypeServiceImplement) InsertOne(user *models.Usertab, entity *model.PackType) error {

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "InsertOnePackType", "插入一个PackType")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTypeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PackTypeId)
	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/PackTypeService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackTypeServiceImplement) DeleteOne(user *models.Usertab, packTypeId string) error {

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "DeleteOnePackType", "删除一个PackType")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTypeService.SelectOne
 *
 ******************************************************************************/
func (impl *PackTypeServiceImplement) SelectOne(user *models.Usertab, packTypeId string) (*model.PackType, error) {

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "SelectOnePackType", "查询一个PackType")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTypeService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackTypeServiceImplement) UpdateOne(user *models.Usertab, entity *model.PackType) error {

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "UpdateOnePackType", "修改一个PackType")

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

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "InsertPackType", "插入多个PackType")

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

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

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "DeletePackType", "删除多个PackType")

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

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

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "SelectPackType", "查询PackType")

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

	grmi.Log(user, "/services/log/implments/LOG_PackType.service.impl.go", "UpdatePackType", "修改多个PackType")

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

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

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