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

package implments

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

/******************************************************************************
 *
 * @Description              : ShipOrderStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfShipOrderStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ShipOrderStatus_ShipOrderId.UrlParameterName:       grmi.NewConditionItem(meta.ShipOrderStatus_ShipOrderId, grmi.Equal, true),
		meta.ShipOrderStatus_Status.UrlParameterName:            grmi.NewConditionItem(meta.ShipOrderStatus_Status, grmi.Equal, false),
		meta.ShipOrderStatus_Status1.UrlParameterName:           grmi.NewConditionItem(meta.ShipOrderStatus_Status1, grmi.Equal, false),
		meta.ShipOrderStatus_Status2.UrlParameterName:           grmi.NewConditionItem(meta.ShipOrderStatus_Status2, grmi.Equal, false),
		meta.ShipOrderStatus_BackFlushedToggle.UrlParameterName: grmi.NewConditionItem(meta.ShipOrderStatus_BackFlushedToggle, grmi.Equal, false),
		meta.ShipOrderStatus_LastModify.UrlParameterName:        grmi.NewConditionItem(meta.ShipOrderStatus_LastModify, grmi.Approximate, false),
		meta.ShipOrderStatus_LastUser.UrlParameterName:          grmi.NewConditionItem(meta.ShipOrderStatus_LastUser, grmi.Equal, false),
		meta.ShipOrderStatus_CreateTime.UrlParameterName:        grmi.NewConditionItem(meta.ShipOrderStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ShipOrderStatus的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfShipOrderStatusAndPaging = grmi.NewCondition(
	DefaultConditionOfShipOrderStatus.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ShipOrderStatusServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ShipOrderStatus的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
type ShipOrderStatusServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewShipOrderStatusDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ShipOrderStatusDAOImplement实例
 *
 * @Return Value             : ShipOrderStatusDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
func NewShipOrderStatusServiceImplement() *ShipOrderStatusServiceImplement {
	return &ShipOrderStatusServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) InsertOne(user *global.User, entity *model.ShipOrderStatus) error {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "InsertOneShipOrderStatus", "插入一个ShipOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewShipOrderStatusDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ShipOrderId)
	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/jit/ShipOrderStatusService.DeleteOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) DeleteOne(user *global.User, shipOrderId string) error {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "DeleteOneShipOrderStatus", "删除一个ShipOrderStatus")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) SelectOne(user *global.User, shipOrderId string) (*model.ShipOrderStatus, error) {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "SelectOneShipOrderStatus", "查询一个ShipOrderStatus")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) UpdateOne(user *global.User, entity *model.ShipOrderStatus) error {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "UpdateOneShipOrderStatus", "修改一个ShipOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewShipOrderStatusDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ShipOrderId)
	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/jit/ShipOrderStatusService.Insert
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) Insert(user *global.User, entities *[]model.ShipOrderStatus) error {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "InsertShipOrderStatus", "插入多个ShipOrderStatus")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ShipOrderId)
		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/jit/ShipOrderStatusService.Delete
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) Delete(user *global.User, entities *[]model.ShipOrderStatus) error {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "DeleteShipOrderStatus", "删除多个ShipOrderStatus")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ShipOrderId)
		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/jit/ShipOrderStatusService.Select
 *
 ******************************************************************************/
func (impl *ShipOrderStatusServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ShipOrderStatus, error) {

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "SelectShipOrderStatus", "查询ShipOrderStatus")

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

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "SelectShipOrderStatus", "查询ShipOrderStatus")

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

	grmi.Log(user, "/services/jit/implments/ShipOrderStatus.service.impl.go", "UpdateShipOrderStatus", "修改多个ShipOrderStatus")

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

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

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