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

package implments

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

/******************************************************************************
 *
 * @Description              : SerialOrderStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderStatus_SerialOrderId.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderStatus_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderStatus_Status.UrlParameterName:             grmi.NewConditionItem(meta.SerialOrderStatus_Status, grmi.Equal, false),
		meta.SerialOrderStatus_TriggerEvent.UrlParameterName:       grmi.NewConditionItem(meta.SerialOrderStatus_TriggerEvent, grmi.Equal, false),
		meta.SerialOrderStatus_TriggerObjectId.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderStatus_TriggerObjectId, grmi.Equal, false),
		meta.SerialOrderStatus_TriggerSubObjectId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderStatus_TriggerSubObjectId, grmi.Equal, false),
		meta.SerialOrderStatus_LastModify.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStatus_LastModify, grmi.Approximate, false),
		meta.SerialOrderStatus_LastUser.UrlParameterName:           grmi.NewConditionItem(meta.SerialOrderStatus_LastUser, grmi.Equal, false),
		meta.SerialOrderStatus_CreateTime.UrlParameterName:         grmi.NewConditionItem(meta.SerialOrderStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderStatus的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderStatusAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderStatus.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderStatusServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderStatus的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
type SerialOrderStatusServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderStatusDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderStatusDAOImplement实例
 *
 * @Return Value             : SerialOrderStatusDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-20 13:07:32
 *
 ******************************************************************************/
func NewSerialOrderStatusServiceImplement() *SerialOrderStatusServiceImplement {
	return &SerialOrderStatusServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/SerialOrderStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) InsertOne(user *models.Usertab, entity *model.SerialOrderStatus) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "InsertOneSerialOrderStatus", "插入一个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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/om/SerialOrderStatusService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) DeleteOne(user *models.Usertab, serialOrderId string) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "DeleteOneSerialOrderStatus", "删除一个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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/om/SerialOrderStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) SelectOne(user *models.Usertab, serialOrderId string) (*model.SerialOrderStatus, error) {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "SelectOneSerialOrderStatus", "查询一个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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/om/SerialOrderStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) UpdateOne(user *models.Usertab, entity *model.SerialOrderStatus) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "UpdateOneSerialOrderStatus", "修改一个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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/om/SerialOrderStatusService.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) Insert(user *models.Usertab, entities *[]model.SerialOrderStatus) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "InsertSerialOrderStatus", "插入多个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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/om/SerialOrderStatusService.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) Delete(user *models.Usertab, entities *[]model.SerialOrderStatus) error {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "DeleteSerialOrderStatus", "删除多个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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/om/SerialOrderStatusService.Select
 *
 ******************************************************************************/
func (impl *SerialOrderStatusServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "SelectSerialOrderStatus", "查询SerialOrderStatus")

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

	grmi.Log(user, "/services/om/implments/SerialOrderStatus.service.impl.go", "UpdateSerialOrderStatus", "修改多个SerialOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderStatusDAO(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
}
