// 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              : GuideRuleInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
var DefaultConditionOfGuideRuleInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.GuideRuleInfo_OperationId.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_OperationId, grmi.Equal, true),
		meta.GuideRuleInfo_PO.UrlParameterName:          grmi.NewConditionItem(meta.GuideRuleInfo_PO, grmi.Equal, false),
		meta.GuideRuleInfo_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.GuideRuleInfo_StepNo, grmi.Equal, false),
		meta.GuideRuleInfo_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.GuideRuleInfo_ProjectId, grmi.Equal, false),
		meta.GuideRuleInfo_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.GuideRuleInfo_StepDesc, grmi.Equal, false),
		meta.GuideRuleInfo_DisplayTime.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_DisplayTime, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara1, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara2, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara3, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara4, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara5, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara6, grmi.Equal, false),
		meta.GuideRuleInfo_GRCtrlPara7.UrlParameterName: grmi.NewConditionItem(meta.GuideRuleInfo_GRCtrlPara7, grmi.Equal, false),
		meta.GuideRuleInfo_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.GuideRuleInfo_LastModify, grmi.Approximate, false),
		meta.GuideRuleInfo_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.GuideRuleInfo_LastUser, grmi.Equal, false),
		meta.GuideRuleInfo_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.GuideRuleInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : GuideRuleInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
var DefaultConditionOfGuideRuleInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfGuideRuleInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : GuideRuleInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : GuideRuleInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
type GuideRuleInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewGuideRuleInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个GuideRuleInfoDAOImplement实例
 *
 * @Return Value             : GuideRuleInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-23 11:13:02
 *
 ******************************************************************************/
func NewGuideRuleInfoServiceImplement() *GuideRuleInfoServiceImplement {
	return &GuideRuleInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/GuideRuleInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *GuideRuleInfoServiceImplement) InsertOne(user *global.User, entity *model.GuideRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGuideRuleInfoDAO(session, user.PlantNr, user.UserId)
	operationDao := dal.NewOperationDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	operation, err := operationDao.SelectOne(entity.OperationId)
	if err != nil {
		return grmi.NewBusinessError("查询工艺数据失败, error:" + err.Error())
	}
	if operation == nil {
		return grmi.NewBusinessError("不存在对应的工艺数据")
	}
	if operation.StepType != model.STEP_GUIDE_RULE {
		return grmi.NewBusinessError("绑定的工步不是指导操作")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/GuideRuleInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *GuideRuleInfoServiceImplement) DeleteOne(user *global.User, operationId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/GuideRuleInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *GuideRuleInfoServiceImplement) SelectOne(user *global.User, operationId int) (*model.GuideRuleInfo, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/GuideRuleInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *GuideRuleInfoServiceImplement) UpdateOne(user *global.User, entity *model.GuideRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewGuideRuleInfoDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId)
	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/base/GuideRuleInfoService.Insert
 *
 ******************************************************************************/
func (impl *GuideRuleInfoServiceImplement) Insert(user *global.User, entities *model.GuideRuleInfoData) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewGuideRuleInfoDAO(session, user.PlantNr, user.UserId)
	operationDao := dal.NewOperationDAO(session, user.PlantNr, user.UserId)
	operationId := entities.OperationId
	operation, err := operationDao.SelectOne(operationId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if operation == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工艺数据")
	}
	if operation.StepType != model.STEP_GUIDE_RULE {
		_ = session.Rollback()
		return grmi.NewBusinessError("工作不是指导操作")
	}
	li := entities.GuideRuleInfoLi
	if len(li) > 1 {
		_ = session.Rollback()
		return grmi.NewBusinessError("规则数据过多")
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.GuideRuleInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	for index, item := range li {
		item.OperationId = operationId
		item.PO = operation.PO
		item.StepNo = operation.StepNo
		item.ProjectId = operation.ProjectId
		item.StepDesc = operation.StepDesc
		li[index] = item
	}
	if len(li) != 0 {
		err = dao.Insert(&li)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/GuideRuleInfoService.Delete
 *
 ******************************************************************************/
func (impl *GuideRuleInfoServiceImplement) Delete(user *global.User, entities *[]model.GuideRuleInfo) error {

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

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

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

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

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

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

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

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

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

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

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