// 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              : ToyotaTask的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-07-12 13:58:15
 *
 ******************************************************************************/
var DefaultConditionOfToyotaTask = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ToyotaTask_TaskId.UrlParameterName:       grmi.NewConditionItem(meta.ToyotaTask_TaskId, grmi.Equal, true),
		meta.ToyotaTask_TaskName.UrlParameterName:     grmi.NewConditionItem(meta.ToyotaTask_TaskName, grmi.Equal, false),
		meta.ToyotaTask_Status.UrlParameterName:       grmi.NewConditionItem(meta.ToyotaTask_Status, grmi.Equal, false),
		meta.ToyotaTask_StrCtrl1.UrlParameterName:     grmi.NewConditionItem(meta.ToyotaTask_StrCtrl1, grmi.Equal, false),
		meta.ToyotaTask_StrCtrl2.UrlParameterName:     grmi.NewConditionItem(meta.ToyotaTask_StrCtrl2, grmi.Equal, false),
		meta.ToyotaTask_IntCtrl1.UrlParameterName:     grmi.NewConditionItem(meta.ToyotaTask_IntCtrl1, grmi.Equal, false),
		meta.ToyotaTask_IntCtrl2.UrlParameterName:     grmi.NewConditionItem(meta.ToyotaTask_IntCtrl2, grmi.Equal, false),
		meta.ToyotaTask_FloatCtrl1.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaTask_FloatCtrl1, grmi.Equal, false),
		meta.ToyotaTask_FloatCtrl2.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaTask_FloatCtrl2, grmi.Equal, false),
		meta.ToyotaTask_LastTaskTime.UrlParameterName: grmi.NewConditionItem(meta.ToyotaTask_LastTaskTime, grmi.Approximate, false),
		meta.ToyotaTask_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaTask_LastModify, grmi.Approximate, false),
		meta.ToyotaTask_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.ToyotaTask_LastUser, grmi.Equal, false),
		meta.ToyotaTask_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.ToyotaTask_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ToyotaTask的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-07-12 13:58:15
 *
 ******************************************************************************/
var DefaultConditionOfToyotaTaskAndPaging = grmi.NewCondition(
	DefaultConditionOfToyotaTask.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ToyotaTaskServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ToyotaTask的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-07-12 13:58:15
 *
 ******************************************************************************/
type ToyotaTaskServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewToyotaTaskDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ToyotaTaskDAOImplement实例
 *
 * @Return Value             : ToyotaTaskDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-07-12 13:58:15
 *
 ******************************************************************************/
func NewToyotaTaskServiceImplement() *ToyotaTaskServiceImplement {
	return &ToyotaTaskServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaTaskService.InsertOne
 *
 ******************************************************************************/
func (impl *ToyotaTaskServiceImplement) InsertOne(user *global.User, entity *model.ToyotaTask) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewToyotaTaskDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.TaskId)
	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/ToyotaTaskService.DeleteOne
 *
 ******************************************************************************/
func (impl *ToyotaTaskServiceImplement) DeleteOne(user *global.User, taskId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaTaskService.SelectOne
 *
 ******************************************************************************/
func (impl *ToyotaTaskServiceImplement) SelectOne(user *global.User, taskId string) (*model.ToyotaTask, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/ToyotaTaskService.UpdateOne
 *
 ******************************************************************************/
func (impl *ToyotaTaskServiceImplement) UpdateOne(user *global.User, entity *model.ToyotaTask) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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