// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/me"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/me"
	model "LAPP_LF_MOM_BACKEND/models/me"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : SetupMatrix的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
var DefaultConditionOfSetupMatrix = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SetupMatrix_FromArtId.UrlParameterName:  grmi.NewConditionItem(meta.SetupMatrix_FromArtId, grmi.Equal, true),
		meta.SetupMatrix_ToArtId.UrlParameterName:    grmi.NewConditionItem(meta.SetupMatrix_ToArtId, grmi.Equal, true),
		meta.SetupMatrix_WorkLineId.UrlParameterName: grmi.NewConditionItem(meta.SetupMatrix_WorkLineId, grmi.Equal, true),
		meta.SetupMatrix_Descr1.UrlParameterName:     grmi.NewConditionItem(meta.SetupMatrix_Descr1, grmi.Equal, false),
		meta.SetupMatrix_Descr2.UrlParameterName:     grmi.NewConditionItem(meta.SetupMatrix_Descr2, grmi.Equal, false),
		meta.SetupMatrix_ProjectId.UrlParameterName:  grmi.NewConditionItem(meta.SetupMatrix_ProjectId, grmi.Equal, false),
		meta.SetupMatrix_SetupTime.UrlParameterName:  grmi.NewConditionItem(meta.SetupMatrix_SetupTime, grmi.Equal, false),
		meta.SetupMatrix_TimeUomId.UrlParameterName:  grmi.NewConditionItem(meta.SetupMatrix_TimeUomId, grmi.Equal, false),
		meta.SetupMatrix_LastModify.UrlParameterName: grmi.NewConditionItem(meta.SetupMatrix_LastModify, grmi.Approximate, false),
		meta.SetupMatrix_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.SetupMatrix_LastUser, grmi.Equal, false),
		meta.SetupMatrix_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.SetupMatrix_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SetupMatrix的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
var DefaultConditionOfSetupMatrixAndPaging = grmi.NewCondition(
	DefaultConditionOfSetupMatrix.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SetupMatrixServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SetupMatrix的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
type SetupMatrixServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSetupMatrixDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SetupMatrixDAOImplement实例
 *
 * @Return Value             : SetupMatrixDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-14 08:56:34
 *
 ******************************************************************************/
func NewSetupMatrixServiceImplement() *SetupMatrixServiceImplement {
	return &SetupMatrixServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/SetupMatrixService.InsertOne
 *
 ******************************************************************************/
func (impl *SetupMatrixServiceImplement) InsertOne(user *models.Usertab, entity *model.SetupMatrix) error {

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "InsertOneSetupMatrix", "插入一个SetupMatrix")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSetupMatrixDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.FromArtId, entity.ToArtId, entity.WorkLineId)
	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/me/SetupMatrixService.DeleteOne
 *
 ******************************************************************************/
func (impl *SetupMatrixServiceImplement) DeleteOne(user *models.Usertab, fromArtId string, toArtId string, workLineId string) error {

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "DeleteOneSetupMatrix", "删除一个SetupMatrix")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/SetupMatrixService.SelectOne
 *
 ******************************************************************************/
func (impl *SetupMatrixServiceImplement) SelectOne(user *models.Usertab, fromArtId string, toArtId string, workLineId string) (*model.SetupMatrix, error) {

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "SelectOneSetupMatrix", "查询一个SetupMatrix")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/me/SetupMatrixService.UpdateOne
 *
 ******************************************************************************/
func (impl *SetupMatrixServiceImplement) UpdateOne(user *models.Usertab, entity *model.SetupMatrix) error {

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "UpdateOneSetupMatrix", "修改一个SetupMatrix")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSetupMatrixDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.FromArtId, entity.ToArtId, entity.WorkLineId)
	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/me/SetupMatrixService.Insert
 *
 ******************************************************************************/
func (impl *SetupMatrixServiceImplement) Insert(user *models.Usertab, entities *[]model.SetupMatrix) error {

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "InsertSetupMatrix", "插入多个SetupMatrix")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.FromArtId, entity.ToArtId, entity.WorkLineId)
		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/me/SetupMatrixService.Delete
 *
 ******************************************************************************/
func (impl *SetupMatrixServiceImplement) Delete(user *models.Usertab, entities *[]model.SetupMatrix) error {

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "DeleteSetupMatrix", "删除多个SetupMatrix")

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

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

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "SelectSetupMatrix", "查询SetupMatrix")

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

	grmi.Log(user, "/services/me/implments/SetupMatrix.service.impl.go", "UpdateSetupMatrix", "修改多个SetupMatrix")

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

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

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