// 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              : ViewDefectGrplst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfViewDefectGrplst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ViewDefectGrplst_ViewId.UrlParameterName:      grmi.NewConditionItem(meta.ViewDefectGrplst_ViewId, grmi.Equal, true),
		meta.ViewDefectGrplst_DefectGrpId.UrlParameterName: grmi.NewConditionItem(meta.ViewDefectGrplst_DefectGrpId, grmi.Equal, true),
		meta.ViewDefectGrplst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.ViewDefectGrplst_Pos, grmi.Equal, false),
		meta.ViewDefectGrplst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ViewDefectGrplst_LastModify, grmi.Approximate, false),
		meta.ViewDefectGrplst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ViewDefectGrplst_LastUser, grmi.Equal, false),
		meta.ViewDefectGrplst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ViewDefectGrplst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewDefectGrplstService.InsertOne
 *
 ******************************************************************************/
func (impl *ViewDefectGrplstServiceImplement) InsertOne(user *models.Usertab, entity *model.ViewDefectGrplst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "InsertOneViewDefectGrplst", "插入一个ViewDefectGrplst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewViewDefectGrplstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ViewId, entity.DefectGrpId)
	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/ViewDefectGrplstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ViewDefectGrplstServiceImplement) DeleteOne(user *models.Usertab, viewId string, defectGrpId string) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "DeleteOneViewDefectGrplst", "删除一个ViewDefectGrplst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewDefectGrplstService.SelectOne
 *
 ******************************************************************************/
func (impl *ViewDefectGrplstServiceImplement) SelectOne(user *models.Usertab, viewId string, defectGrpId string) (*model.ViewDefectGrplst, error) {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "SelectOneViewDefectGrplst", "查询一个ViewDefectGrplst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewDefectGrplstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ViewDefectGrplstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ViewDefectGrplst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "UpdateOneViewDefectGrplst", "修改一个ViewDefectGrplst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewViewDefectGrplstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ViewId, entity.DefectGrpId)
	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/ViewDefectGrplstService.Insert
 *
 ******************************************************************************/
func (impl *ViewDefectGrplstServiceImplement) Insert(user *models.Usertab, entities *[]model.ViewDefectGrplst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "InsertViewDefectGrplst", "插入多个ViewDefectGrplst")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ViewId, entity.DefectGrpId)
		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/ViewDefectGrplstService.Delete
 *
 ******************************************************************************/
func (impl *ViewDefectGrplstServiceImplement) Delete(user *models.Usertab, entities *[]model.ViewDefectGrplst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "DeleteViewDefectGrplst", "删除多个ViewDefectGrplst")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "SelectViewDefectGrplst", "查询ViewDefectGrplst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectGrplst.service.impl.go", "UpdateViewDefectGrplst", "修改多个ViewDefectGrplst")

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

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

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