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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/pm"
	"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"
	"fmt"
)

/******************************************************************************
 *
 * @Description              : UserServiceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:50:49
 *
 ******************************************************************************/
var DefaultConditionOfUserServiceLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.UserServiceLst_UserId.UrlParameterName:        grmi.NewConditionItem(meta.UserServiceLst_UserId, grmi.Equal, true),
		meta.UserServiceLst_MainServiceId.UrlParameterName: grmi.NewConditionItem(meta.UserServiceLst_MainServiceId, grmi.Equal, true),
		meta.UserServiceLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.UserServiceLst_Pos, grmi.Equal, false),
		meta.UserServiceLst_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.UserServiceLst_CtrlPara1, grmi.Equal, false),
		meta.UserServiceLst_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.UserServiceLst_CtrlPara2, grmi.Equal, false),
		meta.UserServiceLst_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.UserServiceLst_CtrlPara3, grmi.Equal, false),
		meta.UserServiceLst_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.UserServiceLst_CtrlPara4, grmi.Equal, false),
		meta.UserServiceLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.UserServiceLst_LastModify, grmi.Approximate, false),
		meta.UserServiceLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.UserServiceLst_LastUser, grmi.Equal, false),
		meta.UserServiceLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.UserServiceLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/UserServiceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) InsertOne(user *models.Usertab, entity *model.UserServiceLst) error {

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "InsertOneUserServiceLst", "插入一个UserServiceLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.UserId, entity.MainServiceId)
	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/UserServiceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) DeleteOne(user *models.Usertab, userId string, mainServiceId string) error {

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "DeleteOneUserServiceLst", "删除一个UserServiceLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/UserServiceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) SelectOne(user *models.Usertab, userId string, mainServiceId string) (*model.UserServiceLst, error) {

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "SelectOneUserServiceLst", "查询一个UserServiceLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pm/UserServiceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.UserServiceLst) error {

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "UpdateOneUserServiceLst", "修改一个UserServiceLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.UserId, entity.MainServiceId)
	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/UserServiceLstService.Insert
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) Insert(user *models.Usertab, insertItem *model.UserServiceLstInsert) error {

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "InsertUserServiceLst", "插入多个UserServiceLst")

	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("不存在对应的用户，请查看")
	}
	entities := make([]model.UserServiceLst, len(records))
	dao := dal.NewUserServiceLstDAO(session, user.Pid, user.Userid)

	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.MainServiceId)
		//if err != nil {
		//	return err
		//}
		//if record != nil {
		//	return grmi.NewBusinessError("已经存在相同主键的记录!")
		//}
		var pmService models.PmService
		pmService.Finr = user.Pid
		pmService.Mainserviceid = entity.MainServiceId
		pmService, err = pmService.SelectOne()
		if err != nil {
			return err
		}
		if pmService.Mainserviceid == "" {
			return grmi.NewBusinessError(fmt.Sprintf("不存在对应的pm service，Mainserviceid:%s", entity.MainServiceId))
		}
		entity.Pos = index + 1
		entities[index] = entity
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.UserServiceLst_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/UserServiceLstService.Delete
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) Delete(user *models.Usertab, entities *[]model.UserServiceLst) error {

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "DeleteUserServiceLst", "删除多个UserServiceLst")

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

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

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "SelectUserServiceLst", "查询UserServiceLst")

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

	grmi.Log(user, "/services/pm/implments/PM_UserServiceLst.service.impl.go", "UpdateUserServiceLst", "修改多个UserServiceLst")

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

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

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