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

package implments

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

/******************************************************************************
 *
 * @Description              : DefectReason的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfDefectReason = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DefectReason_DefectReasonId.UrlParameterName: grmi.NewConditionItem(meta.DefectReason_DefectReasonId, grmi.Equal, true),
		meta.DefectReason_Descr.UrlParameterName:          grmi.NewConditionItem(meta.DefectReason_Descr, grmi.Equal, false),
		meta.DefectReason_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.DefectReason_LastModify, grmi.Approximate, false),
		meta.DefectReason_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.DefectReason_LastUser, grmi.Equal, false),
		meta.DefectReason_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.DefectReason_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectReasonService.InsertOne
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) InsertOne(user *models.Usertab, entity *model.DefectReason) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "InsertOneDefectReason", "插入一个DefectReason")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDefectReasonDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(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_LF_MOM_BACKEND/services/qm/DefectReasonService.DeleteOne
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) DeleteOne(user *models.Usertab, defectReasonId string) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "DeleteOneDefectReason", "删除一个DefectReason")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectReasonService.SelectOne
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) SelectOne(user *models.Usertab, defectReasonId string) (*model.DefectReason, error) {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "SelectOneDefectReason", "查询一个DefectReason")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/DefectReasonService.UpdateOne
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) UpdateOne(user *models.Usertab, entity *model.DefectReason) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "UpdateOneDefectReason", "修改一个DefectReason")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDefectReasonDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(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_LF_MOM_BACKEND/services/qm/DefectReasonService.Insert
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) Insert(user *models.Usertab, entities *[]model.DefectReason) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "InsertDefectReason", "插入多个DefectReason")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(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_LF_MOM_BACKEND/services/qm/DefectReasonService.Delete
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) Delete(user *models.Usertab, entities *[]model.DefectReason) error {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "DeleteDefectReason", "删除多个DefectReason")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(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_LF_MOM_BACKEND/services/qm/DefectReasonService.Select
 *
 ******************************************************************************/
func (impl *DefectReasonServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "SelectDefectReason", "查询DefectReason")

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

	grmi.Log(user, "/services/qm/implments/QM_DefectReason.service.impl.go", "UpdateDefectReason", "修改多个DefectReason")

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

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

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