// 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              : WorkOrderStatusRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-12 09:59:40
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderStatusRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderStatusRecLst_WorkOrderId.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrderStatusRecLst_WorkOrderId, grmi.Equal, true),
		meta.WorkOrderStatusRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.WorkOrderStatusRecLst_Pos, grmi.Equal, true),
		meta.WorkOrderStatusRecLst_PrevStatus.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderStatusRecLst_PrevStatus, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_Status.UrlParameterName:               grmi.NewConditionItem(meta.WorkOrderStatusRecLst_Status, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_StartTime.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrderStatusRecLst_StartTime, grmi.Approximate, false),
		meta.WorkOrderStatusRecLst_EndTime.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderStatusRecLst_EndTime, grmi.Approximate, false),
		meta.WorkOrderStatusRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderStatusRecLst_TriggerEvent, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderStatusRecLst_TriggerObjectId, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrderStatusRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderStatusRecLst_OutputStatus, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrderStatusRecLst_OutputEvent, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderStatusRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderStatusRecLst_OutputObjectId, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrderStatusRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderStatusRecLst_Remark1, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderStatusRecLst_Remark2, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_OutputErrNum.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderStatusRecLst_OutputErrNum, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderStatusRecLst_LastModify, grmi.Approximate, false),
		meta.WorkOrderStatusRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrderStatusRecLst_LastUser, grmi.Equal, false),
		meta.WorkOrderStatusRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderStatusRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkOrderStatusRecLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderStatusRecLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkOrderStatusRecLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkOrderStatusRecLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkOrderStatusRecLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
type WorkOrderStatusRecLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkOrderStatusRecLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkOrderStatusRecLstDAOImplement实例
 *
 * @Return Value             : WorkOrderStatusRecLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
func NewWorkOrderStatusRecLstServiceImplement() *WorkOrderStatusRecLstServiceImplement {
	return &WorkOrderStatusRecLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderStatusRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusRecLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderStatusRecLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkOrderId, 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_LF_MOM_BACKEND/services/om/WorkOrderStatusRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusRecLstServiceImplement) DeleteOne(user *models.Usertab, workOrderId string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderStatusRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusRecLstServiceImplement) SelectOne(user *models.Usertab, workOrderId string, pos int) (*model.WorkOrderStatusRecLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderStatusRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderStatusRecLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderStatusRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderStatusRecLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkOrderId, 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_LF_MOM_BACKEND/services/om/WorkOrderStatusRecLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkOrderStatusRecLstServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkOrderStatusRecLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkOrderId, 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_LF_MOM_BACKEND/services/om/WorkOrderStatusRecLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkOrderStatusRecLstServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkOrderStatusRecLst) error {

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

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

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

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

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

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

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

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

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