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

package implments

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

/******************************************************************************
 *
 * @Description              : Vendor的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 11:04:46
 *
 ******************************************************************************/
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_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-07-21 11:04:46
 *
 ******************************************************************************/
var DefaultConditionOfVendorAndPaging = grmi.NewCondition(
	DefaultConditionOfVendor.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : VendorServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Vendor的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 11:04:46
 *
 ******************************************************************************/
type VendorServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewVendorDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个VendorDAOImplement实例
 *
 * @Return Value             : VendorDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 11:04:46
 *
 ******************************************************************************/
func NewVendorServiceImplement() *VendorServiceImplement {
	return &VendorServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/VendorService.InsertOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) InsertOne(user *models.Usertab, entity *model.Vendor) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, 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_LF_MOM_BACKEND/services/wm/VendorService.DeleteOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) DeleteOne(user *models.Usertab, vendorId string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, 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 {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/VendorService.SelectOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) SelectOne(user *models.Usertab, vendorId string) (*model.Vendor, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/VendorService.UpdateOne
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) UpdateOne(user *models.Usertab, entity *model.Vendor) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/VendorService.Insert
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Insert(user *models.Usertab, entities *[]model.Vendor) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, 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 {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/wm/VendorService.Delete
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Delete(user *models.Usertab, entities *[]model.Vendor) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, 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 {
		return err
	}
	return nil
}

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

	grmi.Log(user, "/services/wm/implments/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.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/wm/VendorService.Update
 *
 ******************************************************************************/
func (impl *VendorServiceImplement) Update(user *models.Usertab, entities *[]model.Vendor) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewVendorDAO(session, 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.Update(entities)
	if err != nil {
		return err
	}
	return nil
}
