// 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              : ProcessDependOn的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfProcessDependOn = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProcessDependOn_PO.UrlParameterName:           grmi.NewConditionItem(meta.ProcessDependOn_PO, grmi.Equal, true),
		meta.ProcessDependOn_Pos.UrlParameterName:          grmi.NewConditionItem(meta.ProcessDependOn_Pos, grmi.Equal, true),
		meta.ProcessDependOn_DependPO.UrlParameterName:     grmi.NewConditionItem(meta.ProcessDependOn_DependPO, grmi.Equal, false),
		meta.ProcessDependOn_DependStepNo.UrlParameterName: grmi.NewConditionItem(meta.ProcessDependOn_DependStepNo, grmi.Equal, false),
		meta.ProcessDependOn_DependRuleId.UrlParameterName: grmi.NewConditionItem(meta.ProcessDependOn_DependRuleId, grmi.Equal, false),
		meta.ProcessDependOn_DependValue1.UrlParameterName: grmi.NewConditionItem(meta.ProcessDependOn_DependValue1, grmi.Equal, false),
		meta.ProcessDependOn_DependValue2.UrlParameterName: grmi.NewConditionItem(meta.ProcessDependOn_DependValue2, grmi.Equal, false),
		meta.ProcessDependOn_CtrlPara1.UrlParameterName:    grmi.NewConditionItem(meta.ProcessDependOn_CtrlPara1, grmi.Equal, false),
		meta.ProcessDependOn_CtrlPara2.UrlParameterName:    grmi.NewConditionItem(meta.ProcessDependOn_CtrlPara2, grmi.Equal, false),
		meta.ProcessDependOn_CtrlPara3.UrlParameterName:    grmi.NewConditionItem(meta.ProcessDependOn_CtrlPara3, grmi.Equal, false),
		meta.ProcessDependOn_CtrlPara4.UrlParameterName:    grmi.NewConditionItem(meta.ProcessDependOn_CtrlPara4, grmi.Equal, false),
		meta.ProcessDependOn_CtrlPara5.UrlParameterName:    grmi.NewConditionItem(meta.ProcessDependOn_CtrlPara5, grmi.Equal, false),
		meta.ProcessDependOn_CtrlPara6.UrlParameterName:    grmi.NewConditionItem(meta.ProcessDependOn_CtrlPara6, grmi.Equal, false),
		meta.ProcessDependOn_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.ProcessDependOn_LastModify, grmi.Approximate, false),
		meta.ProcessDependOn_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.ProcessDependOn_LastUser, grmi.Equal, false),
		meta.ProcessDependOn_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.ProcessDependOn_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProcessDependOn的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfProcessDependOnAndPaging = grmi.NewCondition(
	DefaultConditionOfProcessDependOn.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProcessDependOnServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ProcessDependOn的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
type ProcessDependOnServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProcessDependOnDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProcessDependOnDAOImplement实例
 *
 * @Return Value             : ProcessDependOnDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
func NewProcessDependOnServiceImplement() *ProcessDependOnServiceImplement {
	return &ProcessDependOnServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessDependOnService.InsertOne
 *
 ******************************************************************************/
func (impl *ProcessDependOnServiceImplement) InsertOne(user *global.User, entity *model.ProcessDependOn) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDependOnDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PO, entity.Pos)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessDependOnService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProcessDependOnServiceImplement) DeleteOne(user *global.User, pO string, pos int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessDependOnService.SelectOne
 *
 ******************************************************************************/
func (impl *ProcessDependOnServiceImplement) SelectOne(user *global.User, pO string, pos int) (*model.ProcessDependOn, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessDependOnService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProcessDependOnServiceImplement) UpdateOne(user *global.User, entity *model.ProcessDependOn) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDependOnDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PO, entity.Pos)
	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/ProcessDependOnService.Insert
 *
 ******************************************************************************/
func (impl *ProcessDependOnServiceImplement) Insert(user *global.User, entities *model.ProcessDependOnData) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewProcessDependOnDAO(session, user.PlantNr, user.UserId)
	processDao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	po := entities.PO
	process, err := processDao.SelectOne(po)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if process == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工序")
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.ProcessDependOn_PO.NewPredicate(grmi.Equal, po)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	li := entities.ProcessDependOnLi
	for index, entity := range li {
		entity.Pos = index + 1
		entity.PO = po
		li[index] = entity
	}
	err = dao.Insert(&li)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessDependOnService.Delete
 *
 ******************************************************************************/
func (impl *ProcessDependOnServiceImplement) Delete(user *global.User, entities *[]model.ProcessDependOn) error {

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

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

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

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

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

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

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

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

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

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

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