// 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              : WorkOrderQtyRecLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderQtyRecLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderQtyRecLst_WorkOrderId.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrderQtyRecLst_WorkOrderId, grmi.Equal, true),
		meta.WorkOrderQtyRecLst_Pos.UrlParameterName:                  grmi.NewConditionItem(meta.WorkOrderQtyRecLst_Pos, grmi.Equal, true),
		meta.WorkOrderQtyRecLst_ActQty.UrlParameterName:               grmi.NewConditionItem(meta.WorkOrderQtyRecLst_ActQty, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_OrderQtyType.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderQtyRecLst_OrderQtyType, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_RecordTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderQtyRecLst_RecordTime, grmi.Approximate, false),
		meta.WorkOrderQtyRecLst_TriggerEvent.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderQtyRecLst_TriggerEvent, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_TriggerObjectId.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderQtyRecLst_TriggerObjectId, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_TriggerSubObjectId.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrderQtyRecLst_TriggerSubObjectId, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_OutputStatus.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrderQtyRecLst_OutputStatus, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_OutputEvent.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrderQtyRecLst_OutputEvent, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_OutputEventMessageId.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderQtyRecLst_OutputEventMessageId, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_OutputObjectId.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderQtyRecLst_OutputObjectId, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_OutputSubObjectId.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrderQtyRecLst_OutputSubObjectId, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_Remark1.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderQtyRecLst_Remark1, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_Remark2.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrderQtyRecLst_Remark2, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderQtyRecLst_LastModify, grmi.Approximate, false),
		meta.WorkOrderQtyRecLst_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrderQtyRecLst_LastUser, grmi.Equal, false),
		meta.WorkOrderQtyRecLst_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrderQtyRecLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderQtyRecLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderQtyRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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/WorkOrderQtyRecLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) DeleteOne(user *models.Usertab, workOrderId string, pos int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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/WorkOrderQtyRecLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) SelectOne(user *models.Usertab, workOrderId string, pos int) (*model.WorkOrderQtyRecLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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/WorkOrderQtyRecLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderQtyRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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/WorkOrderQtyRecLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkOrderQtyRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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/WorkOrderQtyRecLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkOrderQtyRecLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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/WorkOrderQtyRecLstService.Select
 *
 ******************************************************************************/
func (impl *WorkOrderQtyRecLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyRecLstDAO(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
}
