// 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              : DemandLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 17:06:45
 *
 ******************************************************************************/
var DefaultConditionOfDemandLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DemandLst_FileNr.UrlParameterName:           grmi.NewConditionItem(meta.DemandLst_FileNr, grmi.Equal, true),
		meta.DemandLst_Pos.UrlParameterName:              grmi.NewConditionItem(meta.DemandLst_Pos, grmi.Equal, true),
		meta.DemandLst_TaskNr.UrlParameterName:           grmi.NewConditionItem(meta.DemandLst_TaskNr, grmi.Equal, true),
		meta.DemandLst_DemandNr.UrlParameterName:         grmi.NewConditionItem(meta.DemandLst_DemandNr, grmi.Equal, false),
		meta.DemandLst_BarCode.UrlParameterName:          grmi.NewConditionItem(meta.DemandLst_BarCode, grmi.Equal, false),
		meta.DemandLst_BarCodeDesc.UrlParameterName:      grmi.NewConditionItem(meta.DemandLst_BarCodeDesc, grmi.Equal, false),
		meta.DemandLst_PartCode.UrlParameterName:         grmi.NewConditionItem(meta.DemandLst_PartCode, grmi.Equal, false),
		meta.DemandLst_PlanDttm.UrlParameterName:         grmi.NewConditionItem(meta.DemandLst_PlanDttm, grmi.Approximate, false),
		meta.DemandLst_Class1.UrlParameterName:           grmi.NewConditionItem(meta.DemandLst_Class1, grmi.Equal, false),
		meta.DemandLst_ConfigDesc.UrlParameterName:       grmi.NewConditionItem(meta.DemandLst_ConfigDesc, grmi.Equal, false),
		meta.DemandLst_ConfigKey.UrlParameterName:        grmi.NewConditionItem(meta.DemandLst_ConfigKey, grmi.Equal, false),
		meta.DemandLst_Seq.UrlParameterName:              grmi.NewConditionItem(meta.DemandLst_Seq, grmi.Equal, false),
		meta.DemandLst_State.UrlParameterName:            grmi.NewConditionItem(meta.DemandLst_State, grmi.Equal, false),
		meta.DemandLst_StateName.UrlParameterName:        grmi.NewConditionItem(meta.DemandLst_StateName, grmi.Equal, false),
		meta.DemandLst_Station.UrlParameterName:          grmi.NewConditionItem(meta.DemandLst_Station, grmi.Equal, false),
		meta.DemandLst_StationCode.UrlParameterName:      grmi.NewConditionItem(meta.DemandLst_StationCode, grmi.Equal, false),
		meta.DemandLst_StationDesc.UrlParameterName:      grmi.NewConditionItem(meta.DemandLst_StationDesc, grmi.Equal, false),
		meta.DemandLst_DemandCreateTime.UrlParameterName: grmi.NewConditionItem(meta.DemandLst_DemandCreateTime, grmi.Approximate, false),
		meta.DemandLst_IsRefreshToggle.UrlParameterName:  grmi.NewConditionItem(meta.DemandLst_IsRefreshToggle, grmi.Equal, false),
		meta.DemandLst_Serial.UrlParameterName:           grmi.NewConditionItem(meta.DemandLst_Serial, grmi.Equal, false),
		meta.DemandLst_CustomId.UrlParameterName:         grmi.NewConditionItem(meta.DemandLst_CustomId, grmi.Equal, false),
		meta.DemandLst_LogTime.UrlParameterName:          grmi.NewConditionItem(meta.DemandLst_LogTime, grmi.Approximate, false),
		meta.DemandLst_CodeBar.UrlParameterName:          grmi.NewConditionItem(meta.DemandLst_CodeBar, grmi.Equal, false),
		meta.DemandLst_Memo.UrlParameterName:             grmi.NewConditionItem(meta.DemandLst_Memo, grmi.Equal, false),
		meta.DemandLst_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.DemandLst_LastModify, grmi.Approximate, false),
		meta.DemandLst_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.DemandLst_LastUser, grmi.Equal, false),
		meta.DemandLst_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.DemandLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DemandLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 17:06:45
 *
 ******************************************************************************/
var DefaultConditionOfDemandLstAndPaging = grmi.NewCondition(
	DefaultConditionOfDemandLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : DemandLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : DemandLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 17:06:45
 *
 ******************************************************************************/
type DemandLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDemandLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DemandLstDAOImplement实例
 *
 * @Return Value             : DemandLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 17:06:45
 *
 ******************************************************************************/
func NewDemandLstServiceImplement() *DemandLstServiceImplement {
	return &DemandLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/DemandLstService.InsertOne
 *
 ******************************************************************************/
func (impl *DemandLstServiceImplement) InsertOne(user *models.Usertab, entity *model.DemandLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDemandLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.FileNr, entity.Pos, entity.TaskNr)
	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/DemandLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *DemandLstServiceImplement) DeleteOne(user *models.Usertab, fileNr int, pos int, taskNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/DemandLstService.SelectOne
 *
 ******************************************************************************/
func (impl *DemandLstServiceImplement) SelectOne(user *models.Usertab, fileNr int, pos int, taskNr int) (*model.DemandLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/DemandLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *DemandLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.DemandLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDemandLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.FileNr, entity.Pos, entity.TaskNr)
	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/DemandLstService.Insert
 *
 ******************************************************************************/
func (impl *DemandLstServiceImplement) Insert(user *models.Usertab, entities *[]model.DemandLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.FileNr, entity.Pos, entity.TaskNr)
		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/DemandLstService.Delete
 *
 ******************************************************************************/
func (impl *DemandLstServiceImplement) Delete(user *models.Usertab, entities *[]model.DemandLst) error {

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

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

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

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

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

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

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

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

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