// 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              : SpecArtShareAllocate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-14 14:51:48
 *
 ******************************************************************************/
var DefaultConditionOfSpecArtShareAllocate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.SpecArtShareAllocate_AttriCode.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_AttriCode, grmi.Equal, true),
		meta.SpecArtShareAllocate_WorkLineId.UrlParameterName:    grmi.NewConditionItem(meta.SpecArtShareAllocate_WorkLineId, grmi.Equal, true),
		meta.SpecArtShareAllocate_SubObject.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_SubObject, grmi.Equal, true),
		meta.SpecArtShareAllocate_AttriCodeDesc.UrlParameterName: grmi.NewConditionItem(meta.SpecArtShareAllocate_AttriCodeDesc, grmi.Equal, false),
		meta.SpecArtShareAllocate_WorkPlaceNr.UrlParameterName:   grmi.NewConditionItem(meta.SpecArtShareAllocate_WorkPlaceNr, grmi.Equal, false),
		meta.SpecArtShareAllocate_TotalPlanty.UrlParameterName:   grmi.NewConditionItem(meta.SpecArtShareAllocate_TotalPlanty, grmi.Equal, false),
		meta.SpecArtShareAllocate_AssignPlanty.UrlParameterName:  grmi.NewConditionItem(meta.SpecArtShareAllocate_AssignPlanty, grmi.Equal, false),
		meta.SpecArtShareAllocate_CtrlPara1.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_CtrlPara1, grmi.Equal, false),
		meta.SpecArtShareAllocate_CtrlPara2.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_CtrlPara2, grmi.Equal, false),
		meta.SpecArtShareAllocate_CtrlPara3.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_CtrlPara3, grmi.Equal, false),
		meta.SpecArtShareAllocate_CtrlPara4.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_CtrlPara4, grmi.Equal, false),
		meta.SpecArtShareAllocate_CtrlPara5.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_CtrlPara5, grmi.Equal, false),
		meta.SpecArtShareAllocate_CtrlPara6.UrlParameterName:     grmi.NewConditionItem(meta.SpecArtShareAllocate_CtrlPara6, grmi.Equal, false),
		meta.SpecArtShareAllocate_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.SpecArtShareAllocate_LastModify, grmi.Approximate, false),
		meta.SpecArtShareAllocate_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.SpecArtShareAllocate_LastUser, grmi.Equal, false),
		meta.SpecArtShareAllocate_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.SpecArtShareAllocate_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : SpecArtShareAllocate的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-14 14:51:48
 *
 ******************************************************************************/
var DefaultConditionOfSpecArtShareAllocateAndPaging = grmi.NewCondition(
	DefaultConditionOfSpecArtShareAllocate.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SpecArtShareAllocateServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : SpecArtShareAllocate的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-14 14:51:48
 *
 ******************************************************************************/
type SpecArtShareAllocateServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSpecArtShareAllocateDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SpecArtShareAllocateDAOImplement实例
 *
 * @Return Value             : SpecArtShareAllocateDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-14 14:51:48
 *
 ******************************************************************************/
func NewSpecArtShareAllocateServiceImplement() *SpecArtShareAllocateServiceImplement {
	return &SpecArtShareAllocateServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SpecArtShareAllocateService.InsertOne
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) InsertOne(user *models.Usertab, entity *model.SpecArtShareAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.SubObject)
	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/SpecArtShareAllocateService.DeleteOne
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) DeleteOne(user *models.Usertab, attriCode int, workLineId string, subObject string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(attriCode, workLineId, subObject)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(attriCode, workLineId, subObject)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SpecArtShareAllocateService.SelectOne
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) SelectOne(user *models.Usertab, attriCode int, workLineId string, subObject string) (*model.SpecArtShareAllocate, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(attriCode, workLineId, subObject)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SpecArtShareAllocateService.UpdateOne
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) UpdateOne(user *models.Usertab, entity *model.SpecArtShareAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.SubObject)
	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/SpecArtShareAllocateService.Insert
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) Insert(user *models.Usertab, entities *[]model.SpecArtShareAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.SubObject)
		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/SpecArtShareAllocateService.Delete
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) Delete(user *models.Usertab, entities *[]model.SpecArtShareAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.SubObject)
		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/SpecArtShareAllocateService.Select
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfSpecArtShareAllocateAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewSpecArtShareAllocateDAO(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/SpecArtShareAllocateService.Update
 *
 ******************************************************************************/
func (impl *SpecArtShareAllocateServiceImplement) Update(user *models.Usertab, entities *[]model.SpecArtShareAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSpecArtShareAllocateDAO(session, user.Pid, user.Userid)

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

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