// 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              : HondaDemandErrorLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-20 09:58:33
 *
 ******************************************************************************/
var DefaultConditionOfHondaDemandErrorLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.HondaDemandErrorLst_DemandId.UrlParameterName:    grmi.NewConditionItem(meta.HondaDemandErrorLst_DemandId, grmi.Equal, true),
		meta.HondaDemandErrorLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.HondaDemandErrorLst_Pos, grmi.Equal, true),
		meta.HondaDemandErrorLst_ErrorType.UrlParameterName:   grmi.NewConditionItem(meta.HondaDemandErrorLst_ErrorType, grmi.Equal, false),
		meta.HondaDemandErrorLst_ErrorInfo.UrlParameterName:   grmi.NewConditionItem(meta.HondaDemandErrorLst_ErrorInfo, grmi.Equal, false),
		meta.HondaDemandErrorLst_ErrorStatus.UrlParameterName: grmi.NewConditionItem(meta.HondaDemandErrorLst_ErrorStatus, grmi.Equal, false),
		meta.HondaDemandErrorLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.HondaDemandErrorLst_LastModify, grmi.Approximate, false),
		meta.HondaDemandErrorLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.HondaDemandErrorLst_LastUser, grmi.Equal, false),
		meta.HondaDemandErrorLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.HondaDemandErrorLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandErrorLstService.InsertOne
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) InsertOne(user *global.User, entity *model.HondaDemandErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) DeleteOne(user *global.User, demandId string, pos int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.SelectOne
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) SelectOne(user *global.User, demandId string, pos int) (*model.HondaDemandErrorLst, error) {

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

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

func (impl *HondaDemandErrorLstServiceImplement) Repair(user *global.User, demandId string, pos int) error {
	grmi.Log(user, "/services/pln/implments/HondaDemandErrorLst.service.impl.go", "Repair", "修复一个HondaDemandErrorLst")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(demandId, pos)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	record.ErrorStatus = "关"
	err = dao.UpdateOne(record)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaDemandErrorLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) UpdateOne(user *global.User, entity *model.HondaDemandErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.Insert
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) Insert(user *global.User, entities *[]model.HondaDemandErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.Delete
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) Delete(user *global.User, entities *[]model.HondaDemandErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.Select
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.HondaDemandErrorLst, error) {

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

	condition := DefaultConditionOfHondaDemandErrorLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfHondaDemandErrorLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewHondaDemandErrorLstDAO(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/HondaDemandErrorLstService.Update
 *
 ******************************************************************************/
func (impl *HondaDemandErrorLstServiceImplement) Update(user *global.User, entities *[]model.HondaDemandErrorLst) error {

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

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