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

package implments

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

/******************************************************************************
 *
 * @Description              : WorkPlaceConfig的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-13 14:01:26
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlaceConfig = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkPlaceConfig_ConfigNr.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlaceConfig_ConfigNr, grmi.Equal, true),
		meta.WorkPlaceConfig_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.WorkPlaceConfig_WorkPlaceNr, grmi.Equal, false),
		meta.WorkPlaceConfig_ParamName.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_ParamName, grmi.Equal, false),
		meta.WorkPlaceConfig_StrValue1.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_StrValue1, grmi.Equal, false),
		meta.WorkPlaceConfig_StrValue2.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_StrValue2, grmi.Equal, false),
		meta.WorkPlaceConfig_StrValue3.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_StrValue3, grmi.Equal, false),
		meta.WorkPlaceConfig_IntValue1.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_IntValue1, grmi.Equal, false),
		meta.WorkPlaceConfig_IntValue2.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_IntValue2, grmi.Equal, false),
		meta.WorkPlaceConfig_IntValue3.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_IntValue3, grmi.Equal, false),
		meta.WorkPlaceConfig_FloatValue1.UrlParameterName: grmi.NewConditionItem(meta.WorkPlaceConfig_FloatValue1, grmi.Equal, false),
		meta.WorkPlaceConfig_FloatValue2.UrlParameterName: grmi.NewConditionItem(meta.WorkPlaceConfig_FloatValue2, grmi.Equal, false),
		meta.WorkPlaceConfig_FloatValue3.UrlParameterName: grmi.NewConditionItem(meta.WorkPlaceConfig_FloatValue3, grmi.Equal, false),
		meta.WorkPlaceConfig_StrValue4.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_StrValue4, grmi.Equal, false),
		meta.WorkPlaceConfig_StrValue5.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_StrValue5, grmi.Equal, false),
		meta.WorkPlaceConfig_IntValue4.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_IntValue4, grmi.Equal, false),
		meta.WorkPlaceConfig_IntValue5.UrlParameterName:   grmi.NewConditionItem(meta.WorkPlaceConfig_IntValue5, grmi.Equal, false),
		meta.WorkPlaceConfig_BoolValue1.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlaceConfig_BoolValue1, grmi.Equal, false),
		meta.WorkPlaceConfig_BoolValue2.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlaceConfig_BoolValue2, grmi.Equal, false),
		meta.WorkPlaceConfig_BoolValue3.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlaceConfig_BoolValue3, grmi.Equal, false),
		meta.WorkPlaceConfig_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlaceConfig_LastModify, grmi.Approximate, false),
		meta.WorkPlaceConfig_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WorkPlaceConfig_LastUser, grmi.Equal, false),
		meta.WorkPlaceConfig_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WorkPlaceConfig_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkPlaceConfig的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-13 14:01:26
 *
 ******************************************************************************/
var DefaultConditionOfWorkPlaceConfigAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkPlaceConfig.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkPlaceConfigServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkPlaceConfig的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-13 14:01:26
 *
 ******************************************************************************/
type WorkPlaceConfigServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkPlaceConfigDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkPlaceConfigDAOImplement实例
 *
 * @Return Value             : WorkPlaceConfigDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-01-13 14:01:26
 *
 ******************************************************************************/
func NewWorkPlaceConfigServiceImplement() *WorkPlaceConfigServiceImplement {
	return &WorkPlaceConfigServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceConfigService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkPlaceConfigServiceImplement) InsertOne(user *global.User, entity *model.WorkPlaceConfig) error {

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "InsertOneWorkPlaceConfig", "插入一个WorkPlaceConfig")
	if entity.WorkPlaceNr == 0 || entity.ParamName == "" {
		return grmi.NewBusinessError("配置参数不完整")
	}
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceConfigDAO(session, user.PlantNr, user.UserId)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		return grmi.NewBusinessError("查询工位失败, error:" + err.Error())
	}
	if workPlace == nil {
		return grmi.NewBusinessError("工位不存在")
	}
	existLi, err := dao.Select([]grmi.Predicate{meta.WorkPlaceConfig_ParamName.NewPredicate(grmi.Equal, entity.ParamName), meta.WorkPlaceConfig_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)}, nil)
	if err != nil {
		return grmi.NewBusinessError("查询已存在数据失败, error:" + err.Error())
	}
	if len(existLi) != 0 {
		return grmi.NewBusinessError("该工位已存在该参数名的配置")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceConfigService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkPlaceConfigServiceImplement) DeleteOne(user *global.User, configNr int) error {

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "DeleteOneWorkPlaceConfig", "删除一个WorkPlaceConfig")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceConfigService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkPlaceConfigServiceImplement) SelectOne(user *global.User, configNr int) (*model.WorkPlaceConfig, error) {

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "SelectOneWorkPlaceConfig", "查询一个WorkPlaceConfig")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceConfigService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkPlaceConfigServiceImplement) UpdateOne(user *global.User, entity *model.WorkPlaceConfig) error {

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "UpdateOneWorkPlaceConfig", "修改一个WorkPlaceConfig")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkPlaceConfigDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.ConfigNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if entity.WorkPlaceNr != record.WorkPlaceNr {
		return grmi.NewBusinessError("工位不允许修改")
	}
	if entity.ParamName != record.ParamName {
		existLi, err := dao.Select([]grmi.Predicate{meta.WorkPlaceConfig_ParamName.NewPredicate(grmi.Equal, entity.ParamName), meta.WorkPlaceConfig_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)}, nil)
		if err != nil {
			return grmi.NewBusinessError("查询已存在数据失败, error:" + err.Error())
		}
		if len(existLi) != 0 {
			return grmi.NewBusinessError("该工位已存在该参数名的配置")
		}
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkPlaceConfigService.Insert
 *
 ******************************************************************************/
func (impl *WorkPlaceConfigServiceImplement) Insert(user *global.User, entities *[]model.WorkPlaceConfig) error {

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "InsertWorkPlaceConfig", "插入多个WorkPlaceConfig")

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

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

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "DeleteWorkPlaceConfig", "删除多个WorkPlaceConfig")

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

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

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "SelectWorkPlaceConfig", "查询WorkPlaceConfig")

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

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "SelectWorkPlaceConfig", "查询WorkPlaceConfig")

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

	grmi.Log(user, "/services/base/implments/WorkPlaceConfig.service.impl.go", "UpdateWorkPlaceConfig", "修改多个WorkPlaceConfig")

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

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

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