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

package implments

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

/******************************************************************************
 *
 * @Description              : Vendor的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-18 22:50:49
 *
 ******************************************************************************/
var DefaultConditionOfVendor = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Vendor_VendorId.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_VendorId, grmi.Equal, true),
		meta.Vendor_Descr.UrlParameterName:          grmi.NewConditionItem(meta.Vendor_Descr, grmi.Equal, false),
		meta.Vendor_Address.UrlParameterName:        grmi.NewConditionItem(meta.Vendor_Address, grmi.Equal, false),
		meta.Vendor_VendorNr.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_VendorNr, grmi.Equal, false),
		meta.Vendor_Name.UrlParameterName:           grmi.NewConditionItem(meta.Vendor_Name, grmi.Equal, false),
		meta.Vendor_Name1.UrlParameterName:          grmi.NewConditionItem(meta.Vendor_Name1, grmi.Equal, false),
		meta.Vendor_Name2.UrlParameterName:          grmi.NewConditionItem(meta.Vendor_Name2, grmi.Equal, false),
		meta.Vendor_ValidStartDate.UrlParameterName: grmi.NewConditionItem(meta.Vendor_ValidStartDate, grmi.Approximate, false),
		meta.Vendor_Country.UrlParameterName:        grmi.NewConditionItem(meta.Vendor_Country, grmi.Equal, false),
		meta.Vendor_City.UrlParameterName:           grmi.NewConditionItem(meta.Vendor_City, grmi.Equal, false),
		meta.Vendor_District.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_District, grmi.Equal, false),
		meta.Vendor_Street.UrlParameterName:         grmi.NewConditionItem(meta.Vendor_Street, grmi.Equal, false),
		meta.Vendor_Location.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_Location, grmi.Equal, false),
		meta.Vendor_PostCode.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_PostCode, grmi.Equal, false),
		meta.Vendor_Phone.UrlParameterName:          grmi.NewConditionItem(meta.Vendor_Phone, grmi.Equal, false),
		meta.Vendor_Mobile.UrlParameterName:         grmi.NewConditionItem(meta.Vendor_Mobile, grmi.Equal, false),
		meta.Vendor_Fax.UrlParameterName:            grmi.NewConditionItem(meta.Vendor_Fax, grmi.Equal, false),
		meta.Vendor_Email1.UrlParameterName:         grmi.NewConditionItem(meta.Vendor_Email1, grmi.Equal, false),
		meta.Vendor_Email2.UrlParameterName:         grmi.NewConditionItem(meta.Vendor_Email2, grmi.Equal, false),
		meta.Vendor_ContactPerson.UrlParameterName:  grmi.NewConditionItem(meta.Vendor_ContactPerson, grmi.Equal, false),
		meta.Vendor_PersonTitle.UrlParameterName:    grmi.NewConditionItem(meta.Vendor_PersonTitle, grmi.Equal, false),
		meta.Vendor_ContactAddress.UrlParameterName: grmi.NewConditionItem(meta.Vendor_ContactAddress, grmi.Equal, false),
		meta.Vendor_Account.UrlParameterName:        grmi.NewConditionItem(meta.Vendor_Account, grmi.Equal, false),
		meta.Vendor_TaxRate.UrlParameterName:        grmi.NewConditionItem(meta.Vendor_TaxRate, grmi.Equal, false),
		meta.Vendor_Status.UrlParameterName:         grmi.NewConditionItem(meta.Vendor_Status, grmi.Equal, false),
		meta.Vendor_Province.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_Province, grmi.Equal, false),
		meta.Vendor_ProvinceCode.UrlParameterName:   grmi.NewConditionItem(meta.Vendor_ProvinceCode, grmi.Equal, false),
		meta.Vendor_CityCode.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_CityCode, grmi.Equal, false),
		meta.Vendor_DistrictCode.UrlParameterName:   grmi.NewConditionItem(meta.Vendor_DistrictCode, grmi.Equal, false),
		meta.Vendor_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.Vendor_LastModify, grmi.Approximate, false),
		meta.Vendor_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.Vendor_LastUser, grmi.Equal, false),
		meta.Vendor_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.Vendor_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/log/VendorService.InsertOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) InsertOne(user *global.User, entity *model.Vendor) error {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "InsertOneVendor", "插入一个Vendor")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.VendorId)
	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/log/VendorService.DeleteOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) DeleteOne(user *global.User, vendorId string) error {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "DeleteOneVendor", "删除一个Vendor")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewVendorDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(vendorId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(vendorId)
	if err != nil {
		session.Rollback()
		return err
	}
	vendorArtLstDao := dal.NewVendorArtLstDAO(session, user.PlantNr, user.UserId)
	err = vendorArtLstDao.DeleteByVendor([]string{vendorId})
	if err != nil {
		session.Rollback()
		return err
	}
	vendorUserLst := dal.NewVendorUserLstDAO(session, user.PlantNr, user.UserId)
	err = vendorUserLst.DeleteByVendor([]string{vendorId})
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/log/VendorService.SelectOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) SelectOne(user *global.User, vendorId string) (*model.Vendor, error) {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "SelectOneVendor", "查询一个Vendor")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(vendorId)
	if err != nil {
		return nil, err
	}
	artDao := dal.NewVendorArtLstDAO(session, user.PlantNr, user.UserId)
	artList, err := artDao.Select([]grmi.Predicate{meta.VendorArtLst_VendorId.NewPredicate(grmi.Equal, vendorId)}, nil)
	if err != nil {
		return nil, err
	}
	userDao := dal.NewVendorUserLstDAO(session, user.PlantNr, user.UserId)
	userList, err := userDao.Select([]grmi.Predicate{meta.VendorUserLst_VendorId.NewPredicate(grmi.Equal, vendorId)}, nil)
	if err != nil {
		return nil, err
	}
	result.UserList = userList
	result.ArtList = artList
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/log/VendorService.UpdateOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) UpdateOne(user *global.User, entity *model.Vendor) error {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "UpdateOneVendor", "修改一个Vendor")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/log/VendorService.Insert
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Insert(user *global.User, entities *[]model.Vendor) error {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "InsertVendor", "插入多个Vendor")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.VendorId)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

	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/log/VendorService.Delete
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Delete(user *global.User, entities *[]model.Vendor) error {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "DeleteVendor", "删除多个Vendor")

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

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

	err = dao.Delete(entities)
	if err != nil {
		session.Rollback()
		return err
	}
	vendors := make([]string, len(*entities))
	for index, entity := range *entities {
		vendors[index] = entity.VendorId
	}
	vendorArtLstDao := dal.NewVendorArtLstDAO(session, user.PlantNr, user.UserId)
	err = vendorArtLstDao.DeleteByVendor(vendors)
	if err != nil {
		session.Rollback()
		return err
	}
	vendorUserLstDao := dal.NewVendorUserLstDAO(session, user.PlantNr, user.UserId)
	err = vendorUserLstDao.DeleteByVendor(vendors)
	if err != nil {
		session.Rollback()
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/log/VendorService.Select
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "SelectVendor", "查询Vendor")

	condition := DefaultConditionOfVendorAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewVendorDAO(session, user.PlantNr, 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/log/VendorService.Update
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Update(user *global.User, entities *[]model.Vendor) error {

	grmi.Log(user, "/services/log/implments/LOG_Vendor.service.impl.go", "UpdateVendor", "修改多个Vendor")

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

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

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