// 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              : HondaCalloffErrorLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-09 13:35:31
 *
 ******************************************************************************/
var DefaultConditionOfHondaCalloffErrorLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.HondaCalloffErrorLst_CalloffId.UrlParameterName:   grmi.NewConditionItem(meta.HondaCalloffErrorLst_CalloffId, grmi.Equal, true),
		meta.HondaCalloffErrorLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.HondaCalloffErrorLst_Pos, grmi.Equal, true),
		meta.HondaCalloffErrorLst_ErrorType.UrlParameterName:   grmi.NewConditionItem(meta.HondaCalloffErrorLst_ErrorType, grmi.Equal, false),
		meta.HondaCalloffErrorLst_ErrorInfo.UrlParameterName:   grmi.NewConditionItem(meta.HondaCalloffErrorLst_ErrorInfo, grmi.Equal, false),
		meta.HondaCalloffErrorLst_ErrorStatus.UrlParameterName: grmi.NewConditionItem(meta.HondaCalloffErrorLst_ErrorStatus, grmi.Equal, false),
		meta.HondaCalloffErrorLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.HondaCalloffErrorLst_LastModify, grmi.Approximate, false),
		meta.HondaCalloffErrorLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.HondaCalloffErrorLst_LastUser, grmi.Equal, false),
		meta.HondaCalloffErrorLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.HondaCalloffErrorLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : HondaCalloffErrorLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-09 13:35:31
 *
 ******************************************************************************/
var DefaultConditionOfHondaCalloffErrorLstAndPaging = grmi.NewCondition(
	DefaultConditionOfHondaCalloffErrorLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : HondaCalloffErrorLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : HondaCalloffErrorLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-09 13:35:31
 *
 ******************************************************************************/
type HondaCalloffErrorLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewHondaCalloffErrorLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个HondaCalloffErrorLstDAOImplement实例
 *
 * @Return Value             : HondaCalloffErrorLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-09 13:35:31
 *
 ******************************************************************************/
func NewHondaCalloffErrorLstServiceImplement() *HondaCalloffErrorLstServiceImplement {
	return &HondaCalloffErrorLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaCalloffErrorLstService.InsertOne
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) InsertOne(user *global.User, entity *model.HondaCalloffErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaCalloffErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CalloffId, 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/HondaCalloffErrorLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) DeleteOne(user *global.User, calloffId string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaCalloffErrorLstService.SelectOne
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) SelectOne(user *global.User, calloffId string, pos int) (*model.HondaCalloffErrorLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/HondaCalloffErrorLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) UpdateOne(user *global.User, entity *model.HondaCalloffErrorLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaCalloffErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CalloffId, 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/HondaCalloffErrorLstService.Insert
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) Insert(user *global.User, entities *[]model.HondaCalloffErrorLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CalloffId, 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/HondaCalloffErrorLstService.Delete
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) Delete(user *global.User, entities *[]model.HondaCalloffErrorLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CalloffId, 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/HondaCalloffErrorLstService.Select
 *
 ******************************************************************************/
func (impl *HondaCalloffErrorLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.HondaCalloffErrorLst, error) {

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

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

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

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

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

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

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

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

// RepairError 修复错误项数据
func (impl *HondaCalloffErrorLstServiceImplement) RepairError(user *global.User, calloffId string, pos int) error  {
	grmi.Log(user, "/services/pln/implments/HondaCalloffErrorLst.service.impl.go", "RepairError", "修复错误")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaCalloffErrorLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(calloffId, pos)
	if err != nil {
		return grmi.NewBusinessError("查询数据失败, error:" + err.Error())
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.ErrorStatus != model.ERROR_STATUS_ON {
		return grmi.NewBusinessError("该错误项状态不是开启, 请检查")
	}
	record.ErrorStatus = model.ERROR_STATUS_OFF
	err = dao.UpdateOne(record)
	if err != nil {
		return grmi.NewBusinessError("更新错误项数据失败, error:" + err.Error())
	}
	return nil
}


// RepairAllError 修复所有错误项数据
func (impl *HondaCalloffErrorLstServiceImplement) RepairAllError(user *global.User, calloffId string) error  {
	grmi.Log(user, "/services/pln/implments/HondaCalloffErrorLst.service.impl.go", "RepairAllError", "修复所有错误项数据")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewHondaCalloffErrorLstDAO(session, user.PlantNr, user.UserId)
	updateData := model.HondaCalloffErrorLst{
		ErrorStatus: model.ERROR_STATUS_OFF,
	}
	err := dao.UpdateWhere([]grmi.Predicate{meta.HondaCalloffErrorLst_CalloffId.NewPredicate(grmi.Equal, calloffId)}, &updateData, meta.HondaCalloffErrorLst_ErrorStatus.Name)
	if err != nil {
		return grmi.NewBusinessError("更新错误项状态失败, error:" + err.Error())
	}
	return nil
}