// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/qm"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/qm"
	model "LAPP_LF_MOM_BACKEND/models/qm"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : ArtView的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfArtView = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtView_ArtId.UrlParameterName:       grmi.NewConditionItem(meta.ArtView_ArtId, grmi.Equal, true),
		meta.ArtView_ViewId.UrlParameterName:      grmi.NewConditionItem(meta.ArtView_ViewId, grmi.Equal, true),
		meta.ArtView_Pos.UrlParameterName:         grmi.NewConditionItem(meta.ArtView_Pos, grmi.Equal, false),
		meta.ArtView_Descr.UrlParameterName:       grmi.NewConditionItem(meta.ArtView_Descr, grmi.Equal, false),
		meta.ArtView_ViewPicture.UrlParameterName: grmi.NewConditionItem(meta.ArtView_ViewPicture, grmi.Equal, false),
		meta.ArtView_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.ArtView_LastModify, grmi.Approximate, false),
		meta.ArtView_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.ArtView_LastUser, grmi.Equal, false),
		meta.ArtView_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.ArtView_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtView的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
var DefaultConditionOfArtViewAndPaging = grmi.NewCondition(
	DefaultConditionOfArtView.Items,
	grmi.NewPaging(),
)

/******************************************************************************
*
* @Struct Name              : ArtViewServiceImplement
*-----------------------------------------------------------------------------
*
* @Description              : ArtView的服务实现
*
* @Author                   : 代码生成器创建
*
* @Date                     : 2021-03-23 10:43:27
*
******************************************************************************/
type ArtViewServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtViewDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtViewDAOImplement实例
 *
 * @Return Value             : ArtViewDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewArtViewServiceImplement() *ArtViewServiceImplement {
	return &ArtViewServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ArtViewService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtViewServiceImplement) InsertOne(user *models.Usertab, entity *model.ArtView) error {

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "InsertOneArtView", "插入一个ArtView")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtViewDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.ViewId)
	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/qm/ArtViewService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtViewServiceImplement) DeleteOne(user *models.Usertab, artId string, viewId string) error {

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "DeleteOneArtView", "删除一个ArtView")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ArtViewService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtViewServiceImplement) SelectOne(user *models.Usertab, artId string, viewId string) (*model.ArtView, error) {

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "SelectOneArtView", "查询一个ArtView")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/qm/ArtViewService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtViewServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArtView) error {

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "UpdateOneArtView", "修改一个ArtView")

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

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "InsertArtView", "插入多个ArtView")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "DeleteArtView", "删除多个ArtView")

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

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

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "SelectArtView", "查询ArtView")

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

	grmi.Log(user, "/services/qm/implments/QM_ArtView.service.impl.go", "UpdateArtView", "修改多个ArtView")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewArtViewDAO(session, user.Pid, user.Userid)

	if entities == nil || len(*entities) <= 0 {
		return grmi.NewBusinessError("不能清空物料和视图的关联!")
	}
	artId := (*entities)[0].ArtId
	viewDAO := dal.NewViewDAO(session, user.Pid, user.Userid)
	for index, entity := range *entities {
		if entity.ArtId != artId {
			return grmi.NewBusinessError("物料标识不一致!")
		}
		view, err := viewDAO.SelectOne(entity.ViewId)
		if err != nil {
			return err
		}
		if view == nil {
			return grmi.NewBusinessError("不存在指定视图!")
		}
		entity.Pos = index + 1
		entity.Descr = view.Descr
		entity.ViewPicture = view.Picture
	}
	artViewDAO := dal.NewArtViewDAO(session, user.Pid, user.Userid)
	err = artViewDAO.DeleteWhere([]grmi.Predicate{meta.ArtView_ArtId.NewPredicate(grmi.Equal, artId)})
	if err != nil {
		return err
	}
	err = artViewDAO.Insert(entities)
	if err != nil {
		return err
	}
	err = dao.Update(entities)
	if err != nil {
		return err
	}
	err = session.Commit()
	if err != nil {
		return err
	}
	return nil
}
