// 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              : CrossPlanTask的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-10 22:01:07
 *
 ******************************************************************************/
var DefaultConditionOfCrossPlanTask = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CrossPlanTask_TaskNr.UrlParameterName:         grmi.NewConditionItem(meta.CrossPlanTask_TaskNr, grmi.Equal, true),
		meta.CrossPlanTask_FromPlantNr.UrlParameterName:    grmi.NewConditionItem(meta.CrossPlanTask_FromPlantNr, grmi.Equal, false),
		meta.CrossPlanTask_ToPlantNr.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTask_ToPlantNr, grmi.Equal, false),
		meta.CrossPlanTask_ProjectId.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTask_ProjectId, grmi.Equal, false),
		meta.CrossPlanTask_ArtId.UrlParameterName:          grmi.NewConditionItem(meta.CrossPlanTask_ArtId, grmi.Equal, false),
		meta.CrossPlanTask_PlanQty.UrlParameterName:        grmi.NewConditionItem(meta.CrossPlanTask_PlanQty, grmi.Equal, false),
		meta.CrossPlanTask_ActQty.UrlParameterName:         grmi.NewConditionItem(meta.CrossPlanTask_ActQty, grmi.Equal, false),
		meta.CrossPlanTask_Status.UrlParameterName:         grmi.NewConditionItem(meta.CrossPlanTask_Status, grmi.Equal, false),
		meta.CrossPlanTask_OrderType.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTask_OrderType, grmi.Equal, false),
		meta.CrossPlanTask_PlanType.UrlParameterName:       grmi.NewConditionItem(meta.CrossPlanTask_PlanType, grmi.Equal, false),
		meta.CrossPlanTask_SchedType.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTask_SchedType, grmi.Equal, false),
		meta.CrossPlanTask_SourceOrderId.UrlParameterName:  grmi.NewConditionItem(meta.CrossPlanTask_SourceOrderId, grmi.Equal, false),
		meta.CrossPlanTask_PlanStartTime.UrlParameterName:  grmi.NewConditionItem(meta.CrossPlanTask_PlanStartTime, grmi.Approximate, false),
		meta.CrossPlanTask_PlanEndTime.UrlParameterName:    grmi.NewConditionItem(meta.CrossPlanTask_PlanEndTime, grmi.Approximate, false),
		meta.CrossPlanTask_Priority.UrlParameterName:       grmi.NewConditionItem(meta.CrossPlanTask_Priority, grmi.Equal, false),
		meta.CrossPlanTask_LastPo.UrlParameterName:         grmi.NewConditionItem(meta.CrossPlanTask_LastPo, grmi.Equal, false),
		meta.CrossPlanTask_ReleaseQty.UrlParameterName:     grmi.NewConditionItem(meta.CrossPlanTask_ReleaseQty, grmi.Equal, false),
		meta.CrossPlanTask_StartTime.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTask_StartTime, grmi.Approximate, false),
		meta.CrossPlanTask_AccomplishTime.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTask_AccomplishTime, grmi.Approximate, false),
		meta.CrossPlanTask_ErrorMsg.UrlParameterName:       grmi.NewConditionItem(meta.CrossPlanTask_ErrorMsg, grmi.Equal, false),
		meta.CrossPlanTask_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.CrossPlanTask_LastModify, grmi.Approximate, false),
		meta.CrossPlanTask_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.CrossPlanTask_LastUser, grmi.Equal, false),
		meta.CrossPlanTask_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.CrossPlanTask_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CrossPlanTask的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-10 22:01:07
 *
 ******************************************************************************/
var DefaultConditionOfCrossPlanTaskAndPaging = grmi.NewCondition(
	DefaultConditionOfCrossPlanTask.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : CrossPlanTaskServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CrossPlanTask的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-10 22:01:07
 *
 ******************************************************************************/
type CrossPlanTaskServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCrossPlanTaskDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CrossPlanTaskDAOImplement实例
 *
 * @Return Value             : CrossPlanTaskDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-10 22:01:07
 *
 ******************************************************************************/
func NewCrossPlanTaskServiceImplement() *CrossPlanTaskServiceImplement {
	return &CrossPlanTaskServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/CrossPlanTaskService.InsertOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) InsertOne(user *global.User, entity *model.CrossPlanTask) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCrossPlanTaskDAO(session, 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/base/CrossPlanTaskService.DeleteOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) DeleteOne(user *global.User, taskNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCrossPlanTaskDAO(session, 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/base/CrossPlanTaskService.SelectOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) SelectOne(user *global.User, taskNr int) (*model.CrossPlanTask, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/CrossPlanTaskService.UpdateOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) UpdateOne(user *global.User, entity *model.CrossPlanTask) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCrossPlanTaskDAO(session, 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/base/CrossPlanTaskService.Insert
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) Insert(user *global.User, entities *[]model.CrossPlanTask) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCrossPlanTaskDAO(session, 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/base/CrossPlanTaskService.Delete
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) Delete(user *global.User, entities *[]model.CrossPlanTask) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCrossPlanTaskDAO(session, 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/base/CrossPlanTaskService.Select
 *
 ******************************************************************************/
func (impl *CrossPlanTaskServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.CrossPlanTask, error) {

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

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

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

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

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewCrossPlanTaskDAO(session, 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
}
