// 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              : OPCMonitorBasicComInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorBasicComInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCMonitorBasicComInfo_WorkPlaceNr.UrlParameterName:                grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_WorkPlaceNr, grmi.Equal, true),
		meta.OPCMonitorBasicComInfo_MonitorPalletInPlace.UrlParameterName:       grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_MonitorPalletInPlace, grmi.Equal, true),
		meta.OPCMonitorBasicComInfo_MonitorManualRelease.UrlParameterName:       grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_MonitorManualRelease, grmi.Equal, true),
		meta.OPCMonitorBasicComInfo_MonitorProblemPieceRelease.UrlParameterName: grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_MonitorProblemPieceRelease, grmi.Equal, true),
		meta.OPCMonitorBasicComInfo_BrandName.UrlParameterName:                  grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_BrandName, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_MachineType.UrlParameterName:                grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_MachineType, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_IpAddress.UrlParameterName:                  grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_IpAddress, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_LastModify.UrlParameterName:                 grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_LastModify, grmi.Approximate, false),
		meta.OPCMonitorBasicComInfo_LastUser.UrlParameterName:                   grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_LastUser, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_CreateTime.UrlParameterName:                 grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCMonitorBasicComInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorBasicComInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCMonitorBasicComInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCMonitorBasicComInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCMonitorBasicComInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
type OPCMonitorBasicComInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCMonitorBasicComInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCMonitorBasicComInfoDAOImplement实例
 *
 * @Return Value             : OPCMonitorBasicComInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
func NewOPCMonitorBasicComInfoServiceImplement() *OPCMonitorBasicComInfoServiceImplement {
	return &OPCMonitorBasicComInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) InsertOne(user *models.Usertab, entity *model.OPCMonitorBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) DeleteOne(user *models.Usertab, workPlaceNr int, monitorPalletInPlace int, monitorManualRelease int, monitorProblemPieceRelease int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) SelectOne(user *models.Usertab, workPlaceNr int, monitorPalletInPlace int, monitorManualRelease int, monitorProblemPieceRelease int) (*model.OPCMonitorBasicComInfo, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) UpdateOne(user *models.Usertab, entity *model.OPCMonitorBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease)
	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/OPCMonitorBasicComInfoService.Insert
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Insert(user *models.Usertab, entities *[]model.OPCMonitorBasicComInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease)
		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/OPCMonitorBasicComInfoService.Delete
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Delete(user *models.Usertab, entities *[]model.OPCMonitorBasicComInfo) error {

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

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

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

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

	condition := DefaultConditionOfOPCMonitorBasicComInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.Pid, user.Userid)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		li := result.Records.([]model.OPCMonitorBasicComInfo)
		for index, opc := range li {
			ruleLi, err := ruleDao.Select([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr)}, []grmi.Field{meta.OPCMonitorComRule_RuleId})
			if err != nil {
				return nil, err
			}
			for index, rule := range ruleLi {
				if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
					detailLi, err := detailDao.Select([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr), meta.OPCMonitorComDetailRule_RuleId.NewPredicate(grmi.Equal, rule.RuleId)}, []grmi.Field{meta.OPCMonitorComDetailRule_DetailId})
					if err != nil {
						return nil, err
					}
					rule.OPCMonitorComDetailRuleLi = detailLi
					ruleLi[index] = rule
				}
			}
			opc.OPCMonitorComRuleLi = ruleLi
			li[index] = opc
		}
		result.Records = li
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		for index, opc := range result {
			ruleLi, err := ruleDao.Select([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr)}, []grmi.Field{meta.OPCMonitorComRule_RuleId})
			if err != nil {
				return nil, err
			}
			for index, rule := range ruleLi {
				if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
					detailLi, err := detailDao.Select([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr), meta.OPCMonitorComDetailRule_RuleId.NewPredicate(grmi.Equal, rule.RuleId)}, []grmi.Field{meta.OPCMonitorComDetailRule_DetailId})
					if err != nil {
						return nil, err
					}
					rule.OPCMonitorComDetailRuleLi = detailLi
					ruleLi[index] = rule
				}
			}
			opc.OPCMonitorComRuleLi = ruleLi
			result[index] = opc
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.Update
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Update(user *models.Usertab, entities *[]model.OPCMonitorBasicComInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease)
		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/OPCMonitorBasicComInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Save(user *models.Usertab, entity *model.OPCMonitorBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.Pid, user.Userid)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(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("不存在对应的工位")
	}
	ruleLi := entity.OPCMonitorComRuleLi
	for index, rule := range ruleLi {
		rule.WorkPlaceNr = entity.WorkPlaceNr
		rule.MonitorManualRelease = entity.MonitorManualRelease
		rule.MonitorProblemPieceRelease = entity.MonitorProblemPieceRelease
		rule.MonitorPalletInPlace = entity.MonitorPalletInPlace
		for detailIndex, detail := range rule.OPCMonitorComDetailRuleLi {
			detail.RuleId = rule.RuleId
			detail.WorkPlaceNr = rule.WorkPlaceNr
			rule.WorkPlaceNr = entity.WorkPlaceNr
			detail.MonitorManualRelease = rule.MonitorManualRelease
			detail.MonitorProblemPieceRelease = rule.MonitorProblemPieceRelease
			detail.MonitorPalletInPlace = rule.MonitorPalletInPlace
			rule.OPCMonitorComDetailRuleLi[detailIndex] = detail
		}
		ruleLi[index] = rule
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorBasicComInfo_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.InsertOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.Insert(&ruleLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	for _, rule := range ruleLi {
		if len(rule.OPCMonitorComDetailRuleLi) > 0 {
			err = detailDao.Insert(&rule.OPCMonitorComDetailRuleLi)
			if err != nil {
				_ = session.Rollback()
				return err
			}
		}
	}
	_ = session.Commit()
	return nil
}

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

	grmi.Log(user, "/services/base/implments/OPCMonitorBasicComInfo.service.impl.go", "SelectOPCMonitorBasicComInfo", "GetRule")

	condition := DefaultConditionOfOPCMonitorBasicComInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.Pid, user.Userid)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	if len(result) > 1 {
		return nil, grmi.NewBusinessError("规则数据过多")
	} else if len(result) == 0 {
		return nil, nil
	}
	for index, opc := range result {
		ruleLi, err := ruleDao.Select([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr)}, []grmi.Field{meta.OPCMonitorComRule_RuleId})
		if err != nil {
			return nil, err
		}
		for index, rule := range ruleLi {
			if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
				detailLi, err := detailDao.Select([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr), meta.OPCMonitorComDetailRule_RuleId.NewPredicate(grmi.Equal, rule.RuleId)}, []grmi.Field{meta.OPCMonitorComDetailRule_DetailId})
				if err != nil {
					return nil, err
				}
				rule.OPCMonitorComDetailRuleLi = detailLi

			} else {
				rule.OPCMonitorComDetailRuleLi = make([]model.OPCMonitorComDetailRule, 0)
			}
			ruleLi[index] = rule
		}
		opc.OPCMonitorComRuleLi = ruleLi
		result[index] = opc
	}

	return result[0], nil
}
