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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/qm"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/qm"
	model "LAPP_ACURA_MOM_BACKEND/models/qm"
)

/******************************************************************************
 *
 * @Description              : DefectGrpReasonLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfDefectGrpReasonLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DefectGrpReasonLst_DefectGrpId.UrlParameterName:    grmi.NewConditionItem(meta.DefectGrpReasonLst_DefectGrpId, grmi.Equal, true),
		meta.DefectGrpReasonLst_DefectReasonId.UrlParameterName: grmi.NewConditionItem(meta.DefectGrpReasonLst_DefectReasonId, grmi.Equal, true),
		meta.DefectGrpReasonLst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.DefectGrpReasonLst_Pos, grmi.Equal, false),
		meta.DefectGrpReasonLst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.DefectGrpReasonLst_LastModify, grmi.Approximate, false),
		meta.DefectGrpReasonLst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.DefectGrpReasonLst_LastUser, grmi.Equal, false),
		meta.DefectGrpReasonLst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.DefectGrpReasonLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DefectGrpReasonLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfDefectGrpReasonLstAndPaging = grmi.NewCondition(
	DefaultConditionOfDefectGrpReasonLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : DefectGrpReasonLstServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : DefectGrpReasonLst的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type DefectGrpReasonLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDefectGrpReasonLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DefectGrpReasonLstDAOImplement实例
 *
 * @Return Value             : DefectGrpReasonLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewDefectGrpReasonLstServiceImplement() *DefectGrpReasonLstServiceImplement {
	return &DefectGrpReasonLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/DefectGrpReasonLstService.InsertOne
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) InsertOne(user *global.User, entity *model.DefectGrpReasonLst) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "InsertOneDefectGrpReasonLst", "插入一个DefectGrpReasonLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDefectGrpReasonLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DefectGrpId, entity.DefectReasonId)
	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/qm/DefectGrpReasonLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) DeleteOne(user *global.User, defectGrpId string, defectReasonId string) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "DeleteOneDefectGrpReasonLst", "删除一个DefectGrpReasonLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/DefectGrpReasonLstService.SelectOne
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) SelectOne(user *global.User, defectGrpId string, defectReasonId string) (*model.DefectGrpReasonLst, error) {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "SelectOneDefectGrpReasonLst", "查询一个DefectGrpReasonLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/DefectGrpReasonLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) UpdateOne(user *global.User, entity *model.DefectGrpReasonLst) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "UpdateOneDefectGrpReasonLst", "修改一个DefectGrpReasonLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDefectGrpReasonLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.DefectGrpId, entity.DefectReasonId)
	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/qm/DefectGrpReasonLstService.Insert
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) Insert(user *global.User, entities *[]model.DefectGrpReasonLst) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "InsertDefectGrpReasonLst", "插入多个DefectGrpReasonLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DefectGrpId, entity.DefectReasonId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/DefectGrpReasonLstService.Delete
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) Delete(user *global.User, entities *[]model.DefectGrpReasonLst) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "DeleteDefectGrpReasonLst", "删除多个DefectGrpReasonLst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DefectGrpId, entity.DefectReasonId)
		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/qm/DefectGrpReasonLstService.Select
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "SelectDefectGrpReasonLst", "查询DefectGrpReasonLst")

	condition := DefaultConditionOfDefectGrpReasonLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewDefectGrpReasonLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/qm/DefectGrpReasonLstService.Update
 *
 ******************************************************************************/
func (impl *DefectGrpReasonLstServiceImplement) Update(user *global.User, entities *[]model.DefectGrpReasonLst) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectGrpReasonLst.service.impl.go", "UpdateDefectGrpReasonLst", "修改多个DefectGrpReasonLst")

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

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

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