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

package implments

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

/******************************************************************************
 *
 * @Description              : DemandHeadFromDB的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-13 16:05:11
 *
 ******************************************************************************/
var DefaultConditionOfDemandHeadFromDB = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.DemandHeadFromDB_TaskNr.UrlParameterName:     grmi.NewConditionItem(meta.DemandHeadFromDB_TaskNr, grmi.Equal, true),
		meta.DemandHeadFromDB_TaskDate.UrlParameterName:   grmi.NewConditionItem(meta.DemandHeadFromDB_TaskDate, grmi.Approximate, false),
		meta.DemandHeadFromDB_Status.UrlParameterName:     grmi.NewConditionItem(meta.DemandHeadFromDB_Status, grmi.Equal, false),
		meta.DemandHeadFromDB_ErrNum.UrlParameterName:     grmi.NewConditionItem(meta.DemandHeadFromDB_ErrNum, grmi.Equal, false),
		meta.DemandHeadFromDB_ErrInfo.UrlParameterName:    grmi.NewConditionItem(meta.DemandHeadFromDB_ErrInfo, grmi.Equal, false),
		meta.DemandHeadFromDB_LastModify.UrlParameterName: grmi.NewConditionItem(meta.DemandHeadFromDB_LastModify, grmi.Approximate, false),
		meta.DemandHeadFromDB_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.DemandHeadFromDB_LastUser, grmi.Equal, false),
		meta.DemandHeadFromDB_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.DemandHeadFromDB_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : DemandHeadFromDB的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-13 16:05:11
 *
 ******************************************************************************/
var DefaultConditionOfDemandHeadFromDBAndPaging = grmi.NewCondition(
	DefaultConditionOfDemandHeadFromDB.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : DemandHeadFromDBServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : DemandHeadFromDB的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-13 16:05:11
 *
 ******************************************************************************/
type DemandHeadFromDBServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewDemandHeadFromDBDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个DemandHeadFromDBDAOImplement实例
 *
 * @Return Value             : DemandHeadFromDBDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-13 16:05:11
 *
 ******************************************************************************/
func NewDemandHeadFromDBServiceImplement() *DemandHeadFromDBServiceImplement {
	return &DemandHeadFromDBServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/DemandHeadFromDBService.InsertOne
 *
 ******************************************************************************/
func (impl *DemandHeadFromDBServiceImplement) InsertOne(user *global.User, entity *model.DemandHeadFromDB) error {

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "InsertOneDemandHeadFromDB", "插入一个DemandHeadFromDB")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDemandHeadFromDBDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.TaskNr)
	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/om/DemandHeadFromDBService.DeleteOne
 *
 ******************************************************************************/
func (impl *DemandHeadFromDBServiceImplement) DeleteOne(user *global.User, taskNr int) error {

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "DeleteOneDemandHeadFromDB", "删除一个DemandHeadFromDB")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/DemandHeadFromDBService.SelectOne
 *
 ******************************************************************************/
func (impl *DemandHeadFromDBServiceImplement) SelectOne(user *global.User, taskNr int) (*model.DemandHeadFromDB, error) {

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "SelectOneDemandHeadFromDB", "查询一个DemandHeadFromDB")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/om/DemandHeadFromDBService.UpdateOne
 *
 ******************************************************************************/
func (impl *DemandHeadFromDBServiceImplement) UpdateOne(user *global.User, entity *model.DemandHeadFromDB) error {

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "UpdateOneDemandHeadFromDB", "修改一个DemandHeadFromDB")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewDemandHeadFromDBDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.TaskNr)
	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/om/DemandHeadFromDBService.Insert
 *
 ******************************************************************************/
func (impl *DemandHeadFromDBServiceImplement) Insert(user *global.User, entities *[]model.DemandHeadFromDB) error {

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "InsertDemandHeadFromDB", "插入多个DemandHeadFromDB")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.TaskNr)
		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/om/DemandHeadFromDBService.Delete
 *
 ******************************************************************************/
func (impl *DemandHeadFromDBServiceImplement) Delete(user *global.User, entities *[]model.DemandHeadFromDB) error {

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "DeleteDemandHeadFromDB", "删除多个DemandHeadFromDB")

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

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

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "SelectDemandHeadFromDB", "查询DemandHeadFromDB")

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

	grmi.Log(user, "/services/om/implments/DemandHeadFromDB.service.impl.go", "UpdateDemandHeadFromDB", "修改多个DemandHeadFromDB")

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

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

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