// 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              : ShipOrderStatusHistory的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfShipOrderStatusHistory = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ShipOrderStatusHistory_ShipOrderId.UrlParameterName:      grmi.NewConditionItem(meta.ShipOrderStatusHistory_ShipOrderId, grmi.Equal, true),
		meta.ShipOrderStatusHistory_Pos.UrlParameterName:              grmi.NewConditionItem(meta.ShipOrderStatusHistory_Pos, grmi.Equal, true),
		meta.ShipOrderStatusHistory_StatusChangeType.UrlParameterName: grmi.NewConditionItem(meta.ShipOrderStatusHistory_StatusChangeType, grmi.Equal, false),
		meta.ShipOrderStatusHistory_FromStatus.UrlParameterName:       grmi.NewConditionItem(meta.ShipOrderStatusHistory_FromStatus, grmi.Equal, false),
		meta.ShipOrderStatusHistory_ToStatus.UrlParameterName:         grmi.NewConditionItem(meta.ShipOrderStatusHistory_ToStatus, grmi.Equal, false),
		meta.ShipOrderStatusHistory_TriggerEvent.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderStatusHistory_TriggerEvent, grmi.Equal, false),
		meta.ShipOrderStatusHistory_TriggerObjId.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderStatusHistory_TriggerObjId, grmi.Equal, false),
		meta.ShipOrderStatusHistory_ChangeTime.UrlParameterName:       grmi.NewConditionItem(meta.ShipOrderStatusHistory_ChangeTime, grmi.Approximate, false),
		meta.ShipOrderStatusHistory_TriggerPara1.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderStatusHistory_TriggerPara1, grmi.Equal, false),
		meta.ShipOrderStatusHistory_TriggerPara2.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderStatusHistory_TriggerPara2, grmi.Equal, false),
		meta.ShipOrderStatusHistory_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.ShipOrderStatusHistory_LastModify, grmi.Approximate, false),
		meta.ShipOrderStatusHistory_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.ShipOrderStatusHistory_LastUser, grmi.Equal, false),
		meta.ShipOrderStatusHistory_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.ShipOrderStatusHistory_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderStatusHistoryService.InsertOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusHistoryServiceImplement) InsertOne(user *global.User, entity *model.ShipOrderStatusHistory) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderStatusHistoryService.SelectOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusHistoryServiceImplement) SelectOne(user *global.User, shipOrderId string, pos int) (*model.ShipOrderStatusHistory, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderStatusHistoryService.UpdateOne
 *
 ******************************************************************************/
func (impl *ShipOrderStatusHistoryServiceImplement) UpdateOne(user *global.User, entity *model.ShipOrderStatusHistory) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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