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

package implments

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

/******************************************************************************
 *
 * @Description              : VendorArtLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-25 10:13:02
 *
 ******************************************************************************/
var DefaultConditionOfVendorArtLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.VendorArtLst_VendorId.UrlParameterName:   grmi.NewConditionItem(meta.VendorArtLst_VendorId, grmi.Equal, true),
		meta.VendorArtLst_ArtId.UrlParameterName:      grmi.NewConditionItem(meta.VendorArtLst_ArtId, grmi.Equal, true),
		meta.VendorArtLst_Pos.UrlParameterName:        grmi.NewConditionItem(meta.VendorArtLst_Pos, grmi.Equal, false),
		meta.VendorArtLst_CtrlLevel.UrlParameterName:  grmi.NewConditionItem(meta.VendorArtLst_CtrlLevel, grmi.Equal, false),
		meta.VendorArtLst_CtrlPara1.UrlParameterName:  grmi.NewConditionItem(meta.VendorArtLst_CtrlPara1, grmi.Equal, false),
		meta.VendorArtLst_CtrlPara2.UrlParameterName:  grmi.NewConditionItem(meta.VendorArtLst_CtrlPara2, grmi.Equal, false),
		meta.VendorArtLst_CtrlPara3.UrlParameterName:  grmi.NewConditionItem(meta.VendorArtLst_CtrlPara3, grmi.Equal, false),
		meta.VendorArtLst_CtrlPara4.UrlParameterName:  grmi.NewConditionItem(meta.VendorArtLst_CtrlPara4, grmi.Equal, false),
		meta.VendorArtLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.VendorArtLst_LastModify, grmi.Approximate, false),
		meta.VendorArtLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.VendorArtLst_LastUser, grmi.Equal, false),
		meta.VendorArtLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.VendorArtLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : VendorArtLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-25 10:13:02
 *
 ******************************************************************************/
var DefaultConditionOfVendorArtLstAndPaging = grmi.NewCondition(
	DefaultConditionOfVendorArtLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : VendorArtLstServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : VendorArtLst的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-25 10:13:02
*
******************************************************************************/
type VendorArtLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewVendorArtLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个VendorArtLstDAOImplement实例
 *
 * @Return Value             : VendorArtLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-25 10:13:02
 *
 ******************************************************************************/
func NewVendorArtLstServiceImplement() *VendorArtLstServiceImplement {
	return &VendorArtLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorArtLstService.InsertOne
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) InsertOne(user *models.Usertab, entity *model.VendorArtLst) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "InsertOneVendorArtLst", "插入一个VendorArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorArtLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.VendorId, entity.ArtId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	vendorDao := dal.NewVendorDAO(session, user.Pid, user.Userid)
	vendor, err := vendorDao.SelectOne(entity.VendorId)
	if err != nil {
		return err
	}
	if vendor == nil {
		return grmi.NewBusinessError("不存在对应的供应商，请检查")
	}
	articleDao := artDal.NewArticleDAO(session, user.Pid, user.Userid)
	article, err := articleDao.SelectOne(entity.ArtId)
	if err != nil {
		return err
	}
	if article == nil {
		return grmi.NewBusinessError("不存在对应的物料，请检查")
	}
	pos, err := dao.GetMaxPos(entity.VendorId)
	if err != nil {
		return err
	}
	entity.Pos = pos + 1
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorArtLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) DeleteOne(user *models.Usertab, vendorId string, artId string) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "DeleteOneVendorArtLst", "删除一个VendorArtLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorArtLstService.SelectOne
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) SelectOne(user *models.Usertab, vendorId string, artId string) (*model.VendorArtLst, error) {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "SelectOneVendorArtLst", "查询一个VendorArtLst")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorArtLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.VendorArtLst) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "UpdateOneVendorArtLst", "修改一个VendorArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorArtLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.VendorId, entity.ArtId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	entity.Pos = record.Pos
	entity.VendorId = record.VendorId
	entity.ArtId = record.ArtId
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorArtLstService.Insert
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) Insert(user *models.Usertab, insertItem *model.VendorArtLstInsert) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "InsertVendorArtLst", "插入多个VendorArtLst")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewVendorArtLstDAO(session, user.Pid, user.Userid)
	vendorDao := dal.NewVendorDAO(session, user.Pid, user.Userid)
	articleDao := artDal.NewArticleDAO(session, user.Pid, user.Userid)
 	entities := make([]model.VendorArtLst, len(insertItem.Records))
	for index, entity := range insertItem.Records {
		vendor, err := vendorDao.SelectOne(entity.VendorId)
		if err != nil {
			return err
		}
		if vendor == nil {
			return grmi.NewBusinessError("不存在对应的供应商，请检查")
		}
		article, err := articleDao.SelectOne(entity.ArtId)
		if err != nil {
			return err
		}
		if article == nil {
			return grmi.NewBusinessError("不存在对应的物料，请检查")
		}
		if entity.VendorId != insertItem.VendorId {
			return grmi.NewBusinessError("插入的数据与设置的VendorId不相符")
		}
		entity.Pos = index + 1
		entities[index] = entity
	}
	//fmt.Println("entity:", entities)
	err = dao.DeleteWhere([]grmi.Predicate{
		meta.VendorArtLst_VendorId.NewPredicate(grmi.Equal, insertItem.VendorId),
	})
	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/log/VendorArtLstService.Delete
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) Delete(user *models.Usertab, entities *[]model.VendorArtLst) error {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "DeleteVendorArtLst", "删除多个VendorArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewVendorArtLstDAO(session, user.Pid, user.Userid)

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

	err = dao.Delete(entities)
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/VendorArtLstService.Select
 *
 ******************************************************************************/
func (impl *VendorArtLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "SelectVendorArtLst", "查询VendorArtLst")

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

	grmi.Log(user, "/services/log/implments/LOG_VendorArtLst.service.impl.go", "UpdateVendorArtLst", "修改多个VendorArtLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewVendorArtLstDAO(session, user.Pid, user.Userid)

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

	err = dao.Update(entities)
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}
