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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
	"strconv"
)

/******************************************************************************
 *
 * @Description              : ProcessWorkPlaceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfProcessWorkPlaceLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ProcessWorkPlaceLst_PO.UrlParameterName:            grmi.NewConditionItem(meta.ProcessWorkPlaceLst_PO, grmi.Equal, true),
		meta.ProcessWorkPlaceLst_WorkPlaceNr.UrlParameterName:   grmi.NewConditionItem(meta.ProcessWorkPlaceLst_WorkPlaceNr, grmi.Equal, true),
		meta.ProcessWorkPlaceLst_WorkPlaceDesc.UrlParameterName: grmi.NewConditionItem(meta.ProcessWorkPlaceLst_WorkPlaceDesc, grmi.Equal, false),
		meta.ProcessWorkPlaceLst_Pos.UrlParameterName:           grmi.NewConditionItem(meta.ProcessWorkPlaceLst_Pos, grmi.Equal, false),
		meta.ProcessWorkPlaceLst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ProcessWorkPlaceLst_LastModify, grmi.Approximate, false),
		meta.ProcessWorkPlaceLst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ProcessWorkPlaceLst_LastUser, grmi.Equal, false),
		meta.ProcessWorkPlaceLst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ProcessWorkPlaceLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ProcessWorkPlaceLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfProcessWorkPlaceLstAndPaging = grmi.NewCondition(
	DefaultConditionOfProcessWorkPlaceLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProcessWorkPlaceLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ProcessWorkPlaceLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
type ProcessWorkPlaceLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProcessWorkPlaceLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProcessWorkPlaceLstDAOImplement实例
 *
 * @Return Value             : ProcessWorkPlaceLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
func NewProcessWorkPlaceLstServiceImplement() *ProcessWorkPlaceLstServiceImplement {
	return &ProcessWorkPlaceLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) InsertOne(user *global.User, entity *model.ProcessWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PO, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) DeleteOne(user *global.User, pO string, workPlaceNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(pO, workPlaceNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(pO, workPlaceNr)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) SelectOne(user *global.User, pO string, workPlaceNr int) (*model.ProcessWorkPlaceLst, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(pO, workPlaceNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) UpdateOne(user *global.User, entity *model.ProcessWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PO, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.Insert
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Insert(user *global.User, entities *model.ProcessWorkPlaceData) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, error:" + err.Error())
	}
	processDao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	po := entities.PO
	process, err := processDao.SelectOne(po)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询工序数据失败, error:" + err.Error())
	}
	if process == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("工序不存在, 工序ID:" + po)
	}
	li := entities.ProcessWorkPlaceLstLi
	for index, data := range li {
		workplace, err := workPlaceDao.SelectOne(data.WorkPlaceNr)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工位数据失败, error:" + err.Error())
		}
		if workplace == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("工位不存在, 工位ID:" + strconv.Itoa(data.WorkPlaceNr))
		}
		data.PO = po
		data.WorkPlaceDesc = workplace.Descr
		data.Pos = index + 1
		li[index] = data
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.ProcessWorkPlaceLst_PO.NewPredicate(grmi.Equal, po)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除原有数据失败, error:" + err.Error())
	}
	err = dao.Insert(&li)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.Delete
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Delete(user *global.User, entities *[]model.ProcessWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.Select
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.ProcessWorkPlaceLst, error) {

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

	condition := DefaultConditionOfProcessWorkPlaceLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfProcessWorkPlaceLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	condition.Fill(urlParameters)
	result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.Update
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Update(user *global.User, entities *[]model.ProcessWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)

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

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