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

package implments

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

/******************************************************************************
 *
 * @Description              : ReleaseTaskConfig的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-23 17:23:13
 *
 ******************************************************************************/
var DefaultConditionOfReleaseTaskConfig = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ReleaseTaskConfig_ConfigNr.UrlParameterName:        grmi.NewConditionItem(meta.ReleaseTaskConfig_ConfigNr, grmi.Equal, true),
		meta.ReleaseTaskConfig_LeadTime.UrlParameterName:        grmi.NewConditionItem(meta.ReleaseTaskConfig_LeadTime, grmi.Equal, false),
		meta.ReleaseTaskConfig_TimeUomId.UrlParameterName:       grmi.NewConditionItem(meta.ReleaseTaskConfig_TimeUomId, grmi.Equal, false),
		meta.ReleaseTaskConfig_BusinessObjType.UrlParameterName: grmi.NewConditionItem(meta.ReleaseTaskConfig_BusinessObjType, grmi.Equal, false),
		meta.ReleaseTaskConfig_BusinessObjId.UrlParameterName:   grmi.NewConditionItem(meta.ReleaseTaskConfig_BusinessObjId, grmi.Equal, false),
		meta.ReleaseTaskConfig_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.ReleaseTaskConfig_LastModify, grmi.Approximate, false),
		meta.ReleaseTaskConfig_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.ReleaseTaskConfig_LastUser, grmi.Equal, false),
		meta.ReleaseTaskConfig_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.ReleaseTaskConfig_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ReleaseTaskConfig的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-23 17:23:13
 *
 ******************************************************************************/
var DefaultConditionOfReleaseTaskConfigAndPaging = grmi.NewCondition(
	DefaultConditionOfReleaseTaskConfig.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ReleaseTaskConfigServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ReleaseTaskConfig的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-23 17:23:13
 *
 ******************************************************************************/
type ReleaseTaskConfigServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewReleaseTaskConfigDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ReleaseTaskConfigDAOImplement实例
 *
 * @Return Value             : ReleaseTaskConfigDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-06-23 17:23:13
 *
 ******************************************************************************/
func NewReleaseTaskConfigServiceImplement() *ReleaseTaskConfigServiceImplement {
	return &ReleaseTaskConfigServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ReleaseTaskConfigService.InsertOne
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) InsertOne(user *global.User, entity *model.ReleaseTaskConfig) error {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "InsertOneReleaseTaskConfig", "插入一个ReleaseTaskConfig")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReleaseTaskConfigDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ConfigNr)
	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/pln/ReleaseTaskConfigService.DeleteOne
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) DeleteOne(user *global.User, configNr int) error {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "DeleteOneReleaseTaskConfig", "删除一个ReleaseTaskConfig")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ReleaseTaskConfigService.SelectOne
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) SelectOne(user *global.User, configNr int) (*model.ReleaseTaskConfig, error) {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "SelectOneReleaseTaskConfig", "查询一个ReleaseTaskConfig")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ReleaseTaskConfigService.UpdateOne
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) UpdateOne(user *global.User, entity *model.ReleaseTaskConfig) error {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "UpdateOneReleaseTaskConfig", "修改一个ReleaseTaskConfig")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReleaseTaskConfigDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ConfigNr)
	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/pln/ReleaseTaskConfigService.Insert
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) Insert(user *global.User, entities *[]model.ReleaseTaskConfig) error {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "InsertReleaseTaskConfig", "插入多个ReleaseTaskConfig")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ConfigNr)
		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/pln/ReleaseTaskConfigService.Delete
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) Delete(user *global.User, entities *[]model.ReleaseTaskConfig) error {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "DeleteReleaseTaskConfig", "删除多个ReleaseTaskConfig")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ConfigNr)
		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/pln/ReleaseTaskConfigService.Select
 *
 ******************************************************************************/
func (impl *ReleaseTaskConfigServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ReleaseTaskConfig, error) {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "SelectReleaseTaskConfig", "查询ReleaseTaskConfig")

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

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "SelectReleaseTaskConfig", "查询ReleaseTaskConfig")

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

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "UpdateReleaseTaskConfig", "修改多个ReleaseTaskConfig")

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

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

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

// Save 保存
func (impl *ReleaseTaskConfigServiceImplement) Save(user *global.User, entity *model.ReleaseTaskConfig) error  {
	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "SaveReleaseTaskConfig", "插入一个ReleaseTaskConfig")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReleaseTaskConfigDAO(session, user.PlantNr, user.UserId)
	if err := session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败，错误:" + err.Error())
	}
	var err error
	defer func() {
		if err != nil {
			_ = session.Rollback()
		} else {
			_ = session.Commit()
		}
	}()
	err = dao.DeleteWhere([]grmi.Predicate{
		meta.ReleaseTaskConfig_BusinessObjType.NewPredicate(grmi.Equal, entity.BusinessObjType),
		meta.ReleaseTaskConfig_BusinessObjId.NewPredicate(grmi.Equal, entity.BusinessObjId),
	})
	if err != nil {
		return err
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

// SelectConfig 查询配置
func (impl *ReleaseTaskConfigServiceImplement) SelectConfig(user *global.User, objType, objId string) (*model.ReleaseTaskConfig, error) {

	grmi.Log(user, "/services/pln/implments/ReleaseTaskConfig.service.impl.go", "SelectOneReleaseTaskConfig", "查询一个ReleaseTaskConfig")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReleaseTaskConfigDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select([]grmi.Predicate{
		meta.ReleaseTaskConfig_BusinessObjType.NewPredicate(grmi.Equal, objType),
		meta.ReleaseTaskConfig_BusinessObjId.NewPredicate(grmi.Equal, objId)}, nil)
	if err != nil {
		return nil, err
	}
	if len(result) == 0 {
		return &model.ReleaseTaskConfig{}, nil
	}
	return &result[0], nil
}