// 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              : PackTemplatePrintlst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfPackTemplatePrintlst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PackTemplatePrintlst_PackTemplateId.UrlParameterName: grmi.NewConditionItem(meta.PackTemplatePrintlst_PackTemplateId, grmi.Equal, true),
		meta.PackTemplatePrintlst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.PackTemplatePrintlst_Pos, grmi.Equal, true),
		meta.PackTemplatePrintlst_VarName.UrlParameterName:        grmi.NewConditionItem(meta.PackTemplatePrintlst_VarName, grmi.Equal, false),
		meta.PackTemplatePrintlst_VarValue.UrlParameterName:       grmi.NewConditionItem(meta.PackTemplatePrintlst_VarValue, grmi.Equal, false),
		meta.PackTemplatePrintlst_VarType.UrlParameterName:        grmi.NewConditionItem(meta.PackTemplatePrintlst_VarType, grmi.Equal, false),
		meta.PackTemplatePrintlst_VarPos.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplatePrintlst_VarPos, grmi.Equal, false),
		meta.PackTemplatePrintlst_Picture.UrlParameterName:        grmi.NewConditionItem(meta.PackTemplatePrintlst_Picture, grmi.Equal, false),
		meta.PackTemplatePrintlst_IsIdxToggle.UrlParameterName:    grmi.NewConditionItem(meta.PackTemplatePrintlst_IsIdxToggle, grmi.Equal, false),
		meta.PackTemplatePrintlst_KeyVal.UrlParameterName:         grmi.NewConditionItem(meta.PackTemplatePrintlst_KeyVal, grmi.Equal, false),
		meta.PackTemplatePrintlst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.PackTemplatePrintlst_LastModify, grmi.Approximate, false),
		meta.PackTemplatePrintlst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.PackTemplatePrintlst_LastUser, grmi.Equal, false),
		meta.PackTemplatePrintlst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.PackTemplatePrintlst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplatePrintlstService.InsertOne
 *
 ******************************************************************************/
func (impl *PackTemplatePrintlstServiceImplement) InsertOne(user *models.Usertab, entity *model.PackTemplatePrintlst) error {

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "InsertOnePackTemplatePrintlst", "插入一个PackTemplatePrintlst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPackTemplatePrintlstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PackTemplateId, 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/PackTemplatePrintlstService.DeleteOne
 *
 ******************************************************************************/
func (impl *PackTemplatePrintlstServiceImplement) DeleteOne(user *models.Usertab, packTemplateId string, pos int) error {

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "DeleteOnePackTemplatePrintlst", "删除一个PackTemplatePrintlst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplatePrintlstService.SelectOne
 *
 ******************************************************************************/
func (impl *PackTemplatePrintlstServiceImplement) SelectOne(user *models.Usertab, packTemplateId string, pos int) (*model.PackTemplatePrintlst, error) {

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "SelectOnePackTemplatePrintlst", "查询一个PackTemplatePrintlst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/PackTemplatePrintlstService.UpdateOne
 *
 ******************************************************************************/
func (impl *PackTemplatePrintlstServiceImplement) UpdateOne(user *models.Usertab, entity *model.PackTemplatePrintlst) error {

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "UpdateOnePackTemplatePrintlst", "修改一个PackTemplatePrintlst")

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

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "InsertPackTemplatePrintlst", "插入多个PackTemplatePrintlst")

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

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

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "DeletePackTemplatePrintlst", "删除多个PackTemplatePrintlst")

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

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

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "SelectPackTemplatePrintlst", "查询PackTemplatePrintlst")

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

	grmi.Log(user, "/services/log/implments/LOG_PackTemplatePrintlst.service.impl.go", "UpdatePackTemplatePrintlst", "修改多个PackTemplatePrintlst")

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

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

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