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

package implments

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

/******************************************************************************
 *
 * @Description              : ProdOrderStatus的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderStatus = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProdOrderStatus_ProdOrderId.UrlParameterName:        grmi.NewConditionItem(meta.ProdOrderStatus_ProdOrderId, grmi.Equal, true),
		meta.ProdOrderStatus_Status.UrlParameterName:             grmi.NewConditionItem(meta.ProdOrderStatus_Status, grmi.Equal, false),
		meta.ProdOrderStatus_TriggerEvent.UrlParameterName:       grmi.NewConditionItem(meta.ProdOrderStatus_TriggerEvent, grmi.Equal, false),
		meta.ProdOrderStatus_TriggerObjectId.UrlParameterName:    grmi.NewConditionItem(meta.ProdOrderStatus_TriggerObjectId, grmi.Equal, false),
		meta.ProdOrderStatus_TriggerSubObjectId.UrlParameterName: grmi.NewConditionItem(meta.ProdOrderStatus_TriggerSubObjectId, grmi.Equal, false),
		meta.ProdOrderStatus_LastModify.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrderStatus_LastModify, grmi.Approximate, false),
		meta.ProdOrderStatus_LastUser.UrlParameterName:           grmi.NewConditionItem(meta.ProdOrderStatus_LastUser, grmi.Equal, false),
		meta.ProdOrderStatus_CreateTime.UrlParameterName:         grmi.NewConditionItem(meta.ProdOrderStatus_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProdOrderStatus的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfProdOrderStatusAndPaging = grmi.NewCondition(
	DefaultConditionOfProdOrderStatus.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : ProdOrderStatusServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : ProdOrderStatus的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type ProdOrderStatusServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProdOrderStatusDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProdOrderStatusDAOImplement实例
 *
 * @Return Value             : ProdOrderStatusDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewProdOrderStatusServiceImplement() *ProdOrderStatusServiceImplement {
	return &ProdOrderStatusServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderStatusService.InsertOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusServiceImplement) InsertOne(user *models.Usertab, entity *model.ProdOrderStatus) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "InsertOneProdOrderStatus", "插入一个ProdOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderStatusDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId)
	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/om/ProdOrderStatusService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusServiceImplement) DeleteOne(user *models.Usertab, prodOrderId string) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "DeleteOneProdOrderStatus", "删除一个ProdOrderStatus")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderStatusService.SelectOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusServiceImplement) SelectOne(user *models.Usertab, prodOrderId string) (*model.ProdOrderStatus, error) {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "SelectOneProdOrderStatus", "查询一个ProdOrderStatus")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/ProdOrderStatusService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProdOrderStatusServiceImplement) UpdateOne(user *models.Usertab, entity *model.ProdOrderStatus) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "UpdateOneProdOrderStatus", "修改一个ProdOrderStatus")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProdOrderStatusDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ProdOrderId)
	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/om/ProdOrderStatusService.Insert
 *
 ******************************************************************************/
func (impl *ProdOrderStatusServiceImplement) Insert(user *models.Usertab, entities *[]model.ProdOrderStatus) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "InsertProdOrderStatus", "插入多个ProdOrderStatus")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ProdOrderId)
		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/om/ProdOrderStatusService.Delete
 *
 ******************************************************************************/
func (impl *ProdOrderStatusServiceImplement) Delete(user *models.Usertab, entities *[]model.ProdOrderStatus) error {

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "DeleteProdOrderStatus", "删除多个ProdOrderStatus")

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

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "SelectProdOrderStatus", "查询ProdOrderStatus")

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

	grmi.Log(user, "/services/om/implments/OM_ProdOrderStatus.service.impl.go", "UpdateProdOrderStatus", "修改多个ProdOrderStatus")

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

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

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