// 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              : ViewDefectlst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfViewDefectlst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ViewDefectlst_ViewId.UrlParameterName:         grmi.NewConditionItem(meta.ViewDefectlst_ViewId, grmi.Equal, true),
		meta.ViewDefectlst_DefectReasonId.UrlParameterName: grmi.NewConditionItem(meta.ViewDefectlst_DefectReasonId, grmi.Equal, true),
		meta.ViewDefectlst_Pos.UrlParameterName:            grmi.NewConditionItem(meta.ViewDefectlst_Pos, grmi.Equal, false),
		meta.ViewDefectlst_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.ViewDefectlst_LastModify, grmi.Approximate, false),
		meta.ViewDefectlst_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.ViewDefectlst_LastUser, grmi.Equal, false),
		meta.ViewDefectlst_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.ViewDefectlst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewDefectlstService.InsertOne
 *
 ******************************************************************************/
func (impl *ViewDefectlstServiceImplement) InsertOne(user *models.Usertab, entity *model.ViewDefectlst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "InsertOneViewDefectlst", "插入一个ViewDefectlst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "DeleteOneViewDefectlst", "删除一个ViewDefectlst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewDefectlstService.SelectOne
 *
 ******************************************************************************/
func (impl *ViewDefectlstServiceImplement) SelectOne(user *models.Usertab, viewId string, defectReasonId string) (*model.ViewDefectlst, error) {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "SelectOneViewDefectlst", "查询一个ViewDefectlst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewDefectlstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ViewDefectlstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ViewDefectlst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "UpdateOneViewDefectlst", "修改一个ViewDefectlst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "InsertViewDefectlst", "插入多个ViewDefectlst")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "DeleteViewDefectlst", "删除多个ViewDefectlst")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "SelectViewDefectlst", "查询ViewDefectlst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewDefectlst.service.impl.go", "UpdateViewDefectlst", "修改多个ViewDefectlst")

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

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

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