// 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              : ViewValst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfViewValst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ViewValst_ViewId.UrlParameterName:     grmi.NewConditionItem(meta.ViewValst_ViewId, grmi.Equal, true),
		meta.ViewValst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.ViewValst_Pos, grmi.Equal, true),
		meta.ViewValst_ValName.UrlParameterName:    grmi.NewConditionItem(meta.ViewValst_ValName, grmi.Equal, false),
		meta.ViewValst_XStart.UrlParameterName:     grmi.NewConditionItem(meta.ViewValst_XStart, grmi.Equal, false),
		meta.ViewValst_YStart.UrlParameterName:     grmi.NewConditionItem(meta.ViewValst_YStart, grmi.Equal, false),
		meta.ViewValst_Width.UrlParameterName:      grmi.NewConditionItem(meta.ViewValst_Width, grmi.Equal, false),
		meta.ViewValst_Height.UrlParameterName:     grmi.NewConditionItem(meta.ViewValst_Height, grmi.Equal, false),
		meta.ViewValst_Descr.UrlParameterName:      grmi.NewConditionItem(meta.ViewValst_Descr, grmi.Equal, false),
		meta.ViewValst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.ViewValst_LastModify, grmi.Approximate, false),
		meta.ViewValst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.ViewValst_LastUser, grmi.Equal, false),
		meta.ViewValst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.ViewValst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewValstService.InsertOne
 *
 ******************************************************************************/
func (impl *ViewValstServiceImplement) InsertOne(user *models.Usertab, entity *model.ViewValst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "InsertOneViewValst", "插入一个ViewValst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "DeleteOneViewValst", "删除一个ViewValst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewValstService.SelectOne
 *
 ******************************************************************************/
func (impl *ViewValstServiceImplement) SelectOne(user *models.Usertab, viewId string, pos int) (*model.ViewValst, error) {

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "SelectOneViewValst", "查询一个ViewValst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ViewValstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ViewValstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ViewValst) error {

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "UpdateOneViewValst", "修改一个ViewValst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "InsertViewValst", "插入多个ViewValst")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "DeleteViewValst", "删除多个ViewValst")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "SelectViewValst", "查询ViewValst")

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

	grmi.Log(user, "/services/qm/implments/QM_ViewValst.service.impl.go", "UpdateViewValst", "修改多个ViewValst")

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

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

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