// 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              : CrossPlanTaskDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-19 17:51:14
 *
 ******************************************************************************/
var DefaultConditionOfCrossPlanTaskDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.CrossPlanTaskDetail_TaskNr.UrlParameterName:          grmi.NewConditionItem(meta.CrossPlanTaskDetail_TaskNr, grmi.Equal, true),
		meta.CrossPlanTaskDetail_ItemNr.UrlParameterName:          grmi.NewConditionItem(meta.CrossPlanTaskDetail_ItemNr, grmi.Equal, true),
		meta.CrossPlanTaskDetail_ArtId.UrlParameterName:           grmi.NewConditionItem(meta.CrossPlanTaskDetail_ArtId, grmi.Equal, false),
		meta.CrossPlanTaskDetail_Status.UrlParameterName:          grmi.NewConditionItem(meta.CrossPlanTaskDetail_Status, grmi.Equal, false),
		meta.CrossPlanTaskDetail_BusinessObjType.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_BusinessObjType, grmi.Equal, false),
		meta.CrossPlanTaskDetail_BusinessObjId.UrlParameterName:   grmi.NewConditionItem(meta.CrossPlanTaskDetail_BusinessObjId, grmi.Equal, false),
		meta.CrossPlanTaskDetail_AccomplishTime.UrlParameterName:  grmi.NewConditionItem(meta.CrossPlanTaskDetail_AccomplishTime, grmi.Approximate, false),
		meta.CrossPlanTaskDetail_ErrorMsg.UrlParameterName:        grmi.NewConditionItem(meta.CrossPlanTaskDetail_ErrorMsg, grmi.Equal, false),
		meta.CrossPlanTaskDetail_TranferStrPara1.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_TranferStrPara1, grmi.Equal, false),
		meta.CrossPlanTaskDetail_TranferStrPara2.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_TranferStrPara2, grmi.Equal, false),
		meta.CrossPlanTaskDetail_TranferStrPara3.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_TranferStrPara3, grmi.Equal, false),
		meta.CrossPlanTaskDetail_TranferIntPara1.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_TranferIntPara1, grmi.Equal, false),
		meta.CrossPlanTaskDetail_TranferIntPara2.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_TranferIntPara2, grmi.Equal, false),
		meta.CrossPlanTaskDetail_TranferIntPara3.UrlParameterName: grmi.NewConditionItem(meta.CrossPlanTaskDetail_TranferIntPara3, grmi.Equal, false),
		meta.CrossPlanTaskDetail_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTaskDetail_LastModify, grmi.Approximate, false),
		meta.CrossPlanTaskDetail_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.CrossPlanTaskDetail_LastUser, grmi.Equal, false),
		meta.CrossPlanTaskDetail_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.CrossPlanTaskDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : CrossPlanTaskDetail的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-19 17:51:14
 *
 ******************************************************************************/
var DefaultConditionOfCrossPlanTaskDetailAndPaging = grmi.NewCondition(
	DefaultConditionOfCrossPlanTaskDetail.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : CrossPlanTaskDetailServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : CrossPlanTaskDetail的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-19 17:51:14
 *
 ******************************************************************************/
type CrossPlanTaskDetailServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewCrossPlanTaskDetailDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个CrossPlanTaskDetailDAOImplement实例
 *
 * @Return Value             : CrossPlanTaskDetailDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2022-05-19 17:51:14
 *
 ******************************************************************************/
func NewCrossPlanTaskDetailServiceImplement() *CrossPlanTaskDetailServiceImplement {
	return &CrossPlanTaskDetailServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/CrossPlanTaskDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskDetailServiceImplement) InsertOne(user *global.User, entity *model.CrossPlanTaskDetail) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/CrossPlanTaskDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskDetailServiceImplement) SelectOne(user *global.User, taskNr int, itemNr int) (*model.CrossPlanTaskDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/CrossPlanTaskDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *CrossPlanTaskDetailServiceImplement) UpdateOne(user *global.User, entity *model.CrossPlanTaskDetail) error {

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

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

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.TaskNr, entity.ItemNr)
		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/CrossPlanTaskDetailService.Select
 *
 ******************************************************************************/
func (impl *CrossPlanTaskDetailServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.CrossPlanTaskDetail, error) {

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

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

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

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

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

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

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

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