// 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              : ArtQtyAllocate的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
var DefaultConditionOfArtQtyAllocate = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtQtyAllocate_AttriCode.UrlParameterName:     grmi.NewConditionItem(meta.ArtQtyAllocate_AttriCode, grmi.Equal, true),
		meta.ArtQtyAllocate_WorkLineId.UrlParameterName:    grmi.NewConditionItem(meta.ArtQtyAllocate_WorkLineId, grmi.Equal, true),
		meta.ArtQtyAllocate_SubObject.UrlParameterName:     grmi.NewConditionItem(meta.ArtQtyAllocate_SubObject, grmi.Equal, true),
		meta.ArtQtyAllocate_AttriCodeDesc.UrlParameterName: grmi.NewConditionItem(meta.ArtQtyAllocate_AttriCodeDesc, grmi.Equal, false),
		meta.ArtQtyAllocate_TotalPlanty.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_TotalPlanty, grmi.Equal, false),
		meta.ArtQtyAllocate_PO.UrlParameterName:            grmi.NewConditionItem(meta.ArtQtyAllocate_PO, grmi.Equal, false),
		meta.ArtQtyAllocate_AssignPlanty.UrlParameterName:  grmi.NewConditionItem(meta.ArtQtyAllocate_AssignPlanty, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara1.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara1, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara2.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara2, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara3.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara3, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara4.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara4, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara5.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara5, grmi.Equal, false),
		meta.ArtQtyAllocate_AQCtrlPara6.UrlParameterName:   grmi.NewConditionItem(meta.ArtQtyAllocate_AQCtrlPara6, grmi.Equal, false),
		meta.ArtQtyAllocate_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ArtQtyAllocate_LastModify, grmi.Approximate, false),
		meta.ArtQtyAllocate_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ArtQtyAllocate_LastUser, grmi.Equal, false),
		meta.ArtQtyAllocate_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ArtQtyAllocate_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtQtyAllocate的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
var DefaultConditionOfArtQtyAllocateAndPaging = grmi.NewCondition(
	DefaultConditionOfArtQtyAllocate.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtQtyAllocateServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtQtyAllocate的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
type ArtQtyAllocateServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtQtyAllocateDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtQtyAllocateDAOImplement实例
 *
 * @Return Value             : ArtQtyAllocateDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
func NewArtQtyAllocateServiceImplement() *ArtQtyAllocateServiceImplement {
	return &ArtQtyAllocateServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtQtyAllocateService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) InsertOne(user *models.Usertab, entity *model.ArtQtyAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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/ArtQtyAllocateService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) DeleteOne(user *models.Usertab, attriCode int, workLineId string, subObject string) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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/ArtQtyAllocateService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) SelectOne(user *models.Usertab, attriCode int, workLineId string, subObject string) (*model.ArtQtyAllocate, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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/ArtQtyAllocateService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArtQtyAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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/ArtQtyAllocateService.Insert
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) Insert(user *models.Usertab, entities *[]model.ArtQtyAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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/ArtQtyAllocateService.Delete
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) Delete(user *models.Usertab, entities *[]model.ArtQtyAllocate) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtQtyAllocateDAO(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/ArtQtyAllocateService.Select
 *
 ******************************************************************************/
func (impl *ArtQtyAllocateServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

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

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