// 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              : Client的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 11:04:46
 *
 ******************************************************************************/
var DefaultConditionOfClient = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Client_ClientId.UrlParameterName:       grmi.NewConditionItem(meta.Client_ClientId, grmi.Equal, true),
		meta.Client_Descr.UrlParameterName:          grmi.NewConditionItem(meta.Client_Descr, grmi.Equal, false),
		meta.Client_Address.UrlParameterName:        grmi.NewConditionItem(meta.Client_Address, grmi.Equal, false),
		meta.Client_Name.UrlParameterName:           grmi.NewConditionItem(meta.Client_Name, grmi.Equal, false),
		meta.Client_Name1.UrlParameterName:          grmi.NewConditionItem(meta.Client_Name1, grmi.Equal, false),
		meta.Client_Name2.UrlParameterName:          grmi.NewConditionItem(meta.Client_Name2, grmi.Equal, false),
		meta.Client_ValidStartDate.UrlParameterName: grmi.NewConditionItem(meta.Client_ValidStartDate, grmi.Approximate, false),
		meta.Client_Country.UrlParameterName:        grmi.NewConditionItem(meta.Client_Country, grmi.Equal, false),
		meta.Client_City.UrlParameterName:           grmi.NewConditionItem(meta.Client_City, grmi.Equal, false),
		meta.Client_District.UrlParameterName:       grmi.NewConditionItem(meta.Client_District, grmi.Equal, false),
		meta.Client_Street.UrlParameterName:         grmi.NewConditionItem(meta.Client_Street, grmi.Equal, false),
		meta.Client_Location.UrlParameterName:       grmi.NewConditionItem(meta.Client_Location, grmi.Equal, false),
		meta.Client_PostCode.UrlParameterName:       grmi.NewConditionItem(meta.Client_PostCode, grmi.Equal, false),
		meta.Client_Phone.UrlParameterName:          grmi.NewConditionItem(meta.Client_Phone, grmi.Equal, false),
		meta.Client_Mobile.UrlParameterName:         grmi.NewConditionItem(meta.Client_Mobile, grmi.Equal, false),
		meta.Client_Fax.UrlParameterName:            grmi.NewConditionItem(meta.Client_Fax, grmi.Equal, false),
		meta.Client_Email1.UrlParameterName:         grmi.NewConditionItem(meta.Client_Email1, grmi.Equal, false),
		meta.Client_Email2.UrlParameterName:         grmi.NewConditionItem(meta.Client_Email2, grmi.Equal, false),
		meta.Client_ContactPerson.UrlParameterName:  grmi.NewConditionItem(meta.Client_ContactPerson, grmi.Equal, false),
		meta.Client_PersonTitle.UrlParameterName:    grmi.NewConditionItem(meta.Client_PersonTitle, grmi.Equal, false),
		meta.Client_ContactAddress.UrlParameterName: grmi.NewConditionItem(meta.Client_ContactAddress, grmi.Equal, false),
		meta.Client_Account.UrlParameterName:        grmi.NewConditionItem(meta.Client_Account, grmi.Equal, false),
		meta.Client_TaxRate.UrlParameterName:        grmi.NewConditionItem(meta.Client_TaxRate, grmi.Equal, false),
		meta.Client_Status.UrlParameterName:         grmi.NewConditionItem(meta.Client_Status, grmi.Equal, false),
		meta.Client_Province.UrlParameterName:       grmi.NewConditionItem(meta.Client_Province, grmi.Equal, false),
		meta.Client_ProvinceCode.UrlParameterName:   grmi.NewConditionItem(meta.Client_ProvinceCode, grmi.Equal, false),
		meta.Client_CityCode.UrlParameterName:       grmi.NewConditionItem(meta.Client_CityCode, grmi.Equal, false),
		meta.Client_DistrictCode.UrlParameterName:   grmi.NewConditionItem(meta.Client_DistrictCode, grmi.Equal, false),
		meta.Client_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.Client_LastModify, grmi.Approximate, false),
		meta.Client_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.Client_LastUser, grmi.Equal, false),
		meta.Client_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.Client_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Client的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-21 11:04:46
 *
 ******************************************************************************/
var DefaultConditionOfClientAndPaging = grmi.NewCondition(
	DefaultConditionOfClient.Items,
	grmi.NewPaging(),
)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/ClientService.InsertOne
 *
 ******************************************************************************/
func (impl *ClientServiceImplement) InsertOne(user *global.User, entity *model.Client) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewClientDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.ClientId)
	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/ClientService.DeleteOne
 *
 ******************************************************************************/
func (impl *ClientServiceImplement) DeleteOne(user *global.User, clientId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/ClientService.SelectOne
 *
 ******************************************************************************/
func (impl *ClientServiceImplement) SelectOne(user *global.User, clientId string) (*model.Client, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/ClientService.UpdateOne
 *
 ******************************************************************************/
func (impl *ClientServiceImplement) UpdateOne(user *global.User, entity *model.Client) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewClientDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.ClientId)
	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/ClientService.Insert
 *
 ******************************************************************************/
func (impl *ClientServiceImplement) Insert(user *global.User, entities *[]model.Client) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/wm/ClientService.Delete
 *
 ******************************************************************************/
func (impl *ClientServiceImplement) Delete(user *global.User, entities *[]model.Client) error {

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

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

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

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

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

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

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

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

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