// 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              : ScanRuleInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-11 10:00:53
 *
 ******************************************************************************/
var DefaultConditionOfScanRuleInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ScanRuleInfo_OperationId.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_OperationId, grmi.Equal, true),
		meta.ScanRuleInfo_PO.UrlParameterName:          grmi.NewConditionItem(meta.ScanRuleInfo_PO, grmi.Equal, false),
		meta.ScanRuleInfo_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.ScanRuleInfo_StepNo, grmi.Equal, false),
		meta.ScanRuleInfo_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.ScanRuleInfo_StepDesc, grmi.Equal, false),
		meta.ScanRuleInfo_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.ScanRuleInfo_ProjectId, grmi.Equal, false),
		meta.ScanRuleInfo_RuleType.UrlParameterName:    grmi.NewConditionItem(meta.ScanRuleInfo_RuleType, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara1.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara1, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara2.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara2, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara3.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara3, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara4.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara4, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara5.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara5, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara6.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara6, grmi.Equal, false),
		meta.ScanRuleInfo_SRCtrlPara7.UrlParameterName: grmi.NewConditionItem(meta.ScanRuleInfo_SRCtrlPara7, grmi.Equal, false),
		meta.ScanRuleInfo_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ScanRuleInfo_LastModify, grmi.Approximate, false),
		meta.ScanRuleInfo_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ScanRuleInfo_LastUser, grmi.Equal, false),
		meta.ScanRuleInfo_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ScanRuleInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScanRuleInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *ScanRuleInfoServiceImplement) InsertOne(user *models.Usertab, entity *model.ScanRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScanRuleInfoDAO(session, user.Pid, user.Userid)
	operationDao := dal.NewOperationDAO(session, user.Pid, 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 err
	}
	if operation == nil {
		return grmi.NewBusinessError("不存在对应的工艺数据")
	}
	if operation.StepType != model.STEP_CHECK_RULE {
		return grmi.NewBusinessError("工艺操作类型错误")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScanRuleInfoDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/ScanRuleInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *ScanRuleInfoServiceImplement) SelectOne(user *models.Usertab, operationId int) (*model.ScanRuleInfo, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScanRuleInfoDAO(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/ScanRuleInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *ScanRuleInfoServiceImplement) UpdateOne(user *models.Usertab, entity *model.ScanRuleInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScanRuleInfoDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/ScanRuleInfoService.Insert
 *
 ******************************************************************************/
func (impl *ScanRuleInfoServiceImplement) Insert(user *models.Usertab, entities *model.ScanRuleInfoData) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewScanRuleInfoDAO(session, user.Pid, user.Userid)
	operationDao := dal.NewOperationDAO(session, user.Pid, 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_CHECK_RULE {
		return grmi.NewBusinessError("工艺操作类型错误")
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.ScanRuleInfo_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	li := entities.ScanRuleInfoLi
	if len(li) > 1 {
		_ = session.Rollback()
		return grmi.NewBusinessError("创建规则超过1条")
	}
	for index, item := range li {
		item.OperationId = operationId
		item.PO = operation.PO
		item.StepNo = operation.StepNo
		item.ProjectId = operation.ProjectId
		item.StepDesc = operation.StepDesc
		if item.RuleType == model.SCAN_RULE_TYPE_OTHER {
			item.SRCtrlPara1 = model.SCAN_RULE_SERIALORDER
		}
		li[index] = item
	}
	if len(li) != 0 {
		err = dao.Insert(&li)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ScanRuleInfoService.Delete
 *
 ******************************************************************************/
func (impl *ScanRuleInfoServiceImplement) Delete(user *models.Usertab, entities *[]model.ScanRuleInfo) error {

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

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

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

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

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

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