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

package implments

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

/******************************************************************************
 *
 * @Description              : HondaDemandItemLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-20 09:58:33
 *
 ******************************************************************************/
var DefaultConditionOfHondaDemandItemLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.HondaDemandItemLst_DemandId.UrlParameterName:        grmi.NewConditionItem(meta.HondaDemandItemLst_DemandId, grmi.Equal, true),
		meta.HondaDemandItemLst_Pos.UrlParameterName:             grmi.NewConditionItem(meta.HondaDemandItemLst_Pos, grmi.Equal, true),
		meta.HondaDemandItemLst_DeliveryDate.UrlParameterName:    grmi.NewConditionItem(meta.HondaDemandItemLst_DeliveryDate, grmi.Approximate, false),
		meta.HondaDemandItemLst_SeqNr.UrlParameterName:           grmi.NewConditionItem(meta.HondaDemandItemLst_SeqNr, grmi.Equal, false),
		meta.HondaDemandItemLst_VinPrefix.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_VinPrefix, grmi.Equal, false),
		meta.HondaDemandItemLst_VinNrFrom.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_VinNrFrom, grmi.Equal, false),
		meta.HondaDemandItemLst_VinNrTo.UrlParameterName:         grmi.NewConditionItem(meta.HondaDemandItemLst_VinNrTo, grmi.Equal, false),
		meta.HondaDemandItemLst_ConfigValue.UrlParameterName:     grmi.NewConditionItem(meta.HondaDemandItemLst_ConfigValue, grmi.Equal, false),
		meta.HondaDemandItemLst_ModelValue.UrlParameterName:      grmi.NewConditionItem(meta.HondaDemandItemLst_ModelValue, grmi.Equal, false),
		meta.HondaDemandItemLst_ColorValue.UrlParameterName:      grmi.NewConditionItem(meta.HondaDemandItemLst_ColorValue, grmi.Equal, false),
		meta.HondaDemandItemLst_ProductFamilyId.UrlParameterName: grmi.NewConditionItem(meta.HondaDemandItemLst_ProductFamilyId, grmi.Equal, false),
		meta.HondaDemandItemLst_PlanQty.UrlParameterName:         grmi.NewConditionItem(meta.HondaDemandItemLst_PlanQty, grmi.Equal, false),
		meta.HondaDemandItemLst_Color.UrlParameterName:           grmi.NewConditionItem(meta.HondaDemandItemLst_Color, grmi.Equal, false),
		meta.HondaDemandItemLst_Grade.UrlParameterName:           grmi.NewConditionItem(meta.HondaDemandItemLst_Grade, grmi.Equal, false),
		meta.HondaDemandItemLst_OrderType.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_OrderType, grmi.Equal, false),
		meta.HondaDemandItemLst_ProductType.UrlParameterName:     grmi.NewConditionItem(meta.HondaDemandItemLst_ProductType, grmi.Equal, false),
		meta.HondaDemandItemLst_CtrlPara1.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_CtrlPara1, grmi.Equal, false),
		meta.HondaDemandItemLst_CtrlPara2.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_CtrlPara2, grmi.Equal, false),
		meta.HondaDemandItemLst_CtrlStr1.UrlParameterName:        grmi.NewConditionItem(meta.HondaDemandItemLst_CtrlStr1, grmi.Equal, false),
		meta.HondaDemandItemLst_CtrlStr2.UrlParameterName:        grmi.NewConditionItem(meta.HondaDemandItemLst_CtrlStr2, grmi.Equal, false),
		meta.HondaDemandItemLst_CtrlTime1.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_CtrlTime1, grmi.Approximate, false),
		meta.HondaDemandItemLst_CtrlTime2.UrlParameterName:       grmi.NewConditionItem(meta.HondaDemandItemLst_CtrlTime2, grmi.Approximate, false),
		meta.HondaDemandItemLst_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.HondaDemandItemLst_LastModify, grmi.Approximate, false),
		meta.HondaDemandItemLst_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.HondaDemandItemLst_LastUser, grmi.Equal, false),
		meta.HondaDemandItemLst_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.HondaDemandItemLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : HondaDemandItemLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-20 09:58:33
 *
 ******************************************************************************/
var DefaultConditionOfHondaDemandItemLstAndPaging = grmi.NewCondition(
	DefaultConditionOfHondaDemandItemLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : HondaDemandItemLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : HondaDemandItemLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-20 09:58:33
 *
 ******************************************************************************/
type HondaDemandItemLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewHondaDemandItemLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个HondaDemandItemLstDAOImplement实例
 *
 * @Return Value             : HondaDemandItemLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-20 09:58:33
 *
 ******************************************************************************/
func NewHondaDemandItemLstServiceImplement() *HondaDemandItemLstServiceImplement {
	return &HondaDemandItemLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.InsertOne
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) InsertOne(user *global.User, entity *model.HondaDemandItemLst) error {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "InsertOneHondaDemandItemLst", "插入一个HondaDemandItemLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandItemLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DemandId, 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_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) DeleteOne(user *global.User, demandId string, pos int) error {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "DeleteOneHondaDemandItemLst", "删除一个HondaDemandItemLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.SelectOne
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) SelectOne(user *global.User, demandId string, pos int) (*model.HondaDemandItemLst, error) {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "SelectOneHondaDemandItemLst", "查询一个HondaDemandItemLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) UpdateOne(user *global.User, entity *model.HondaDemandItemLst) error {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "UpdateOneHondaDemandItemLst", "修改一个HondaDemandItemLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandItemLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DemandId, 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_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.Insert
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) Insert(user *global.User, entities *[]model.HondaDemandItemLst) error {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "InsertHondaDemandItemLst", "插入多个HondaDemandItemLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, 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_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.Delete
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) Delete(user *global.User, entities *[]model.HondaDemandItemLst) error {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "DeleteHondaDemandItemLst", "删除多个HondaDemandItemLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, 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_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.Select
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.HondaDemandItemLst, error) {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "SelectHondaDemandItemLst", "查询HondaDemandItemLst")

	condition := DefaultConditionOfHondaDemandItemLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewHondaDemandItemLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "SelectHondaDemandItemLst", "查询HondaDemandItemLst")

	condition := DefaultConditionOfHondaDemandItemLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewHondaDemandItemLstDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandItemLstService.Update
 *
 ******************************************************************************/
func (impl *HondaDemandItemLstServiceImplement) Update(user *global.User, entities *[]model.HondaDemandItemLst) error {

	grmi.Log(user, "/services/pln/implments/HondaDemandItemLst.service.impl.go", "UpdateHondaDemandItemLst", "修改多个HondaDemandItemLst")

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

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

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