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

/******************************************************************************
 *
 * @Description              : WorkLineWorkPlaceLst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkPlaceLst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkLineWorkPlaceLst_WorkLineId.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_WorkLineId, grmi.Equal, true),
		meta.WorkLineWorkPlaceLst_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_WorkPlaceNr, grmi.Equal, true),
		meta.WorkLineWorkPlaceLst_Pos.UrlParameterName:         grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_Pos, grmi.Equal, false),
		meta.WorkLineWorkPlaceLst_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_LastModify, grmi.Approximate, false),
		meta.WorkLineWorkPlaceLst_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_LastUser, grmi.Equal, false),
		meta.WorkLineWorkPlaceLst_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.WorkLineWorkPlaceLst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkLineWorkPlaceLst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
var DefaultConditionOfWorkLineWorkPlaceLstAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkLineWorkPlaceLst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkLineWorkPlaceLstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkLineWorkPlaceLst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
type WorkLineWorkPlaceLstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkLineWorkPlaceLstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkLineWorkPlaceLstDAOImplement实例
 *
 * @Return Value             : WorkLineWorkPlaceLstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:13:21
 *
 ******************************************************************************/
func NewWorkLineWorkPlaceLstServiceImplement() *WorkLineWorkPlaceLstServiceImplement {
	return &WorkLineWorkPlaceLstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) InsertOne(user *global.User, entity *model.WorkLineWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkLineId, 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/WorkLineWorkPlaceLstService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) DeleteOne(user *global.User, workLineId string, workPlaceNr int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) SelectOne(user *global.User, workLineId string, workPlaceNr int) (*model.WorkLineWorkPlaceLst, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) UpdateOne(user *global.User, entity *model.WorkLineWorkPlaceLst) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkLineWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.WorkLineId, 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/WorkLineWorkPlaceLstService.Insert
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) Insert(user *global.User, entities *[]model.WorkLineWorkPlaceLst) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkLineId, entity.WorkPlaceNr)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.Delete
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) Delete(user *global.User, entities *[]model.WorkLineWorkPlaceLst) error {

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

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

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

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

	condition := DefaultConditionOfWorkLineWorkPlaceLstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewWorkLineWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		li := result.Records.([]model.WorkLineWorkPlaceLst)
		workPlaceNrLi := make([]interface{}, 0, len(li))
		for _, item := range li {
			workPlaceNrLi = append(workPlaceNrLi, item.WorkPlaceNr)
		}
		workPlaceLi, err := workPlaceDao.Select([]grmi.Predicate{meta.WorkPlace_WorkPlaceNr.NewPredicate(grmi.Include, workPlaceNrLi...)}, nil)
		if err != nil {
			return nil, err
		}
		if len(workPlaceLi) != len(workPlaceNrLi) {
			return nil, grmi.NewBusinessError("已关联的工位数据和工位数据有差异")
		}
		workPlaceMap := make(map[int]model.WorkPlace, len(workPlaceNrLi))

		for _, workPlace := range workPlaceLi {
			workPlaceMap[workPlace.WorkPlaceNr] = workPlace
		}
		returnResult := make([]model.WorkLineWorkPlaceReturnData, 0, len(workPlaceLi))
		for _, item := range li {
			workPlace := workPlaceMap[item.WorkPlaceNr]
			returnData := model.WorkLineWorkPlaceReturnData{
				WorkPlaceDescr: workPlace.Descr,
			}
			returnData.WorkLineWorkPlaceLst = item
			returnResult = append(returnResult, returnData)
		}
		result.Records = returnResult
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		workPlaceNrLi := make([]interface{}, 0, len(result))
		for _, item := range result {
			workPlaceNrLi = append(workPlaceNrLi, item.WorkPlaceNr)
		}
		workPlaceLi, err := workPlaceDao.Select([]grmi.Predicate{meta.WorkPlace_WorkPlaceNr.NewPredicate(grmi.Include, workPlaceNrLi...)}, nil)
		if err != nil {
			return nil, err
		}
		if len(workPlaceLi) != len(workPlaceNrLi) {
			return nil, grmi.NewBusinessError("已关联的工位数据和工位数据有差异")
		}
		workPlaceMap := make(map[int]model.WorkPlace, len(workPlaceNrLi))
		for _, workPlace := range workPlaceLi {
			workPlaceMap[workPlace.WorkPlaceNr] = workPlace
		}
		returnResult := make([]model.WorkLineWorkPlaceReturnData, 0, len(workPlaceLi))
		for _, item := range result {
			workPlace := workPlaceMap[item.WorkPlaceNr]
			returnData := model.WorkLineWorkPlaceReturnData{
				WorkPlaceDescr: workPlace.Descr,
			}
			returnData.WorkLineWorkPlaceLst = item
			returnResult = append(returnResult, returnData)
		}

		return returnResult, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.Update
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) Update(user *global.User, entities *[]model.WorkLineWorkPlaceLst) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/WorkLineWorkPlaceLstService.Save
 *
 ******************************************************************************/
func (impl *WorkLineWorkPlaceLstServiceImplement) Save(user *global.User, entities *model.WorkLineWorkPlaceData) error {

	grmi.Log(user, "/services/base/implments/WorkLineWorkPlaceLst.service.impl.go", "SaveWorkLineWorkPlaceLst", "保存多个WorkLineWorkPlaceLst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewWorkLineWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	workLineDao := dal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	workLine, err := workLineDao.SelectOne(entities.WorkLineId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workLine == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("产线不存在")
	}
	workPlaceNrLi := make([]interface{}, 0, len(entities.WorkPlaceNrLi))
	for _, workPlaceNr := range entities.WorkPlaceNrLi {
		workPlaceNrLi = append(workPlaceNrLi, workPlaceNr)
	}
	workPlaceLi, err := workPlaceDao.Select([]grmi.Predicate{meta.WorkPlace_WorkPlaceNr.NewPredicate(grmi.Include, workPlaceNrLi...)}, nil)
	if err != nil {
		_ = session.Rollback()
		return err
	}

	if len(workPlaceLi) != len(entities.WorkPlaceNrLi) {
		_ = session.Rollback()
		return grmi.NewBusinessError("部分工位数据不存在")
	}

	err = dao.DeleteWhere([]grmi.Predicate{meta.WorkLineWorkPlaceLst_WorkLineId.NewPredicate(grmi.Equal, entities.WorkLineId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	workLineWorkPlaceLi := make([]model.WorkLineWorkPlaceLst, 0, len(entities.WorkPlaceNrLi))
	for index, workPlaceNr := range entities.WorkPlaceNrLi {
		data := model.WorkLineWorkPlaceLst{
			WorkLineId:  entities.WorkLineId,
			WorkPlaceNr: workPlaceNr,
			Pos:         index + 1,
		}
		workLineWorkPlaceLi = append(workLineWorkPlaceLi, data)
	}
	err = dao.Insert(&workLineWorkPlaceLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}
