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

package implments

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

/******************************************************************************
 *
 * @Description              : StatusColor的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfStatusColor = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.StatusColor_StatusObj.UrlParameterName:      grmi.NewConditionItem(meta.StatusColor_StatusObj, grmi.Equal, true),
		meta.StatusColor_StatusVal.UrlParameterName:      grmi.NewConditionItem(meta.StatusColor_StatusVal, grmi.Equal, true),
		meta.StatusColor_Descr.UrlParameterName:          grmi.NewConditionItem(meta.StatusColor_Descr, grmi.Equal, false),
		meta.StatusColor_StatusRgb.UrlParameterName:      grmi.NewConditionItem(meta.StatusColor_StatusRgb, grmi.Equal, false),
		meta.StatusColor_StatusHexColor.UrlParameterName: grmi.NewConditionItem(meta.StatusColor_StatusHexColor, grmi.Equal, false),
		meta.StatusColor_StatusBitmap.UrlParameterName:   grmi.NewConditionItem(meta.StatusColor_StatusBitmap, grmi.Equal, false),
		meta.StatusColor_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.StatusColor_LastModify, grmi.Approximate, false),
		meta.StatusColor_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.StatusColor_LastUser, grmi.Equal, false),
		meta.StatusColor_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.StatusColor_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : StatusColor的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfStatusColorAndPaging = grmi.NewCondition(
	DefaultConditionOfStatusColor.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : StatusColorServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : StatusColor的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
type StatusColorServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewStatusColorDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个StatusColorDAOImplement实例
 *
 * @Return Value             : StatusColorDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
func NewStatusColorServiceImplement() *StatusColorServiceImplement {
	return &StatusColorServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StatusColorService.InsertOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) InsertOne(user *global.User, entity *model.StatusColor) error {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "InsertOneStatusColor", "插入一个StatusColor")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStatusColorDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.StatusObj, entity.StatusVal)
	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/base/StatusColorService.DeleteOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) DeleteOne(user *global.User, statusObj string, statusVal string) error {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "DeleteOneStatusColor", "删除一个StatusColor")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StatusColorService.SelectOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) SelectOne(user *global.User, statusObj string, statusVal string) (*model.StatusColor, error) {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "SelectOneStatusColor", "查询一个StatusColor")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StatusColorService.UpdateOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) UpdateOne(user *global.User, entity *model.StatusColor) error {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "UpdateOneStatusColor", "修改一个StatusColor")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStatusColorDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.StatusObj, entity.StatusVal)
	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/base/StatusColorService.Insert
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Insert(user *global.User, entities *[]model.StatusColor) error {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "InsertStatusColor", "插入多个StatusColor")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StatusObj, entity.StatusVal)
		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/base/StatusColorService.Delete
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Delete(user *global.User, entities *[]model.StatusColor) error {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "DeleteStatusColor", "删除多个StatusColor")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StatusObj, entity.StatusVal)
		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/base/StatusColorService.Select
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.StatusColor, error) {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "SelectStatusColor", "查询StatusColor")

	condition := DefaultConditionOfStatusColorAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewStatusColorDAO(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/base/StatusColorService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "SelectStatusColor", "查询StatusColor")

	condition := DefaultConditionOfStatusColorAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewStatusColorDAO(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/base/StatusColorService.Update
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Update(user *global.User, entities *[]model.StatusColor) error {

	grmi.Log(user, "/services/base/implments/StatusColor.service.impl.go", "UpdateStatusColor", "修改多个StatusColor")

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

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

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