// 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              : ShipOrderDataLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-19 10:26:40
 *
 ******************************************************************************/
var DefaultConditionOfShipOrderDataLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ShipOrderDataLst_ShipOrderId.UrlParameterName: grmi.NewConditionItem(meta.ShipOrderDataLst_ShipOrderId, grmi.Equal, true),
		meta.ShipOrderDataLst_PackOrderId.UrlParameterName: grmi.NewConditionItem(meta.ShipOrderDataLst_PackOrderId, grmi.Equal, true),
		meta.ShipOrderDataLst_ItemPos.UrlParameterName: grmi.NewConditionItem(meta.ShipOrderDataLst_ItemPos, grmi.Equal, false),
		meta.ShipOrderDataLst_PackTypeId.UrlParameterName:  grmi.NewConditionItem(meta.ShipOrderDataLst_PackTypeId, grmi.Equal, false),
		meta.ShipOrderDataLst_Status.UrlParameterName:      grmi.NewConditionItem(meta.ShipOrderDataLst_Status, grmi.Equal, false),
		meta.ShipOrderDataLst_Status1.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderDataLst_Status1, grmi.Equal, false),
		meta.ShipOrderDataLst_Status2.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderDataLst_Status2, grmi.Equal, false),
		meta.ShipOrderDataLst_PlanQty.UrlParameterName:     grmi.NewConditionItem(meta.ShipOrderDataLst_PlanQty, grmi.Equal, false),
		meta.ShipOrderDataLst_ActQty.UrlParameterName:      grmi.NewConditionItem(meta.ShipOrderDataLst_ActQty, grmi.Equal, false),
		meta.ShipOrderDataLst_CtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.ShipOrderDataLst_CtrlPara1, grmi.Equal, false),
		meta.ShipOrderDataLst_CtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.ShipOrderDataLst_CtrlPara2, grmi.Equal, false),
		meta.ShipOrderDataLst_CtrlStr1.UrlParameterName:    grmi.NewConditionItem(meta.ShipOrderDataLst_CtrlStr1, grmi.Equal, false),
		meta.ShipOrderDataLst_CtrlStr2.UrlParameterName:    grmi.NewConditionItem(meta.ShipOrderDataLst_CtrlStr2, grmi.Equal, false),
		meta.ShipOrderDataLst_CtrlTime1.UrlParameterName:   grmi.NewConditionItem(meta.ShipOrderDataLst_CtrlTime1, grmi.Approximate, false),
		meta.ShipOrderDataLst_CtrlTime2.UrlParameterName:   grmi.NewConditionItem(meta.ShipOrderDataLst_CtrlTime2, grmi.Approximate, false),
		meta.ShipOrderDataLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ShipOrderDataLst_LastModify, grmi.Approximate, false),
		meta.ShipOrderDataLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ShipOrderDataLst_LastUser, grmi.Equal, false),
		meta.ShipOrderDataLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ShipOrderDataLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderDataLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ShipOrderDataLstServiceImplement) InsertOne(user *global.User, entity *model.ShipOrderDataLst) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderDataLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ShipOrderDataLstServiceImplement) SelectOne(user *global.User, shipOrderId string, packOrderId string) (*model.ShipOrderDataLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/jit/ShipOrderDataLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ShipOrderDataLstServiceImplement) UpdateOne(user *global.User, entity *model.ShipOrderDataLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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