// 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              : Operation的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfOperation = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Operation_OperationId.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OperationId, grmi.Equal, true),
		meta.Operation_PO.UrlParameterName:                 grmi.NewConditionItem(meta.Operation_PO, grmi.Equal, false),
		meta.Operation_StepNo.UrlParameterName:             grmi.NewConditionItem(meta.Operation_StepNo, grmi.Equal, false),
		meta.Operation_StepDesc.UrlParameterName:           grmi.NewConditionItem(meta.Operation_StepDesc, grmi.Equal, false),
		meta.Operation_ProjectId.UrlParameterName:          grmi.NewConditionItem(meta.Operation_ProjectId, grmi.Equal, false),
		meta.Operation_WorkLineId.UrlParameterName:          grmi.NewConditionItem(meta.Operation_WorkLineId, grmi.Equal, false),
		meta.Operation_ActivateInStation.UrlParameterName:  grmi.NewConditionItem(meta.Operation_ActivateInStation, grmi.Equal, false),
		meta.Operation_ActivateWhenRepair.UrlParameterName: grmi.NewConditionItem(meta.Operation_ActivateWhenRepair, grmi.Equal, false),
		meta.Operation_AttrCode.UrlParameterName:           grmi.NewConditionItem(meta.Operation_AttrCode, grmi.Equal, false),
		meta.Operation_AttrValue.UrlParameterName:          grmi.NewConditionItem(meta.Operation_AttrValue, grmi.Equal, false),
		meta.Operation_StepType.UrlParameterName:           grmi.NewConditionItem(meta.Operation_StepType, grmi.Equal, false),
		meta.Operation_OPCtrlPara1.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OPCtrlPara1, grmi.Equal, false),
		meta.Operation_OPCtrlPara2.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OPCtrlPara2, grmi.Equal, false),
		meta.Operation_OPCtrlPara3.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OPCtrlPara3, grmi.Equal, false),
		meta.Operation_OPCtrlPara4.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OPCtrlPara4, grmi.Equal, false),
		meta.Operation_OPCtrlPara5.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OPCtrlPara5, grmi.Equal, false),
		meta.Operation_OPCtrlPara6.UrlParameterName:        grmi.NewConditionItem(meta.Operation_OPCtrlPara6, grmi.Equal, false),
		meta.Operation_LastModify.UrlParameterName:         grmi.NewConditionItem(meta.Operation_LastModify, grmi.Approximate, false),
		meta.Operation_LastUser.UrlParameterName:           grmi.NewConditionItem(meta.Operation_LastUser, grmi.Equal, false),
		meta.Operation_CreateTime.UrlParameterName:         grmi.NewConditionItem(meta.Operation_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Operation的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfOperationAndPaging = grmi.NewCondition(
	DefaultConditionOfOperation.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OperationServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Operation的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
type OperationServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOperationDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OperationDAOImplement实例
 *
 * @Return Value             : OperationDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
func NewOperationServiceImplement() *OperationServiceImplement {
	return &OperationServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OperationService.InsertOne
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) InsertOne(user *models.Usertab, entity *model.Operation) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	attributeDao := dal.NewAttributeValstDAO(session, user.Pid, user.Userid)
	processDao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	stepDao := dal.NewStepTypeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	processLi, err := processDao.Select([]grmi.Predicate{meta.Process_PO.NewPredicate(grmi.Equal, entity.PO)}, nil)
	if err != nil {
		return err
	}
	if len(processLi) == 0 {
		return grmi.NewBusinessError("不存在对应的工序")
	}
	attributeLi, err := attributeDao.Select([]grmi.Predicate{meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, entity.AttrCode), meta.AttributeValst_AttrValue.NewPredicate(grmi.Equal, entity.AttrValue)}, nil)
	if err != nil {
		return err
	}
	if len(attributeLi) == 0 {
		return grmi.NewBusinessError("不存在对应的零件属性数据")
	}
	step, err := stepDao.SelectOne(entity.StepType)
	if err != nil {
		return err
	}
	if step == nil {
		return grmi.NewBusinessError("不存在对应的操作类型")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OperationService.DeleteOne
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) DeleteOne(user *models.Usertab, operationId int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	opcBasicDao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	opcComDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	opcDetailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	twDao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	twrDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	scanDao := dal.NewScanRuleInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(operationId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(operationId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = opcBasicDao.DeleteWhere([]grmi.Predicate{meta.OPCBasicComInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	 err = opcComDao.DeleteWhere([]grmi.Predicate{meta.OPCComRule_OperationId.NewPredicate(grmi.Equal, operationId)})
	 if err != nil {
	 	_ = session.Rollback()
	 	return err
	 }
	 err = opcDetailDao.DeleteWhere([]grmi.Predicate{meta.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, operationId)})
	 if err != nil {
	 	_ = session.Rollback()
	 	return err
	 }
	 err = twDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	 if err != nil {
	 	_ = session.Rollback()
	 	return err
	 }
	 err = twrDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = scanDao.DeleteWhere([]grmi.Predicate{meta.ScanRuleInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	 _ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OperationService.SelectOne
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) SelectOne(user *models.Usertab, operationId int) (*model.Operation, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OperationService.UpdateOne
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) UpdateOne(user *models.Usertab, entity *model.Operation) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	attributeDao := dal.NewAttributeValstDAO(session, user.Pid, user.Userid)
	processDao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	stepDao := dal.NewStepTypeDAO(session, user.Pid, user.Userid)
	opcBasicDao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	opcComDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	opcDetailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	twDao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	twrDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	scanDao := dal.NewScanRuleInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.PO != entity.PO {
		processLi, err := processDao.Select([]grmi.Predicate{meta.Process_PO.NewPredicate(grmi.Equal, entity.PO)}, nil)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if len(processLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的工序")
		}
	}
	if record.AttrCode != entity.AttrCode || record.AttrValue != record.AttrValue {
		attributeLi, err := attributeDao.Select([]grmi.Predicate{meta.AttributeValst_AttrCode.NewPredicate(grmi.Equal, entity.AttrCode), meta.AttributeValst_AttrValue.NewPredicate(grmi.Equal, entity.AttrValue)}, nil)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if len(attributeLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的零件属性数据")
		}
	}
	if record.StepType != entity.StepType {
		step, err := stepDao.SelectOne(entity.StepType)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if step == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的操作类型")
		}
	}
	if record.StepDesc != entity.StepDesc {
		basic := model.OPCBasicComInfo{StepDesc: entity.StepDesc}
		com := model.OPCComRule{StepDesc: entity.StepDesc}
		detail := model.OPCComDetailRule{StepDesc: entity.StepDesc}
		tw := model.TightenWrenchInfo{StepDesc: entity.StepDesc}
		twr := model.TightenWrenchRuleInfo{StepDesc: entity.StepDesc}
		scan := model.ScanRuleInfo{StepDesc: entity.StepDesc}
		err = opcBasicDao.UpdateWhere([]grmi.Predicate{meta.OPCBasicComInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)}, &basic, meta.OPCBasicComInfo_StepDesc.ColumnName)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		err = opcComDao.UpdateWhere([]grmi.Predicate{meta.OPCComRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)}, &com, meta.OPCComRule_StepDesc.ColumnName)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		err = opcDetailDao.UpdateWhere([]grmi.Predicate{meta.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)}, &detail, meta.OPCComRule_StepDesc.ColumnName)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		err = twDao.UpdateWhere([]grmi.Predicate{meta.TightenWrenchInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)}, &tw, meta.TightenWrenchInfo_StepDesc.ColumnName)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		err = twrDao.UpdateWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)}, &twr, meta.TightenWrenchRuleInfo_StepDesc.ColumnName)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		err = scanDao.UpdateWhere([]grmi.Predicate{meta.ScanRuleInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)}, &scan, meta.ScanRuleInfo_StepDesc.ColumnName)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OperationService.Insert
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) Insert(user *models.Usertab, entities *[]model.Operation) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOperationDAO(session, user.Pid, 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.Insert(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OperationService.Delete
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) Delete(user *models.Usertab, entities *[]model.Operation) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOperationDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/OperationService.Select
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfOperationAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOperationDAO(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/OperationService.Update
 *
 ******************************************************************************/
func (impl *OperationServiceImplement) Update(user *models.Usertab, entities *[]model.Operation) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOperationDAO(session, user.Pid, 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
}
