// 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              : ReceiptDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 16:19:35
 *
 ******************************************************************************/
var DefaultConditionOfReceiptDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ReceiptDetail_ReceiptHeadId.UrlParameterName:    grmi.NewConditionItem(meta.ReceiptDetail_ReceiptHeadId, grmi.Equal, true),
		meta.ReceiptDetail_Pos.UrlParameterName:              grmi.NewConditionItem(meta.ReceiptDetail_Pos, grmi.Equal, true),
		meta.ReceiptDetail_VariableName.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptDetail_VariableName, grmi.Equal, false),
		meta.ReceiptDetail_VariableType.UrlParameterName:     grmi.NewConditionItem(meta.ReceiptDetail_VariableType, grmi.Equal, false),
		meta.ReceiptDetail_VariableValue.UrlParameterName:    grmi.NewConditionItem(meta.ReceiptDetail_VariableValue, grmi.Equal, false),
		meta.ReceiptDetail_VariablePicture.UrlParameterName:  grmi.NewConditionItem(meta.ReceiptDetail_VariablePicture, grmi.Equal, false),
		meta.ReceiptDetail_VariablePosition.UrlParameterName: grmi.NewConditionItem(meta.ReceiptDetail_VariablePosition, grmi.Equal, false),
		meta.ReceiptDetail_JsonValue.UrlParameterName:        grmi.NewConditionItem(meta.ReceiptDetail_JsonValue, grmi.Equal, false),
		meta.ReceiptDetail_CtrlPara1.UrlParameterName:        grmi.NewConditionItem(meta.ReceiptDetail_CtrlPara1, grmi.Equal, false),
		meta.ReceiptDetail_CtrlPara2.UrlParameterName:        grmi.NewConditionItem(meta.ReceiptDetail_CtrlPara2, grmi.Equal, false),
		meta.ReceiptDetail_CtrlStr1.UrlParameterName:         grmi.NewConditionItem(meta.ReceiptDetail_CtrlStr1, grmi.Equal, false),
		meta.ReceiptDetail_CtrlStr2.UrlParameterName:         grmi.NewConditionItem(meta.ReceiptDetail_CtrlStr2, grmi.Equal, false),
		meta.ReceiptDetail_CtrlTime1.UrlParameterName:        grmi.NewConditionItem(meta.ReceiptDetail_CtrlTime1, grmi.Approximate, false),
		meta.ReceiptDetail_CtrlTime2.UrlParameterName:        grmi.NewConditionItem(meta.ReceiptDetail_CtrlTime2, grmi.Approximate, false),
		meta.ReceiptDetail_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.ReceiptDetail_LastModify, grmi.Approximate, false),
		meta.ReceiptDetail_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.ReceiptDetail_LastUser, grmi.Equal, false),
		meta.ReceiptDetail_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.ReceiptDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) InsertOne(user *global.User, entity *model.ReceiptDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptDetailDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ReceiptHeadId, 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_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.DeleteOne
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) DeleteOne(user *global.User, receiptHeadId string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) SelectOne(user *global.User, receiptHeadId string, pos int) (*model.ReceiptDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) UpdateOne(user *global.User, entity *model.ReceiptDetail) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReceiptDetailDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ReceiptHeadId, 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_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.Insert
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) Insert(user *global.User, entities *[]model.ReceiptDetail) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ReceiptHeadId, 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_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.Delete
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) Delete(user *global.User, entities *[]model.ReceiptDetail) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ReceiptHeadId, 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_ACURA_MOM_BACKEND/services/base/ReceiptDetailService.Select
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ReceiptDetail, error) {

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

	condition := DefaultConditionOfReceiptDetailAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewReceiptDetailDAO(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/ReceiptDetailService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfReceiptDetailAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewReceiptDetailDAO(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/ReceiptDetailService.Update
 *
 ******************************************************************************/
func (impl *ReceiptDetailServiceImplement) Update(user *global.User, entities *[]model.ReceiptDetail) error {

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

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

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

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