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

package implments

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

/******************************************************************************
 *
 * @Description              : StationOrder的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-18 11:12:43
 *
 ******************************************************************************/
var DefaultConditionOfStationOrder = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StationOrder_Station.UrlParameterName:       grmi.NewConditionItem(meta.StationOrder_Station, grmi.Equal, true),
		meta.StationOrder_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.StationOrder_ArtId, grmi.Equal, false),
		meta.StationOrder_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.StationOrder_SerialOrderId, grmi.Equal, false),
		meta.StationOrder_ODSCtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.StationOrder_ODSCtrlPara1, grmi.Equal, false),
		meta.StationOrder_ODSCtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.StationOrder_ODSCtrlPara2, grmi.Equal, false),
		meta.StationOrder_ODSCtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.StationOrder_ODSCtrlPara3, grmi.Equal, false),
		meta.StationOrder_ODSCtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.StationOrder_ODSCtrlPara4, grmi.Equal, false),
		meta.StationOrder_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.StationOrder_LastModify, grmi.Approximate, false),
		meta.StationOrder_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.StationOrder_LastUser, grmi.Equal, false),
		meta.StationOrder_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.StationOrder_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

/******************************************************************************
 *
 * @Function Name            : NewStationOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个StationOrderDAOImplement实例
 *
 * @Return Value             : StationOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-18 11:12:43
 *
 ******************************************************************************/
func NewStationOrderServiceImplement() *StationOrderServiceImplement {
	return &StationOrderServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/StationOrderService.InsertOne
 *
 ******************************************************************************/
func (impl *StationOrderServiceImplement) InsertOne(user *models.Usertab, entity *model.StationOrder) error {

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "InsertOneStationOrder", "插入一个StationOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStationOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.Station)
	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/ods/StationOrderService.DeleteOne
 *
 ******************************************************************************/
func (impl *StationOrderServiceImplement) DeleteOne(user *models.Usertab, station string) error {

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "DeleteOneStationOrder", "删除一个StationOrder")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/StationOrderService.SelectOne
 *
 ******************************************************************************/
func (impl *StationOrderServiceImplement) SelectOne(user *models.Usertab, station string) (*model.StationOrder, error) {

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "SelectOneStationOrder", "查询一个StationOrder")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/ods/StationOrderService.UpdateOne
 *
 ******************************************************************************/
func (impl *StationOrderServiceImplement) UpdateOne(user *models.Usertab, entity *model.StationOrder) error {

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "UpdateOneStationOrder", "修改一个StationOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStationOrderDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.Station)
	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/ods/StationOrderService.Insert
 *
 ******************************************************************************/
func (impl *StationOrderServiceImplement) Insert(user *models.Usertab, entities *[]model.StationOrder) error {

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "InsertStationOrder", "插入多个StationOrder")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.Station)
		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/ods/StationOrderService.Delete
 *
 ******************************************************************************/
func (impl *StationOrderServiceImplement) Delete(user *models.Usertab, entities *[]model.StationOrder) error {

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "DeleteStationOrder", "删除多个StationOrder")

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

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

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "SelectStationOrder", "查询StationOrder")

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

	grmi.Log(user, "/services/ods/implments/StationOrder.service.impl.go", "UpdateStationOrder", "修改多个StationOrder")

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

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

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