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

package implments

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

/******************************************************************************
 *
 * @Description              : CustOrderStatusHistory的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderStatusHistory = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CustOrderStatusHistory_CustOrderId.UrlParameterName:      grmi.NewConditionItem(meta.CustOrderStatusHistory_CustOrderId, grmi.Equal, true),
		meta.CustOrderStatusHistory_Pos.UrlParameterName:              grmi.NewConditionItem(meta.CustOrderStatusHistory_Pos, grmi.Equal, true),
		meta.CustOrderStatusHistory_StatusChangeType.UrlParameterName: grmi.NewConditionItem(meta.CustOrderStatusHistory_StatusChangeType, grmi.Equal, false),
		meta.CustOrderStatusHistory_FromStatus.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderStatusHistory_FromStatus, grmi.Equal, false),
		meta.CustOrderStatusHistory_ToStatus.UrlParameterName:         grmi.NewConditionItem(meta.CustOrderStatusHistory_ToStatus, grmi.Equal, false),
		meta.CustOrderStatusHistory_TriggerEvent.UrlParameterName:     grmi.NewConditionItem(meta.CustOrderStatusHistory_TriggerEvent, grmi.Equal, false),
		meta.CustOrderStatusHistory_TriggerObjId.UrlParameterName:     grmi.NewConditionItem(meta.CustOrderStatusHistory_TriggerObjId, grmi.Equal, false),
		meta.CustOrderStatusHistory_ChangeTime.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderStatusHistory_ChangeTime, grmi.Approximate, false),
		meta.CustOrderStatusHistory_TriggerPara1.UrlParameterName:     grmi.NewConditionItem(meta.CustOrderStatusHistory_TriggerPara1, grmi.Equal, false),
		meta.CustOrderStatusHistory_TriggerPara2.UrlParameterName:     grmi.NewConditionItem(meta.CustOrderStatusHistory_TriggerPara2, grmi.Equal, false),
		meta.CustOrderStatusHistory_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderStatusHistory_LastModify, grmi.Approximate, false),
		meta.CustOrderStatusHistory_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.CustOrderStatusHistory_LastUser, grmi.Equal, false),
		meta.CustOrderStatusHistory_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.CustOrderStatusHistory_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CustOrderStatusHistory的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
var DefaultConditionOfCustOrderStatusHistoryAndPaging = grmi.NewCondition(
	DefaultConditionOfCustOrderStatusHistory.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : CustOrderStatusHistoryServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CustOrderStatusHistory的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
type CustOrderStatusHistoryServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCustOrderStatusHistoryDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CustOrderStatusHistoryDAOImplement实例
 *
 * @Return Value             : CustOrderStatusHistoryDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-10-22 13:50:40
 *
 ******************************************************************************/
func NewCustOrderStatusHistoryServiceImplement() *CustOrderStatusHistoryServiceImplement {
	return &CustOrderStatusHistoryServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderStatusHistoryService.InsertOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) InsertOne(user *global.User, entity *model.CustOrderStatusHistory) error {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "InsertOneCustOrderStatusHistory", "插入一个CustOrderStatusHistory")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusHistoryDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CustOrderId, entity.Pos)
	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/pln/CustOrderStatusHistoryService.DeleteOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) DeleteOne(user *global.User, custOrderId string, pos int) error {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "DeleteOneCustOrderStatusHistory", "删除一个CustOrderStatusHistory")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderStatusHistoryService.SelectOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) SelectOne(user *global.User, custOrderId string, pos int) (*model.CustOrderStatusHistory, error) {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "SelectOneCustOrderStatusHistory", "查询一个CustOrderStatusHistory")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderStatusHistoryService.UpdateOne
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) UpdateOne(user *global.User, entity *model.CustOrderStatusHistory) error {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "UpdateOneCustOrderStatusHistory", "修改一个CustOrderStatusHistory")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCustOrderStatusHistoryDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.CustOrderId, entity.Pos)
	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/pln/CustOrderStatusHistoryService.Insert
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) Insert(user *global.User, entities *[]model.CustOrderStatusHistory) error {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "InsertCustOrderStatusHistory", "插入多个CustOrderStatusHistory")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CustOrderId, entity.Pos)
		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/pln/CustOrderStatusHistoryService.Delete
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) Delete(user *global.User, entities *[]model.CustOrderStatusHistory) error {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "DeleteCustOrderStatusHistory", "删除多个CustOrderStatusHistory")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.CustOrderId, entity.Pos)
		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/pln/CustOrderStatusHistoryService.Select
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.CustOrderStatusHistory, error) {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "SelectCustOrderStatusHistory", "查询CustOrderStatusHistory")

	condition := DefaultConditionOfCustOrderStatusHistoryAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewCustOrderStatusHistoryDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderStatusHistoryService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "SelectCustOrderStatusHistory", "查询CustOrderStatusHistory")

	condition := DefaultConditionOfCustOrderStatusHistoryAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewCustOrderStatusHistoryDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/pln/CustOrderStatusHistoryService.Update
 *
 ******************************************************************************/
func (impl *CustOrderStatusHistoryServiceImplement) Update(user *global.User, entities *[]model.CustOrderStatusHistory) error {

	grmi.Log(user, "/services/pln/implments/CustOrderStatusHistory.service.impl.go", "UpdateCustOrderStatusHistory", "修改多个CustOrderStatusHistory")

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

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

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