// 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              : PrintDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfPrintDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PrintDetail_PrintHeadId.UrlParameterName: grmi.NewConditionItem(meta.PrintDetail_PrintHeadId, grmi.Equal, true),
		meta.PrintDetail_Pos.UrlParameterName:         grmi.NewConditionItem(meta.PrintDetail_Pos, grmi.Equal, true),
		meta.PrintDetail_VarName.UrlParameterName:     grmi.NewConditionItem(meta.PrintDetail_VarName, grmi.Equal, false),
		meta.PrintDetail_VarValue.UrlParameterName:    grmi.NewConditionItem(meta.PrintDetail_VarValue, grmi.Equal, false),
		meta.PrintDetail_VarType.UrlParameterName:     grmi.NewConditionItem(meta.PrintDetail_VarType, grmi.Equal, false),
		meta.PrintDetail_VarPos.UrlParameterName:      grmi.NewConditionItem(meta.PrintDetail_VarPos, grmi.Equal, false),
		meta.PrintDetail_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.PrintDetail_LastModify, grmi.Approximate, false),
		meta.PrintDetail_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.PrintDetail_LastUser, grmi.Equal, false),
		meta.PrintDetail_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.PrintDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PrintDetail的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfPrintDetailAndPaging = grmi.NewCondition(
	DefaultConditionOfPrintDetail.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PrintDetailServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PrintDetail的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
type PrintDetailServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPrintDetailDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PrintDetailDAOImplement实例
 *
 * @Return Value             : PrintDetailDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
func NewPrintDetailServiceImplement() *PrintDetailServiceImplement {
	return &PrintDetailServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *PrintDetailServiceImplement) InsertOne(user *models.Usertab, entity *model.PrintDetail) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *PrintDetailServiceImplement) SelectOne(user *models.Usertab, printHeadId string, pos int) (*model.PrintDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrintDetailServiceImplement) UpdateOne(user *models.Usertab, entity *model.PrintDetail) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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