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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : ReceiptHead的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfReceiptHead = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ReceiptHead_ReceiptHeadId.UrlParameterName: grmi.NewConditionItem(meta.ReceiptHead_ReceiptHeadId, grmi.Equal, true),
		meta.ReceiptHead_PrinterId.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptHead_PrinterId, grmi.Equal, false),
		meta.ReceiptHead_PrintFileType.UrlParameterName: grmi.NewConditionItem(meta.ReceiptHead_PrintFileType, grmi.Equal, false),
		meta.ReceiptHead_PrintObjId.UrlParameterName:    grmi.NewConditionItem(meta.ReceiptHead_PrintObjId, grmi.Equal, false),
		meta.ReceiptHead_PrintObjType.UrlParameterName:  grmi.NewConditionItem(meta.ReceiptHead_PrintObjType, grmi.Equal, false),
		meta.ReceiptHead_Status.UrlParameterName:        grmi.NewConditionItem(meta.ReceiptHead_Status, grmi.Equal, false),
		meta.ReceiptHead_TemplateFile.UrlParameterName:  grmi.NewConditionItem(meta.ReceiptHead_TemplateFile, grmi.Equal, false),
		meta.ReceiptHead_PrintCopies.UrlParameterName:   grmi.NewConditionItem(meta.ReceiptHead_PrintCopies, grmi.Equal, false),
		meta.ReceiptHead_PrintedTime.UrlParameterName:   grmi.NewConditionItem(meta.ReceiptHead_PrintedTime, grmi.Approximate, false),
		meta.ReceiptHead_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptHead_CtrlPara1, grmi.Equal, false),
		meta.ReceiptHead_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptHead_CtrlPara2, grmi.Equal, false),
		meta.ReceiptHead_CtrlStr1.UrlParameterName:      grmi.NewConditionItem(meta.ReceiptHead_CtrlStr1, grmi.Equal, false),
		meta.ReceiptHead_CtrlStr2.UrlParameterName:      grmi.NewConditionItem(meta.ReceiptHead_CtrlStr2, grmi.Equal, false),
		meta.ReceiptHead_CtrlTime1.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptHead_CtrlTime1, grmi.Approximate, false),
		meta.ReceiptHead_CtrlTime2.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptHead_CtrlTime2, grmi.Approximate, false),
		meta.ReceiptHead_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ReceiptHead_LastModify, grmi.Approximate, false),
		meta.ReceiptHead_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ReceiptHead_LastUser, grmi.Equal, false),
		meta.ReceiptHead_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ReceiptHead_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ReceiptHead的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfReceiptHeadAndPaging = grmi.NewCondition(
	DefaultConditionOfReceiptHead.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ReceiptHeadServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ReceiptHead的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
type ReceiptHeadServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewReceiptHeadDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ReceiptHeadDAOImplement实例
 *
 * @Return Value             : ReceiptHeadDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
func NewReceiptHeadServiceImplement() *ReceiptHeadServiceImplement {
	return &ReceiptHeadServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.InsertOne
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) InsertOne(user *global.User, entity *model.ReceiptHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ReceiptHeadId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.DeleteOne
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) DeleteOne(user *global.User, receiptHeadId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.SelectOne
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) SelectOne(user *global.User, receiptHeadId string) (*model.ReceiptHead, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(receiptHeadId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.UpdateOne
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) UpdateOne(user *global.User, entity *model.ReceiptHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ReceiptHeadId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.Insert
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) Insert(user *global.User, entities *[]model.ReceiptHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ReceiptHeadId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.Delete
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) Delete(user *global.User, entities *[]model.ReceiptHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.Select
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ReceiptHead, error) {

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

	condition := DefaultConditionOfReceiptHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfReceiptHeadAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptHeadService.Update
 *
 ******************************************************************************/
func (impl *ReceiptHeadServiceImplement) Update(user *global.User, entities *[]model.ReceiptHead) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptHeadDAO(session, user.PlantNr, user.UserId)

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

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