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

package implments

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

/******************************************************************************
 *
 * @Description              : VendorUserLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-25 10:13:02
 *
 ******************************************************************************/
var DefaultConditionOfVendorUserLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.VendorUserLst_VendorId.UrlParameterName:   grmi.NewConditionItem(meta.VendorUserLst_VendorId, grmi.Equal, true),
		meta.VendorUserLst_UserId.UrlParameterName:     grmi.NewConditionItem(meta.VendorUserLst_UserId, grmi.Equal, true),
		meta.VendorUserLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.VendorUserLst_Pos, grmi.Equal, false),
		meta.VendorUserLst_CtrlLevel.UrlParameterName:  grmi.NewConditionItem(meta.VendorUserLst_CtrlLevel, grmi.Equal, false),
		meta.VendorUserLst_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.VendorUserLst_CtrlPara1, grmi.Equal, false),
		meta.VendorUserLst_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.VendorUserLst_CtrlPara2, grmi.Equal, false),
		meta.VendorUserLst_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.VendorUserLst_CtrlPara3, grmi.Equal, false),
		meta.VendorUserLst_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.VendorUserLst_CtrlPara4, grmi.Equal, false),
		meta.VendorUserLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.VendorUserLst_LastModify, grmi.Approximate, false),
		meta.VendorUserLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.VendorUserLst_LastUser, grmi.Equal, false),
		meta.VendorUserLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.VendorUserLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : VendorUserLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-25 10:13:02
 *
 ******************************************************************************/
var DefaultConditionOfVendorUserLstAndPaging = grmi.NewCondition(
	DefaultConditionOfVendorUserLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : VendorUserLstServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : VendorUserLst的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-25 10:13:02
*
******************************************************************************/
type VendorUserLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewVendorUserLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个VendorUserLstDAOImplement实例
 *
 * @Return Value             : VendorUserLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-25 10:13:02
 *
 ******************************************************************************/
func NewVendorUserLstServiceImplement() *VendorUserLstServiceImplement {
	return &VendorUserLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorUserLstService.InsertOne
 *
 ******************************************************************************/
func (impl *VendorUserLstServiceImplement) InsertOne(user *models.Usertab, entity *model.VendorUserLst) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "InsertOneVendorUserLst", "插入一个VendorUserLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	vendorDao := dal.NewVendorDAO(session, user.Pid, user.Userid)
	vendor, err := vendorDao.SelectOne(entity.VendorId)
	if err != nil {
		return err
	}
	if vendor == nil {
		return grmi.NewBusinessError("不存在对应的供应商，请查看")
	}
	var userTab models.Usertab
	userTab.Userid = entity.UserId
	userTab.Pid = user.Pid
	_, err = userTab.Get()
	if err != nil {
		return err
	}
	dao := dal.NewVendorUserLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.VendorId, entity.UserId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	pos, err := dao.GetMaxPos(entity.VendorId)
	if err != nil {
		return err
	}
	entity.Pos = pos + 1
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorUserLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *VendorUserLstServiceImplement) DeleteOne(user *models.Usertab, vendorId string, userId string) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "DeleteOneVendorUserLst", "删除一个VendorUserLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorUserLstService.SelectOne
 *
 ******************************************************************************/
func (impl *VendorUserLstServiceImplement) SelectOne(user *models.Usertab, vendorId string, userId string) (*model.VendorUserLst, error) {

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "SelectOneVendorUserLst", "查询一个VendorUserLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorUserLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *VendorUserLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.VendorUserLst) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "UpdateOneVendorUserLst", "修改一个VendorUserLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorUserLstService.Insert
 *
 ******************************************************************************/
func (impl *VendorUserLstServiceImplement) Insert(user *models.Usertab, insertItem *model.VendorUserLstInsert) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "InsertVendorUserLst", "插入多个VendorUserLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewVendorUserLstDAO(session, user.Pid, user.Userid)
	vendorDao := dal.NewVendorDAO(session, user.Pid, user.Userid)
	entities := make([]model.VendorUserLst, len(insertItem.Records))

	for index, entity := range insertItem.Records {
		vendor, err := vendorDao.SelectOne(entity.VendorId)
		if err != nil {
			return err
		}
		if vendor == nil {
			return grmi.NewBusinessError("不存在对应的供应商，请查看")
		}
		var userTab models.Usertab
		userTab.Userid = entity.UserId
		userTab.Pid = user.Pid
		user, err := userTab.Get()
		if err != nil {
			return err
		}
		if user.Pid == 0 {
			return grmi.NewBusinessError("不存在对应的用户，请查看")
		}
		if entity.VendorId != insertItem.VendorId {
			return grmi.NewBusinessError("插入的数据与设置的vendorId不相符")
		}
		entity.Pos = index + 1
		entities[index] = entity
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.VendorUserLst_VendorId.NewPredicate(grmi.Equal, insertItem.VendorId)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = dao.Insert(&entities)
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorUserLstService.Delete
 *
 ******************************************************************************/
func (impl *VendorUserLstServiceImplement) Delete(user *models.Usertab, entities *[]model.VendorUserLst) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "DeleteVendorUserLst", "删除多个VendorUserLst")

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

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

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "SelectVendorUserLst", "查询VendorUserLst")

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

	grmi.Log(user, "/services/log/implments/LOG_VendorUserLst.service.impl.go", "UpdateVendorUserLst", "修改多个VendorUserLst")

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

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

	err = dao.Update(entities)
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}
