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

package implments

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

/******************************************************************************
 *
 * @Description              : SerialOrderPOLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderPOLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SerialOrderPOLst_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.SerialOrderPOLst_SerialOrderId, grmi.Equal, true),
		meta.SerialOrderPOLst_PO.UrlParameterName:            grmi.NewConditionItem(meta.SerialOrderPOLst_PO, grmi.Equal, true),
		meta.SerialOrderPOLst_Status.UrlParameterName:        grmi.NewConditionItem(meta.SerialOrderPOLst_Status, grmi.Equal, false),
		meta.SerialOrderPOLst_HasDependOn.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_HasDependOn, grmi.Equal, false),
		meta.SerialOrderPOLst_SWCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_SWCtrlPara1, grmi.Equal, false),
		meta.SerialOrderPOLst_SWCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_SWCtrlPara2, grmi.Equal, false),
		meta.SerialOrderPOLst_SWCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_SWCtrlPara3, grmi.Equal, false),
		meta.SerialOrderPOLst_SWCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_SWCtrlPara4, grmi.Equal, false),
		meta.SerialOrderPOLst_SWCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_SWCtrlPara5, grmi.Equal, false),
		meta.SerialOrderPOLst_SWCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.SerialOrderPOLst_SWCtrlPara6, grmi.Equal, false),
		meta.SerialOrderPOLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderPOLst_LastModify, grmi.Approximate, false),
		meta.SerialOrderPOLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.SerialOrderPOLst_LastUser, grmi.Equal, false),
		meta.SerialOrderPOLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.SerialOrderPOLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SerialOrderPOLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
var DefaultConditionOfSerialOrderPOLstAndPaging = grmi.NewCondition(
	DefaultConditionOfSerialOrderPOLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SerialOrderPOLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SerialOrderPOLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
type SerialOrderPOLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSerialOrderPOLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SerialOrderPOLstDAOImplement实例
 *
 * @Return Value             : SerialOrderPOLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-29 17:19:37
 *
 ******************************************************************************/
func NewSerialOrderPOLstServiceImplement() *SerialOrderPOLstServiceImplement {
	return &SerialOrderPOLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderPOLstService.InsertOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) InsertOne(user *global.User, entity *model.SerialOrderPOLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderPOLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.PO)
	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/om/SerialOrderPOLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) DeleteOne(user *global.User, serialOrderId string, pO string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderPOLstService.SelectOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) SelectOne(user *global.User, serialOrderId string, pO string) (*model.SerialOrderPOLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/SerialOrderPOLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) UpdateOne(user *global.User, entity *model.SerialOrderPOLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSerialOrderPOLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.SerialOrderId, entity.PO)
	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/om/SerialOrderPOLstService.Insert
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) Insert(user *global.User, entities *[]model.SerialOrderPOLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.PO)
		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/om/SerialOrderPOLstService.Delete
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) Delete(user *global.User, entities *[]model.SerialOrderPOLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.SerialOrderId, entity.PO)
		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/om/SerialOrderPOLstService.Select
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfSerialOrderPOLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewSerialOrderPOLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/om/SerialOrderPOLstService.Update
 *
 ******************************************************************************/
func (impl *SerialOrderPOLstServiceImplement) Update(user *global.User, entities *[]model.SerialOrderPOLst) error {

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

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

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

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