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

package implments

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

/******************************************************************************
 *
 * @Description              : PrinterTemplateLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
var DefaultConditionOfPrinterTemplateLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PrinterTemplateLst_TemplateId.UrlParameterName: grmi.NewConditionItem(meta.PrinterTemplateLst_TemplateId, grmi.Equal, true),
		meta.PrinterTemplateLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.PrinterTemplateLst_Pos, grmi.Equal, true),
		meta.PrinterTemplateLst_VarName.UrlParameterName:    grmi.NewConditionItem(meta.PrinterTemplateLst_VarName, grmi.Equal, false),
		meta.PrinterTemplateLst_VarValue.UrlParameterName:   grmi.NewConditionItem(meta.PrinterTemplateLst_VarValue, grmi.Equal, false),
		meta.PrinterTemplateLst_VarType.UrlParameterName:    grmi.NewConditionItem(meta.PrinterTemplateLst_VarType, grmi.Equal, false),
		meta.PrinterTemplateLst_VarPos.UrlParameterName:     grmi.NewConditionItem(meta.PrinterTemplateLst_VarPos, grmi.Equal, false),
		meta.PrinterTemplateLst_Picture.UrlParameterName:    grmi.NewConditionItem(meta.PrinterTemplateLst_Picture, grmi.Equal, false),
		meta.PrinterTemplateLst_IsIdx.UrlParameterName:      grmi.NewConditionItem(meta.PrinterTemplateLst_IsIdx, grmi.Equal, false),
		meta.PrinterTemplateLst_KeyVal.UrlParameterName:     grmi.NewConditionItem(meta.PrinterTemplateLst_KeyVal, grmi.Equal, false),
		meta.PrinterTemplateLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.PrinterTemplateLst_LastModify, grmi.Approximate, false),
		meta.PrinterTemplateLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.PrinterTemplateLst_LastUser, grmi.Equal, false),
		meta.PrinterTemplateLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.PrinterTemplateLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PrinterTemplateLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
var DefaultConditionOfPrinterTemplateLstAndPaging = grmi.NewCondition(
	DefaultConditionOfPrinterTemplateLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PrinterTemplateLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PrinterTemplateLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
type PrinterTemplateLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPrinterTemplateLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PrinterTemplateLstDAOImplement实例
 *
 * @Return Value             : PrinterTemplateLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-20 16:59:51
 *
 ******************************************************************************/
func NewPrinterTemplateLstServiceImplement() *PrinterTemplateLstServiceImplement {
	return &PrinterTemplateLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateLstService.InsertOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateLstServiceImplement) InsertOne(user *models.Usertab, entity *model.PrinterTemplateLst) error {

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "InsertOnePrinterTemplateLst", "插入一个PrinterTemplateLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPrinterTemplateLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.TemplateId, 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/base/PrinterTemplateLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateLstServiceImplement) DeleteOne(user *models.Usertab, templateId string, pos int) error {

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "DeleteOnePrinterTemplateLst", "删除一个PrinterTemplateLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateLstService.SelectOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateLstServiceImplement) SelectOne(user *models.Usertab, templateId string, pos int) (*model.PrinterTemplateLst, error) {

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "SelectOnePrinterTemplateLst", "查询一个PrinterTemplateLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrinterTemplateLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrinterTemplateLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.PrinterTemplateLst) error {

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "UpdateOnePrinterTemplateLst", "修改一个PrinterTemplateLst")

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

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "InsertPrinterTemplateLst", "插入多个PrinterTemplateLst")

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

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

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "DeletePrinterTemplateLst", "删除多个PrinterTemplateLst")

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

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

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "SelectPrinterTemplateLst", "查询PrinterTemplateLst")

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

	grmi.Log(user, "/services/base/implments/PrinterTemplateLst.service.impl.go", "UpdatePrinterTemplateLst", "修改多个PrinterTemplateLst")

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

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

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