// 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              : ArtCheckRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-10 14:18:07
 *
 ******************************************************************************/
var DefaultConditionOfArtCheckRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtCheckRule_AttriCode.UrlParameterName:       grmi.NewConditionItem(meta.ArtCheckRule_AttriCode, grmi.Equal, true),
		meta.ArtCheckRule_WorkLineId.UrlParameterName:      grmi.NewConditionItem(meta.ArtCheckRule_WorkLineId, grmi.Equal, true),
		meta.ArtCheckRule_ProjectId.UrlParameterName:       grmi.NewConditionItem(meta.ArtCheckRule_ProjectId, grmi.Equal, true),
		meta.ArtCheckRule_SubObject.UrlParameterName:       grmi.NewConditionItem(meta.ArtCheckRule_SubObject, grmi.Equal, true),
		meta.ArtCheckRule_AttriCodeDesc.UrlParameterName:   grmi.NewConditionItem(meta.ArtCheckRule_AttriCodeDesc, grmi.Equal, false),
		meta.ArtCheckRule_CodeLength.UrlParameterName:      grmi.NewConditionItem(meta.ArtCheckRule_CodeLength, grmi.Equal, false),
		meta.ArtCheckRule_CheckStartPos.UrlParameterName:   grmi.NewConditionItem(meta.ArtCheckRule_CheckStartPos, grmi.Equal, false),
		meta.ArtCheckRule_CheckLength.UrlParameterName:     grmi.NewConditionItem(meta.ArtCheckRule_CheckLength, grmi.Equal, false),
		meta.ArtCheckRule_CheckObject.UrlParameterName:     grmi.NewConditionItem(meta.ArtCheckRule_CheckObject, grmi.Equal, false),
		meta.ArtCheckRule_OnlyBarCodeFlag.UrlParameterName: grmi.NewConditionItem(meta.ArtCheckRule_OnlyBarCodeFlag, grmi.Equal, false),
		meta.ArtCheckRule_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.ArtCheckRule_LastModify, grmi.Approximate, false),
		meta.ArtCheckRule_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.ArtCheckRule_LastUser, grmi.Equal, false),
		meta.ArtCheckRule_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.ArtCheckRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtCheckRule的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-10 14:18:07
 *
 ******************************************************************************/
var DefaultConditionOfArtCheckRuleAndPaging = grmi.NewCondition(
	DefaultConditionOfArtCheckRule.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtCheckRuleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtCheckRule的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-10 14:18:07
 *
 ******************************************************************************/
type ArtCheckRuleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtCheckRuleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtCheckRuleDAOImplement实例
 *
 * @Return Value             : ArtCheckRuleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-10 14:18:07
 *
 ******************************************************************************/
func NewArtCheckRuleServiceImplement() *ArtCheckRuleServiceImplement {
	return &ArtCheckRuleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtCheckRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) InsertOne(user *models.Usertab, entity *model.ArtCheckRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtCheckRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	daoWorkLine := dal.NewWorkLineDAO(session, user.Pid, user.Userid)
	recordWorkLine, err := daoWorkLine.SelectOne(entity.WorkLineId)
	if err != nil {
		return err
	}
	if recordWorkLine == nil {
		return grmi.NewBusinessError("不存在指定产线!")
	}
	daoAttribute := dal.NewAttributeDAO(session, user.Pid, user.Userid)
	recordAttribute, err := daoAttribute.SelectOne(entity.AttriCode)
	if err != nil {
		return err
	}
	if recordAttribute == nil {
		return grmi.NewBusinessError("不存在指定零件属性!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtCheckRuleService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) DeleteOne(user *models.Usertab, attriCode int, workLineId string, projectId string, subObject string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtCheckRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) SelectOne(user *models.Usertab, attriCode int, workLineId string, projectId string, subObject string) (*model.ArtCheckRule, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtCheckRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArtCheckRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtCheckRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.AttriCode, entity.WorkLineId, entity.ProjectId, entity.SubObject)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	daoWorkLine := dal.NewWorkLineDAO(session, user.Pid, user.Userid)
	daoAttribute := dal.NewAttributeDAO(session, user.Pid, user.Userid)
	if  record.WorkLineId != entity.WorkLineId {
		recordWorkLine, err := daoWorkLine.SelectOne(entity.WorkLineId)
		if err != nil {
			return err
		}
		if recordWorkLine == nil {
			return grmi.NewBusinessError("不存在指定产线!")
		}
	}
	if record.AttriCode != entity.AttriCode {
		recordAttribute, err := daoAttribute.SelectOne(entity.AttriCode)
		if err != nil {
			return err
		}
		if recordAttribute == nil {
			return grmi.NewBusinessError("不存在指定零件属性")
		}
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtCheckRuleService.Insert
 *
 ******************************************************************************/
func (impl *ArtCheckRuleServiceImplement) Insert(user *models.Usertab, entities *[]model.ArtCheckRule) error {

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

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

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

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

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

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

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

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

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

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

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

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