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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/qm"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/qm"
	model "LAPP_LF_MOM_BACKEND/models/qm"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : SerialOrderRuting的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-25 11:18:25
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderRuting = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderRuting_SerialorderId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderRuting_SerialorderId, grmi.Equal, true),
		meta.SerialOrderRuting_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderRuting_ArtId, grmi.Equal, false),
		meta.SerialOrderRuting_WorkPlaceNr.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderRuting_WorkPlaceNr, grmi.Equal, false),
		meta.SerialOrderRuting_Status.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderRuting_Status, grmi.Equal, false),
		meta.SerialOrderRuting_IsPack.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderRuting_IsPack, grmi.Equal, false),
		meta.SerialOrderRuting_Remark.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderRuting_Remark, grmi.Equal, false),
		meta.SerialOrderRuting_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderRuting_LastModify, grmi.Approximate, false),
		meta.SerialOrderRuting_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderRuting_LastUser, grmi.Equal, false),
		meta.SerialOrderRuting_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderRuting_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderRuting的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-25 11:18:25
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderRutingAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderRuting.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderRutingServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderRuting的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-25 11:18:25
 *
 ******************************************************************************/
type SerialOrderRutingServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderRutingDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderRutingDAOImplement实例
 *
 * @Return Value             : SerialOrderRutingDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-25 11:18:25
 *
 ******************************************************************************/
func NewSerialOrderRutingServiceImplement() *SerialOrderRutingServiceImplement {
	return &SerialOrderRutingServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/SerialOrderRutingService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderRutingServiceImplement) InsertOne(user *models.Usertab, entity *model.SerialOrderRuting) error {

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "InsertOneSerialOrderRuting", "插入一个SerialOrderRuting")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderRutingDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.SerialorderId)
	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/qm/SerialOrderRutingService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderRutingServiceImplement) DeleteOne(user *models.Usertab, serialorderId string) error {

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "DeleteOneSerialOrderRuting", "删除一个SerialOrderRuting")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/SerialOrderRutingService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderRutingServiceImplement) SelectOne(user *models.Usertab, serialorderId string) (*model.SerialOrderRuting, error) {

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "SelectOneSerialOrderRuting", "查询一个SerialOrderRuting")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/SerialOrderRutingService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderRutingServiceImplement) UpdateOne(user *models.Usertab, entity *model.SerialOrderRuting) error {

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "UpdateOneSerialOrderRuting", "修改一个SerialOrderRuting")

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

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "InsertSerialOrderRuting", "插入多个SerialOrderRuting")

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

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

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "DeleteSerialOrderRuting", "删除多个SerialOrderRuting")

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

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

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "SelectSerialOrderRuting", "查询SerialOrderRuting")

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

	grmi.Log(user, "/services/qm/implments/SerialOrderRuting.service.impl.go", "UpdateSerialOrderRuting", "修改多个SerialOrderRuting")

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

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

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