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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/pm"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/pm"
	model "LAPP_ACURA_MOM_BACKEND/models/pm"
	"fmt"
	"strconv"
)

/******************************************************************************
 *
 * @Description              : UserServiceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
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-10-08 13:20:42
 *
 ******************************************************************************/
var DefaultConditionOfUserServiceLstAndPaging = grmi.NewCondition(
	DefaultConditionOfUserServiceLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : UserServiceLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : UserServiceLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
type UserServiceLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewUserServiceLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个UserServiceLstDAOImplement实例
 *
 * @Return Value             : UserServiceLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-08 13:20:42
 *
 ******************************************************************************/
func NewUserServiceLstServiceImplement() *UserServiceLstServiceImplement {
	return &UserServiceLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) InsertOne(user *global.User, entity *model.UserServiceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) DeleteOne(user *global.User, userId string, mainServiceId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) SelectOne(user *global.User, userId string, mainServiceId string) (*model.UserServiceLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) UpdateOne(user *global.User, entity *model.UserServiceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.Insert
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) Insert(user *global.User, entities *[]model.UserServiceLst) error {

	grmi.Log(user, "/services/pm/implments/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 := user.UserId
	records := entities
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, user.UserId)
	dao_Service := dal.NewServiceDAO(session, user.PlantNr, user.UserId)
	//dao_User := dal_base.NewUserDAO(session, user.PlantNr, user.UserId)
	//_, err = dao_User.SelectOne(user.UserId)
	//if err != nil {
	//	session.Rollback()
	//	return err
	//}
	fmt.Println("records:", records)

	for index, _ := range *entities {
		entitiesValue := *entities
		if entitiesValue[index].UserId != userId {
			return grmi.NewBusinessError(fmt.Sprintf("记录与传入的userId不符, record userId:%s, need userId:%s", entitiesValue[index].UserId, userId))
		}
		MainServiceId, err := strconv.Atoi(entitiesValue[index].MainServiceId)
		if err != nil {
			return err
		}
		pmService, err := dao_Service.SelectOne(MainServiceId)
		if err != nil {
			return err
		}
		if pmService == nil {
			return grmi.NewBusinessError(fmt.Sprintf("不存在对应的pm service，Mainserviceid:%d", MainServiceId))
		}

		entitiesValue[index].Pos = index + 1
	}
	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_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.Delete
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) Delete(user *global.User, entities *[]model.UserServiceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.Select
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.UserServiceLst, error) {

	grmi.Log(user, "/services/pm/implments/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.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/pm/implments/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 grmi.EmptyPagingResult, err
	}
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pm/UserServiceLstService.Update
 *
 ******************************************************************************/
func (impl *UserServiceLstServiceImplement) Update(user *global.User, entities *[]model.UserServiceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewUserServiceLstDAO(session, user.PlantNr, 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
}
