// 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              : ArtWorkLineLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-23 15:03:00
 *
 ******************************************************************************/
var DefaultConditionOfArtWorkLineLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtWorkLineLst_ArtId.UrlParameterName:      grmi.NewConditionItem(meta.ArtWorkLineLst_ArtId, grmi.Equal, true),
		meta.ArtWorkLineLst_WorkLineId.UrlParameterName: grmi.NewConditionItem(meta.ArtWorkLineLst_WorkLineId, grmi.Equal, true),
		meta.ArtWorkLineLst_LastModify.UrlParameterName: grmi.NewConditionItem(meta.ArtWorkLineLst_LastModify, grmi.Approximate, false),
		meta.ArtWorkLineLst_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.ArtWorkLineLst_LastUser, grmi.Equal, false),
		meta.ArtWorkLineLst_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.ArtWorkLineLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtWorkLineLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-23 15:03:00
 *
 ******************************************************************************/
var DefaultConditionOfArtWorkLineLstAndPaging = grmi.NewCondition(
	DefaultConditionOfArtWorkLineLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtWorkLineLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtWorkLineLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-23 15:03:00
 *
 ******************************************************************************/
type ArtWorkLineLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtWorkLineLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtWorkLineLstDAOImplement实例
 *
 * @Return Value             : ArtWorkLineLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-23 15:03:00
 *
 ******************************************************************************/
func NewArtWorkLineLstServiceImplement() *ArtWorkLineLstServiceImplement {
	return &ArtWorkLineLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtWorkLineLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtWorkLineLstServiceImplement) InsertOne(user *models.Usertab, entity *model.ArtWorkLineLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtWorkLineLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.WorkLineId)
	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/ArtWorkLineLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtWorkLineLstServiceImplement) DeleteOne(user *models.Usertab, artId string, workLineId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtWorkLineLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtWorkLineLstServiceImplement) SelectOne(user *models.Usertab, artId string, workLineId string) (*model.ArtWorkLineLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtWorkLineLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtWorkLineLstServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArtWorkLineLst) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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


/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ArtWorkLineLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtWorkLineLstServiceImplement) Save(user *models.Usertab, entity *model.ArtWorkLineData) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return  err
	}
	dao := dal.NewArtWorkLineLstDAO(session, user.Pid, user.Userid)
	artDao := dal.NewArticleDAO(session, user.Pid, user.Userid)
	workLineDao := dal.NewWorkLineDAO(session, user.Pid, user.Userid)
	artId := entity.ArtID
	li := entity.ArtWorkLineLstLi
	article, err := artDao.SelectOne(artId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if article == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的物料")
	}
	if article.ArtSpec1 != model.ART_TYPE_VIRTUAL && article.ArtSpec1 != model.ART_TYPE_FINISHED {
		_ = session.Rollback()
		return grmi.NewBusinessError("物料类型错误")
	}
	for index, data := range li {
		data.ArtId = artId
		workLine, err := workLineDao.SelectOne(data.WorkLineId)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if workLine == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的产线")
		}
		li[index] = data
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.ArtWorkLineLst_ArtId.NewPredicate(grmi.Equal, artId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(&li)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}