// 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              : TightenWrenchInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
var DefaultConditionOfTightenWrenchInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.TightenWrenchInfo_OperationId.UrlParameterName: grmi.NewConditionItem(meta.TightenWrenchInfo_OperationId, grmi.Equal, true),
		meta.TightenWrenchInfo_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.TightenWrenchInfo_WorkPlaceNr, grmi.Equal, true),
		meta.TightenWrenchInfo_PO.UrlParameterName:          grmi.NewConditionItem(meta.TightenWrenchInfo_PO, grmi.Equal, false),
		meta.TightenWrenchInfo_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.TightenWrenchInfo_StepNo, grmi.Equal, false),
		meta.TightenWrenchInfo_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.TightenWrenchInfo_StepDesc, grmi.Equal, false),
		meta.TightenWrenchInfo_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.TightenWrenchInfo_ProjectId, grmi.Equal, false),
		meta.TightenWrenchInfo_BrandName.UrlParameterName:   grmi.NewConditionItem(meta.TightenWrenchInfo_BrandName, grmi.Equal, false),
		meta.TightenWrenchInfo_MachineType.UrlParameterName: grmi.NewConditionItem(meta.TightenWrenchInfo_MachineType, grmi.Equal, false),
		meta.TightenWrenchInfo_IpAddress.UrlParameterName:   grmi.NewConditionItem(meta.TightenWrenchInfo_IpAddress, grmi.Equal, false),
		meta.TightenWrenchInfo_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.TightenWrenchInfo_LastModify, grmi.Approximate, false),
		meta.TightenWrenchInfo_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.TightenWrenchInfo_LastUser, grmi.Equal, false),
		meta.TightenWrenchInfo_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.TightenWrenchInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TightenWrenchInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) InsertOne(user *models.Usertab, entity *model.TightenWrenchInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	opDao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	operation, err := opDao.SelectOne(entity.OperationId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if operation == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工艺")
	}
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workPlace == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工位")
	}
	if operation.PO != entity.PO || operation.StepNo != entity.StepNo || operation.StepDesc != entity.StepDesc || operation.ProjectId != entity.ProjectId {
		_ = session.Rollback()
		return grmi.NewBusinessError("工艺数据错误")
	}
	ruleLi := entity.TightenWrenchRuleInfoLi
	err = dao.InsertOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if len(ruleLi) != 0 {
		for index, rule := range ruleLi {
			if rule.MainControlAction != model.CONTROL_ACTION_PSET && rule.MainControlAction != model.CONTROL_ACTION_JOB {
				if rule.ControlId != 1 || rule.ControlTime != 1 {
					_ = session.Rollback()
					return grmi.NewBusinessError("规则明细数据不合规")
				}
			}
			if rule.ControlId >= 10 {
				_ = session.Rollback()
				return grmi.NewBusinessError("规则明细数据不合规")
			}
			rule.PO = entity.PO
			rule.StepNo = entity.StepNo
			rule.StepDesc = entity.StepDesc
			rule.OperationId = entity.OperationId
			rule.ProjectId = entity.ProjectId
			rule.WorkPlaceNr = entity.WorkPlaceNr
			ruleLi[index] = rule
		}
		err = ruleDao.Insert(&ruleLi)
		if err != nil {
			_ = session.Rollback()
		}
	}
	_ = session.Commit()
	return nil
}

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(operationId, workPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(operationId, workPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TightenWrenchInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) SelectOne(user *models.Usertab, operationId int, workPlaceNr int) (*model.TightenWrenchInfo, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(operationId, workPlaceNr)
	if err != nil {
		return nil, err
	}
	ruleLi, err := ruleDao.Select([]grmi.Predicate{meta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, operationId)}, nil)
	if err != nil {
		return nil, err
	}
	result.TightenWrenchRuleInfoLi = ruleLi
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TightenWrenchInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) UpdateOne(user *models.Usertab, entity *model.TightenWrenchInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	opDao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if record.PO != entity.PO || record.StepNo != entity.StepNo {
		operation, err := opDao.SelectOne(entity.OperationId)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if operation == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的工艺")
		}
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	ruleLi := entity.TightenWrenchRuleInfoLi
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if len(ruleLi) != 0 {
		for index, rule := range ruleLi {
			if rule.MainControlAction != model.CONTROL_ACTION_PSET && rule.MainControlAction != model.CONTROL_ACTION_JOB {
				if rule.ControlId != 1 || rule.ControlTime != 1 {
					_ = session.Rollback()
					return grmi.NewBusinessError("规则明细数据不合规")
				}
			}
			if rule.ControlId >= 10 {
				_ = session.Rollback()
				return grmi.NewBusinessError("规则明细数据不合规")
			}
			rule.PO = entity.PO
			rule.StepNo = entity.StepNo
			rule.StepDesc = entity.StepDesc
			rule.OperationId = entity.OperationId
			ruleLi[index] = rule
		}
		err = ruleDao.Insert(&ruleLi)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TightenWrenchInfoService.Insert
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) Insert(user *models.Usertab, entities *[]model.TightenWrenchInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, 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/TightenWrenchInfoService.Delete
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) Delete(user *models.Usertab, entities *[]model.TightenWrenchInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, 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/TightenWrenchInfoService.Select
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/TightenWrenchInfoService.Save
 *
 ******************************************************************************/
func (impl *TightenWrenchInfoServiceImplement) Save(user *models.Usertab, entity *model.TightenWrenchInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewTightenWrenchInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewTightenWrenchRuleInfoDAO(session, user.Pid, user.Userid)
	opDao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workPlace == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工位")
	}

	operation, err := opDao.SelectOne(entity.OperationId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if operation == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工艺")
	}
	if operation.PO != entity.PO || operation.StepNo != entity.StepNo || operation.StepDesc != entity.StepDesc || operation.ProjectId != entity.ProjectId {
		_ = session.Rollback()
		return grmi.NewBusinessError("工艺数据错误")
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	ruleLi := entity.TightenWrenchRuleInfoLi
	if len(ruleLi) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("未获取到开发协议详细规则")
	}
	for index, rule := range ruleLi {
		if rule.MainControlAction != model.CONTROL_ACTION_PSET && rule.MainControlAction != model.CONTROL_ACTION_JOB {
			if rule.ControlId != 1 || rule.ControlTime != 1 {
				_ = session.Rollback()
				return grmi.NewBusinessError("规则明细数据不合规")
			}
		}
		if rule.ControlId >= 10 {
			_ = session.Rollback()
			return grmi.NewBusinessError("规则明细数据不合规")
		}
		rule.PO = entity.PO
		rule.StepNo = entity.StepNo
		rule.StepDesc = entity.StepDesc
		rule.OperationId = entity.OperationId
		rule.WorkPlaceNr = entity.WorkPlaceNr
		ruleLi[index] = rule
	}
	err = ruleDao.Insert(&ruleLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}
