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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/wm"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/wm"
	model "LAPP_ACURA_MOM_BACKEND/models/wm"
)

/******************************************************************************
 *
 * @Description              : AssertArtLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfAssertArtLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.AssertArtLst_AssertId.UrlParameterName:   grmi.NewConditionItem(meta.AssertArtLst_AssertId, grmi.Equal, true),
		meta.AssertArtLst_ArtId.UrlParameterName:      grmi.NewConditionItem(meta.AssertArtLst_ArtId, grmi.Equal, true),
		meta.AssertArtLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.AssertArtLst_Pos, grmi.Equal, false),
		meta.AssertArtLst_Qty.UrlParameterName:        grmi.NewConditionItem(meta.AssertArtLst_Qty, grmi.Equal, false),
		meta.AssertArtLst_Uom.UrlParameterName:        grmi.NewConditionItem(meta.AssertArtLst_Uom, grmi.Equal, false),
		meta.AssertArtLst_CtrlLevel.UrlParameterName:  grmi.NewConditionItem(meta.AssertArtLst_CtrlLevel, grmi.Equal, false),
		meta.AssertArtLst_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.AssertArtLst_CtrlPara1, grmi.Equal, false),
		meta.AssertArtLst_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.AssertArtLst_CtrlPara2, grmi.Equal, false),
		meta.AssertArtLst_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.AssertArtLst_CtrlPara3, grmi.Equal, false),
		meta.AssertArtLst_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.AssertArtLst_CtrlPara4, grmi.Equal, false),
		meta.AssertArtLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.AssertArtLst_LastModify, grmi.Approximate, false),
		meta.AssertArtLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.AssertArtLst_LastUser, grmi.Equal, false),
		meta.AssertArtLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.AssertArtLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : AssertArtLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
var DefaultConditionOfAssertArtLstAndPaging = grmi.NewCondition(
	DefaultConditionOfAssertArtLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : AssertArtLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : AssertArtLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
type AssertArtLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewAssertArtLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个AssertArtLstDAOImplement实例
 *
 * @Return Value             : AssertArtLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-29 10:57:04
 *
 ******************************************************************************/
func NewAssertArtLstServiceImplement() *AssertArtLstServiceImplement {
	return &AssertArtLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/AssertArtLstService.InsertOne
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) InsertOne(user *global.User, entity *model.AssertArtLst) error {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "InsertOneAssertArtLst", "插入一个AssertArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssertArtLstDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.AssertId, entity.ArtId)
	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/wm/AssertArtLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) DeleteOne(user *global.User, assertId string, artId string) error {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "DeleteOneAssertArtLst", "删除一个AssertArtLst")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/AssertArtLstService.SelectOne
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) SelectOne(user *global.User, assertId string, artId string) (*model.AssertArtLst, error) {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "SelectOneAssertArtLst", "查询一个AssertArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssertArtLstDAO(session, user.UserId)
	result, err := dao.SelectOne(assertId, artId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/AssertArtLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) UpdateOne(user *global.User, entity *model.AssertArtLst) error {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "UpdateOneAssertArtLst", "修改一个AssertArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssertArtLstDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.AssertId, entity.ArtId)
	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/wm/AssertArtLstService.Insert
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) Insert(user *global.User, entities *model.ArticleAssertRequest) error {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "InsertAssertArtLst", "插入多个AssertArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewAssertArtLstDAO(session, user.UserId)
	artAssertLi := entities.AssertArtLstLi
	for _, entity := range artAssertLi {
		if entity.AssertId != entities.AssertId {
			return grmi.NewBusinessError("传入的assertId不一致")
		}
	}
	err := dao.DeleteWhere([]grmi.Predicate{meta.AssertArtLst_AssertId.NewPredicate(grmi.Equal, entities.AssertId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(&artAssertLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/AssertArtLstService.Delete
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) Delete(user *global.User, entities *[]model.AssertArtLst) error {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "DeleteAssertArtLst", "删除多个AssertArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssertArtLstDAO(session, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AssertId, entity.ArtId)
		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/wm/AssertArtLstService.Select
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "SelectAssertArtLst", "查询AssertArtLst")

	condition := DefaultConditionOfAssertArtLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewAssertArtLstDAO(session, 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_ACURA_MOM_BACKEND/services/wm/AssertArtLstService.Update
 *
 ******************************************************************************/
func (impl *AssertArtLstServiceImplement) Update(user *global.User, entities *[]model.AssertArtLst) error {

	grmi.Log(user, "/services/wm/implments/AssertArtLst.service.impl.go", "UpdateAssertArtLst", "修改多个AssertArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewAssertArtLstDAO(session, user.UserId)

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

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