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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/pm"
	"fmt"

	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/pm"
	model "LAPP_LF_MOM_BACKEND/models/pm"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : UserAssetLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:50:49
 *
 ******************************************************************************/
var DefaultConditionOfUserAssetLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.UserAssetLst_UserId.UrlParameterName:     grmi.NewConditionItem(meta.UserAssetLst_UserId, grmi.Equal, true),
		meta.UserAssetLst_AssetId.UrlParameterName:    grmi.NewConditionItem(meta.UserAssetLst_AssetId, grmi.Equal, true),
		meta.UserAssetLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.UserAssetLst_Pos, grmi.Equal, false),
		meta.UserAssetLst_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.UserAssetLst_CtrlPara1, grmi.Equal, false),
		meta.UserAssetLst_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.UserAssetLst_CtrlPara2, grmi.Equal, false),
		meta.UserAssetLst_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.UserAssetLst_CtrlPara3, grmi.Equal, false),
		meta.UserAssetLst_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.UserAssetLst_CtrlPara4, grmi.Equal, false),
		meta.UserAssetLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.UserAssetLst_LastModify, grmi.Approximate, false),
		meta.UserAssetLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.UserAssetLst_LastUser, grmi.Equal, false),
		meta.UserAssetLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.UserAssetLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : UserAssetLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:50:49
 *
 ******************************************************************************/
var DefaultConditionOfUserAssetLstAndPaging = grmi.NewCondition(
	DefaultConditionOfUserAssetLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : UserAssetLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : UserAssetLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:50:49
 *
 ******************************************************************************/
type UserAssetLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewUserAssetLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个UserAssetLstDAOImplement实例
 *
 * @Return Value             : UserAssetLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:50:49
 *
 ******************************************************************************/
func NewUserAssetLstServiceImplement() *UserAssetLstServiceImplement {
	return &UserAssetLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/UserAssetLstService.InsertOne
 *
 ******************************************************************************/
func (impl *UserAssetLstServiceImplement) InsertOne(user *models.Usertab, entity *model.UserAssetLst) error {

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "InsertOneUserAssetLst", "插入一个UserAssetLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserAssetLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.UserId, entity.AssetId)
	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/pm/UserAssetLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *UserAssetLstServiceImplement) DeleteOne(user *models.Usertab, userId string, assetId int) error {

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "DeleteOneUserAssetLst", "删除一个UserAssetLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/UserAssetLstService.SelectOne
 *
 ******************************************************************************/
func (impl *UserAssetLstServiceImplement) SelectOne(user *models.Usertab, userId string, assetId int) (*model.UserAssetLst, error) {

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "SelectOneUserAssetLst", "查询一个UserAssetLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/UserAssetLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *UserAssetLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.UserAssetLst) error {

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "UpdateOneUserAssetLst", "修改一个UserAssetLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserAssetLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.UserId, entity.AssetId)
	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/pm/UserAssetLstService.Insert
 *
 ******************************************************************************/
func (impl *UserAssetLstServiceImplement) Insert(user *models.Usertab, insertItem *model.UserAssetLstInsert) error {

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "InsertUserAssetLst", "插入多个UserAssetLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	userId := insertItem.UserId
	records := insertItem.Records
	var userTab models.Usertab
	userTab.Userid = userId
	userTab.Pid = user.Pid
	userView, err := userTab.Get()
	if err != nil {
		return err
	}
	if userView.Pid == 0 {
		return grmi.NewBusinessError("不存在对应的用户，请查看")
	}
	dao := dal.NewUserAssetLstDAO(session, user.Pid, user.Userid)
	fmt.Println("records:", records)
	entities := make([]model.UserAssetLst, len(records))
	for index, entity := range records {
		if entity.UserId != userId {
			return grmi.NewBusinessError(fmt.Sprintf("记录与传入的userId不符, record userId:%s, need userId:%s", entity.UserId, userId))
		}
		//record, err := dao.SelectOne(entity.UserId, entity.AssetId)
		//if err != nil {
		//	return err
		//}
		//if record != nil {
		//	return grmi.NewBusinessError("已经存在相同主键的记录!")
		//}
		var pmAsset models.PmAsset
		pmAsset.Finr = user.Pid
		pmAsset.Assetid = entity.AssetId
		pmAsset, err = pmAsset.SelectOne()
		if err != nil {
			return err
		}
		if pmAsset.Assetid == 0 {
			return grmi.NewBusinessError(fmt.Sprintf("不存在对应的资产, assertId:%d", entity.AssetId))
		}
		entity.Pos = index + 1
		entities[index] = entity
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.UserAssetLst_UserId.NewPredicate(grmi.Equal, userId)})
	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/pm/UserAssetLstService.Delete
 *
 ******************************************************************************/
func (impl *UserAssetLstServiceImplement) Delete(user *models.Usertab, entities *[]model.UserAssetLst) error {

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "DeleteUserAssetLst", "删除多个UserAssetLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "SelectUserAssetLst", "查询UserAssetLst")

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

	grmi.Log(user, "/services/pm/implments/PM_UserAssetLst.service.impl.go", "UpdateUserAssetLst", "修改多个UserAssetLst")

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

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

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