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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : OPCComDetailRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
var DefaultConditionOfOPCComDetailRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCComDetailRule_OperationId.UrlParameterName: grmi.NewConditionItem(meta.OPCComDetailRule_OperationId, grmi.Equal, true),
		meta.OPCComDetailRule_RuleId.UrlParameterName:      grmi.NewConditionItem(meta.OPCComDetailRule_RuleId, grmi.Equal, true),
		meta.OPCComDetailRule_DetailId.UrlParameterName:    grmi.NewConditionItem(meta.OPCComDetailRule_DetailId, grmi.Equal, true),
		meta.OPCComDetailRule_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.OPCComDetailRule_WorkPlaceNr, grmi.Equal, true),
		meta.OPCComDetailRule_PO.UrlParameterName:          grmi.NewConditionItem(meta.OPCComDetailRule_PO, grmi.Equal, false),
		meta.OPCComDetailRule_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.OPCComDetailRule_StepNo, grmi.Equal, false),
		meta.OPCComDetailRule_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCComDetailRule_StepDesc, grmi.Equal, false),
		meta.OPCComDetailRule_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.OPCComDetailRule_ProjectId, grmi.Equal, false),
		meta.OPCComDetailRule_BaseOnType.UrlParameterName:  grmi.NewConditionItem(meta.OPCComDetailRule_BaseOnType, grmi.Equal, false),
		meta.OPCComDetailRule_BaseOnValue.UrlParameterName: grmi.NewConditionItem(meta.OPCComDetailRule_BaseOnValue, grmi.Equal, false),
		meta.OPCComDetailRule_DataType.UrlParameterName:    grmi.NewConditionItem(meta.OPCComDetailRule_DataType, grmi.Equal, false),
		meta.OPCComDetailRule_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.OPCComDetailRule_LastModify, grmi.Approximate, false),
		meta.OPCComDetailRule_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.OPCComDetailRule_LastUser, grmi.Equal, false),
		meta.OPCComDetailRule_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.OPCComDetailRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCComDetailRule的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
var DefaultConditionOfOPCComDetailRuleAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCComDetailRule.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCComDetailRuleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCComDetailRule的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
type OPCComDetailRuleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCComDetailRuleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCComDetailRuleDAOImplement实例
 *
 * @Return Value             : OPCComDetailRuleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
func NewOPCComDetailRuleServiceImplement() *OPCComDetailRuleServiceImplement {
	return &OPCComDetailRuleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) InsertOne(user *models.Usertab, entity *model.OPCComDetailRule) error {

	grmi.Log(user, "/services/base/implments/OPCComDetailRule.service.impl.go", "InsertOneOPCComDetailRule", "插入一个OPCComDetailRule")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	//ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.RuleId, entity.DetailId, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	//rule, err := ruleDao.SelectOne(entity.PO, entity.StepNo, entity.RuleId)
	//if err != nil {
	//	return err
	//}
	//if rule == nil {
	//	return grmi.NewBusinessError("不存在赢得OPC通信规则")
	//}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) DeleteOne(user *models.Usertab, operationId int, ruleId int, detailId int, workPlaceNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(operationId, ruleId, detailId, workPlaceNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(operationId, ruleId, detailId, workPlaceNr)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) SelectOne(user *models.Usertab, operationId int, ruleId int, detailId int, workPlaceNr int) (*model.OPCComDetailRule, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(operationId, ruleId, detailId, workPlaceNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) UpdateOne(user *models.Usertab, entity *model.OPCComDetailRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.RuleId, entity.DetailId, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.Insert
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) Insert(user *models.Usertab, entities *[]model.OPCComDetailRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.RuleId, entity.DetailId, entity.WorkPlaceNr)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.Delete
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) Delete(user *models.Usertab, entities *[]model.OPCComDetailRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.Select
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfOPCComDetailRuleAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComDetailRuleService.Update
 *
 ******************************************************************************/
func (impl *OPCComDetailRuleServiceImplement) Update(user *models.Usertab, entities *[]model.OPCComDetailRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)

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

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