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

package implments

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

/******************************************************************************
 *
 * @Description              : ScrewDown的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-24 10:49:50
 *
 ******************************************************************************/
var DefaultConditionOfScrewDown = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ScrewDown_ArtId.UrlParameterName:      grmi.NewConditionItem(meta.ScrewDown_ArtId, grmi.Equal, true),
		meta.ScrewDown_ScrewPos.UrlParameterName:   grmi.NewConditionItem(meta.ScrewDown_ScrewPos, grmi.Equal, true),
		meta.ScrewDown_Result.UrlParameterName:     grmi.NewConditionItem(meta.ScrewDown_Result, grmi.Equal, true),
		meta.ScrewDown_PicPath.UrlParameterName:    grmi.NewConditionItem(meta.ScrewDown_PicPath, grmi.Equal, false),
		meta.ScrewDown_LastModify.UrlParameterName: grmi.NewConditionItem(meta.ScrewDown_LastModify, grmi.Approximate, false),
		meta.ScrewDown_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.ScrewDown_LastUser, grmi.Equal, false),
		meta.ScrewDown_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.ScrewDown_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ScrewDown的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-24 10:49:50
 *
 ******************************************************************************/
var DefaultConditionOfScrewDownAndPaging = grmi.NewCondition(
	DefaultConditionOfScrewDown.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ScrewDownServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ScrewDown的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-24 10:49:50
 *
 ******************************************************************************/
type ScrewDownServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewScrewDownDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ScrewDownDAOImplement实例
 *
 * @Return Value             : ScrewDownDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-05-27 11:38:34
 *
 ******************************************************************************/
func NewScrewDownServiceImplement() *ScrewDownServiceImplement {
	return &ScrewDownServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pdc/ScrewDownService.InsertOne
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) InsertOne(user *models.Usertab, entity *model.ScrewDown) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "InsertOneScrewDown", "插入一个ScrewDown")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScrewDownDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.ScrewPos, entity.Result)
	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/pdc/ScrewDownService.DeleteOne
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) DeleteOne(user *models.Usertab, artId string, screwPos int, result bool) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "DeleteOneScrewDown", "删除一个ScrewDown")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pdc/ScrewDownService.SelectOne
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) SelectOne(user *models.Usertab, artId string, screwPos int, result bool) (*model.ScrewDown, error) {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "SelectOneScrewDown", "查询一个ScrewDown")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pdc/ScrewDownService.UpdateOne
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) UpdateOne(user *models.Usertab, entity *model.ScrewDown) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "UpdateOneScrewDown", "修改一个ScrewDown")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScrewDownDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.ArtId, entity.ScrewPos, entity.Result)
	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/pdc/ScrewDownService.Insert
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) Insert(user *models.Usertab, entities *[]model.ScrewDown) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "InsertScrewDown", "插入多个ScrewDown")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.ScrewPos, entity.Result)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
		if entity.PicPath == "" {
			return grmi.NewBusinessError("图片为空")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pdc/ScrewDownService.Delete
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) Delete(user *models.Usertab, entities *[]model.ScrewDown) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "DeleteScrewDown", "删除多个ScrewDown")

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

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

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "SelectScrewDown", "查询ScrewDown")

	condition := DefaultConditionOfScrewDownAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	grmi.DeleteUrlParametersEmpty(&urlParameters)
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewScrewDownDAO(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/pdc/ScrewDownService.List
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) List(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "ListScrewDown", "列表页展示List")

	condition := DefaultConditionOfScrewDownAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	grmi.DeleteUrlParametersEmpty(&urlParameters)
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}

	predicates = append(predicates, meta.ScrewDown_ScrewPos.NewPredicate(grmi.Equal, -1))
	dao := dal.NewScrewDownDAO(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/pdc/ScrewDownService.Update
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) Update(user *models.Usertab, entities *[]model.ScrewDown) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "UpdateScrewDown", "修改多个ScrewDown")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.ArtId, entity.ScrewPos, entity.Result)
		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/pdc/ScrewDownService.Save
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) Save(user *models.Usertab, screwData model.ScrewDownSave) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "Save", "插入多个ScrewDown")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewScrewDownDAO(session, user.Pid, user.Userid)
	artId := screwData.ArtId
	entities := &screwData.ScrewLi
	for _, entity := range *entities {
		//record, err := dao.SelectOne(entity.ArtId, entity.ScrewPos, entity.Result)
		//if err != nil {
		//	return err
		//}
		//if record != nil {
		//	return grmi.NewBusinessError("已经存在相同主键的记录!")
		//}
		if entity.PicPath == "" {
			return grmi.NewBusinessError("图片为空")
		}
		if entity.ArtId != artId {
			return grmi.NewBusinessError("artID不符")
		}
	}
	err := dao.DeleteWhere([]grmi.Predicate{meta.ScrewDown_ArtId.NewPredicate(grmi.Equal, artId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(entities)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}




/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/pdc/ScrewDownService.DeleteByArtId
 *
 ******************************************************************************/
func (impl *ScrewDownServiceImplement) DeleteByArtId(user *models.Usertab, artId string) error {

	grmi.Log(user, "/services/pdc/implments/ScrewDown.service.impl.go", "DeleteScrewDown", "删除多个ScrewDown")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewScrewDownDAO(session, user.Pid, user.Userid)
	err := dao.DeleteWhere([]grmi.Predicate{meta.ScrewDown_ArtId.NewPredicate(grmi.Equal, artId)})

	if err != nil {
		return err
	}
	return nil
}