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

package implments

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

/******************************************************************************
 *
 * @Description              : StatusColor的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
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-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfStatusColorAndPaging = grmi.NewCondition(
	DefaultConditionOfStatusColor.Items,
	grmi.NewPaging(),
)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/StatusColorService.InsertOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) InsertOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/StatusColorService.DeleteOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) DeleteOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/StatusColorService.SelectOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) SelectOne(user *models.Usertab, 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.Pid, user.Userid)
	result, err := dao.SelectOne(statusObj, statusVal)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/StatusColorService.UpdateOne
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) UpdateOne(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/StatusColorService.Insert
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Insert(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/StatusColorService.Delete
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Delete(user *models.Usertab, 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.Pid, 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_LF_MOM_BACKEND/services/base/StatusColorService.Select
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, 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.Pid, 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/base/StatusColorService.Update
 *
 ******************************************************************************/
func (impl *StatusColorServiceImplement) Update(user *models.Usertab, 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.Pid, 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
}
