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

package implments

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

/******************************************************************************
 *
 * @Description              : AttributeGroup的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfAttributeGroup = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AttributeGroup_AttrGrpCode.UrlParameterName: grmi.NewConditionItem(meta.AttributeGroup_AttrGrpCode, grmi.Equal, true),
		meta.AttributeGroup_Descr.UrlParameterName:       grmi.NewConditionItem(meta.AttributeGroup_Descr, grmi.Equal, false),
		meta.AttributeGroup_Color.UrlParameterName:       grmi.NewConditionItem(meta.AttributeGroup_Color, grmi.Equal, false),
		meta.AttributeGroup_AgiPara1.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroup_AgiPara1, grmi.Equal, false),
		meta.AttributeGroup_AgiPara2.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroup_AgiPara2, grmi.Equal, false),
		meta.AttributeGroup_AgcPara1.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroup_AgcPara1, grmi.Equal, false),
		meta.AttributeGroup_AgcPara2.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroup_AgcPara2, grmi.Equal, false),
		meta.AttributeGroup_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.AttributeGroup_LastModify, grmi.Approximate, false),
		meta.AttributeGroup_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.AttributeGroup_LastUser, grmi.Equal, false),
		meta.AttributeGroup_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.AttributeGroup_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupService.InsertOne
 *
 ******************************************************************************/
func (impl *AttributeGroupServiceImplement) InsertOne(user *models.Usertab, entity *model.AttributeGroup) error {

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "InsertOneAttributeGroup", "插入一个AttributeGroup")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAttributeGroupDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttrGrpCode)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}

	/**处理数据**/
	GroupValst := make([]model.AttributeGroupValst, 0)
	for k, v := range entity.Valst {
		onedata := model.AttributeGroupValst{}
		onedata.AttrGrpCode = entity.AttrGrpCode
		onedata.Pos = (k+1)
		onedata.AttrCode = v.AttrCode
		GroupValst = append(GroupValst, onedata)
	}
	lstdao := dal.NewAttributeGroupValstDAO(session, user.Pid, user.Userid)
	err = lstdao.Insert(&GroupValst)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()

	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupService.DeleteOne
 *
 ******************************************************************************/
func (impl *AttributeGroupServiceImplement) DeleteOne(user *models.Usertab, attrGrpCode int) error {

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "DeleteOneAttributeGroup", "删除一个AttributeGroup")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAttributeGroupDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(attrGrpCode)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(attrGrpCode)
	if err != nil {
		session.Rollback()
		return err
	}
	lstdao := dal.NewAttributeGroupValstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.AttributeGroupValst_AttrGrpCode.NewPredicate(grmi.Equal, attrGrpCode),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupService.SelectOne
 *
 ******************************************************************************/
func (impl *AttributeGroupServiceImplement) SelectOne(user *models.Usertab, attrGrpCode int) (*model.AttributeGroup, error) {

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "SelectOneAttributeGroup", "查询一个AttributeGroup")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAttributeGroupDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(attrGrpCode)
	if err != nil {
		return nil, err
	}
	lstdao := dal.NewAttributeGroupValstDAO(session, user.Pid, user.Userid)
	list, err := lstdao.SelectAllot(attrGrpCode)
	if err != nil {
		return nil, err
	}
	if utils.ValueIsEmpty(result){
		return nil, err
	}
	//获取已分配的属性集合
	var attrCodes []int
	for _,v := range list{
		attrCodes = append(attrCodes,v.AttrCode)
	}
	unlist, err := lstdao.SelectUnAllot(attrCodes)
	if err != nil {
		return nil, err
	}
	//查询已分配
	result.Valst = list
	//查询未分配
	result.UnValst = unlist
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupService.UpdateOne
 *
 ******************************************************************************/
func (impl *AttributeGroupServiceImplement) UpdateOne(user *models.Usertab, entity *model.AttributeGroup) error {

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "UpdateOneAttributeGroup", "修改一个AttributeGroup")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAttributeGroupDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttrGrpCode)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	/**处理数据**/
	GroupValst := make([]model.AttributeGroupValst, 0)
	for k, v := range entity.Valst {
		onedata := model.AttributeGroupValst{}
		onedata.AttrGrpCode = entity.AttrGrpCode
		onedata.Pos = (k+1)
		onedata.AttrCode = v.AttrCode
		GroupValst = append(GroupValst, onedata)
	}
	lstdao := dal.NewAttributeGroupValstDAO(session, user.Pid, user.Userid)
	err = lstdao.DeleteWhere([]grmi.Predicate{
		meta.AttributeGroupValst_AttrGrpCode.NewPredicate(grmi.Equal, entity.AttrGrpCode),
	})
	if err != nil {
		session.Rollback()
		return err
	}
	err = lstdao.Insert(&GroupValst)
	if err != nil {
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/AttributeGroupService.Insert
 *
 ******************************************************************************/
func (impl *AttributeGroupServiceImplement) Insert(user *models.Usertab, entities *[]model.AttributeGroup) error {

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "InsertAttributeGroup", "插入多个AttributeGroup")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttrGrpCode)
		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/base/AttributeGroupService.Delete
 *
 ******************************************************************************/
func (impl *AttributeGroupServiceImplement) Delete(user *models.Usertab, entities *[]model.AttributeGroup) error {

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "DeleteAttributeGroup", "删除多个AttributeGroup")

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

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

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "SelectAttributeGroup", "查询AttributeGroup")

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

	grmi.Log(user, "/services/base/implments/AttributeGroup.service.impl.go", "UpdateAttributeGroup", "修改多个AttributeGroup")

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

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

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