// 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"
	"strconv"
)

/******************************************************************************
 *
 * @Description              : ProcessWorkPlaceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-28 10:57:29
 *
 ******************************************************************************/
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_WorkLineId.UrlParameterName:  grmi.NewConditionItem(meta.ProcessWorkPlaceLst_WorkLineId, grmi.Equal, true),
		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-07-28 10:57:29
 *
 ******************************************************************************/
var DefaultConditionOfProcessWorkPlaceLstAndPaging = grmi.NewCondition(
	DefaultConditionOfProcessWorkPlaceLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProcessWorkPlaceLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ProcessWorkPlaceLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-28 10:57:29
 *
 ******************************************************************************/
type ProcessWorkPlaceLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProcessWorkPlaceLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProcessWorkPlaceLstDAOImplement实例
 *
 * @Return Value             : ProcessWorkPlaceLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-28 10:57:29
 *
 ******************************************************************************/
func NewProcessWorkPlaceLstServiceImplement() *ProcessWorkPlaceLstServiceImplement {
	return &ProcessWorkPlaceLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) InsertOne(user *models.Usertab, 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.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PO, entity.WorkPlaceNr, 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/ProcessWorkPlaceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) DeleteOne(user *models.Usertab, pO int, workPlaceNr int, workLineId string) 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.Pid, user.Userid)
	record, err := dao.SelectOne(pO, workPlaceNr, workLineId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(pO, workPlaceNr, workLineId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) SelectOne(user *models.Usertab, pO int, workPlaceNr int, workLineId string) (*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.Pid, user.Userid)
	result, err := dao.SelectOne(pO, workPlaceNr, workLineId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) UpdateOne(user *models.Usertab, 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.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PO, entity.WorkPlaceNr, 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/ProcessWorkPlaceLstService.Insert
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Insert(user *models.Usertab, entities model.ProcessWPInsert) 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 err
	}
	dao := dal.NewProcessWorkPlaceLstDAO(session, user.Pid, user.Userid)

	poDao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	wpDao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	po := entities.PO
	workLineId := entities.WorkLineId
	process, err := poDao.SelectOne(po, workLineId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if process == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工序数据")
	}
	li := entities.ProcessWorkPlaceLstLi
	for _, entity := range li {
		if entity.PO != po || entity.WorkLineId != workLineId {
			_ = session.Rollback()
			return grmi.NewBusinessError("关联工序不一致")
		}
		workPlace, err := wpDao.SelectOne(entity.WorkPlaceNr)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		if workPlace == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("不存在对应的工位，workPlaceNr:" + strconv.Itoa(entity.WorkPlaceNr))
		}
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.ProcessWorkPlaceLst_PO.NewPredicate(grmi.Equal, po), meta.ProcessWorkPlaceLst_WorkLineId.NewPredicate(grmi.Equal, workLineId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.Insert(&li)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/ProcessWorkPlaceLstService.Delete
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Delete(user *models.Usertab, 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.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PO, entity.WorkPlaceNr, 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/ProcessWorkPlaceLstService.Select
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, 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.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/ProcessWorkPlaceLstService.Update
 *
 ******************************************************************************/
func (impl *ProcessWorkPlaceLstServiceImplement) Update(user *models.Usertab, 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.Pid, user.Userid)

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

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