// 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              : PrintRuleInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 15:09:09
 *
 ******************************************************************************/
var DefaultConditionOfPrintRuleInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.PrintRuleInfo_OperationId.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_OperationId, grmi.Equal, true),
		meta.PrintRuleInfo_PO.UrlParameterName:          grmi.NewConditionItem(meta.PrintRuleInfo_PO, grmi.Equal, false),
		meta.PrintRuleInfo_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.PrintRuleInfo_StepNo, grmi.Equal, false),
		meta.PrintRuleInfo_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.PrintRuleInfo_StepDesc, grmi.Equal, false),
		meta.PrintRuleInfo_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.PrintRuleInfo_ProjectId, grmi.Equal, false),
		meta.PrintRuleInfo_DisplayTime.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_DisplayTime, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara1, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara2, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara3, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara4, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara5, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara6, grmi.Equal, false),
		meta.PrintRuleInfo_PRCtrlPara7.UrlParameterName: grmi.NewConditionItem(meta.PrintRuleInfo_PRCtrlPara7, grmi.Equal, false),
		meta.PrintRuleInfo_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.PrintRuleInfo_LastModify, grmi.Approximate, false),
		meta.PrintRuleInfo_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.PrintRuleInfo_LastUser, grmi.Equal, false),
		meta.PrintRuleInfo_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.PrintRuleInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : PrintRuleInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 15:09:09
 *
 ******************************************************************************/
var DefaultConditionOfPrintRuleInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfPrintRuleInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : PrintRuleInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : PrintRuleInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 15:09:09
 *
 ******************************************************************************/
type PrintRuleInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewPrintRuleInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个PrintRuleInfoDAOImplement实例
 *
 * @Return Value             : PrintRuleInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 15:09:09
 *
 ******************************************************************************/
func NewPrintRuleInfoServiceImplement() *PrintRuleInfoServiceImplement {
	return &PrintRuleInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintRuleInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *PrintRuleInfoServiceImplement) InsertOne(user *models.Usertab, entity *model.PrintRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewPrintRuleInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId)
	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/PrintRuleInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *PrintRuleInfoServiceImplement) DeleteOne(user *models.Usertab, operationId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintRuleInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *PrintRuleInfoServiceImplement) SelectOne(user *models.Usertab, operationId int) (*model.PrintRuleInfo, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/PrintRuleInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *PrintRuleInfoServiceImplement) UpdateOne(user *models.Usertab, entity *model.PrintRuleInfo) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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