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

package implments

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

/******************************************************************************
 *
 * @Description              : WorkOrderQty的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderQty = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrderQty_WorkOrderId.UrlParameterName:  grmi.NewConditionItem(meta.WorkOrderQty_WorkOrderId, grmi.Equal, true),
		meta.WorkOrderQty_PlanQty.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderQty_PlanQty, grmi.Equal, false),
		meta.WorkOrderQty_ReleasedQty.UrlParameterName:  grmi.NewConditionItem(meta.WorkOrderQty_ReleasedQty, grmi.Equal, false),
		meta.WorkOrderQty_ActQty.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrderQty_ActQty, grmi.Equal, false),
		meta.WorkOrderQty_ActGoodQty.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrderQty_ActGoodQty, grmi.Equal, false),
		meta.WorkOrderQty_ActScrapQty.UrlParameterName:  grmi.NewConditionItem(meta.WorkOrderQty_ActScrapQty, grmi.Equal, false),
		meta.WorkOrderQty_ActReworkQty.UrlParameterName: grmi.NewConditionItem(meta.WorkOrderQty_ActReworkQty, grmi.Equal, false),
		meta.WorkOrderQty_ActQty1.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderQty_ActQty1, grmi.Equal, false),
		meta.WorkOrderQty_ActQty2.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderQty_ActQty2, grmi.Equal, false),
		meta.WorkOrderQty_ActQty3.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderQty_ActQty3, grmi.Equal, false),
		meta.WorkOrderQty_ActQty4.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrderQty_ActQty4, grmi.Equal, false),
		meta.WorkOrderQty_LastModify.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrderQty_LastModify, grmi.Approximate, false),
		meta.WorkOrderQty_LastUser.UrlParameterName:     grmi.NewConditionItem(meta.WorkOrderQty_LastUser, grmi.Equal, false),
		meta.WorkOrderQty_CreateTime.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrderQty_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkOrderQty的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderQtyAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkOrderQty.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkOrderQtyServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkOrderQty的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
type WorkOrderQtyServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkOrderQtyDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkOrderQtyDAOImplement实例
 *
 * @Return Value             : WorkOrderQtyDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
func NewWorkOrderQtyServiceImplement() *WorkOrderQtyServiceImplement {
	return &WorkOrderQtyServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderQtyService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderQty) error {

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "InsertOneWorkOrderQty", "插入一个WorkOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkOrderId)
	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/om/WorkOrderQtyService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) DeleteOne(user *models.Usertab, workOrderId string) error {

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "DeleteOneWorkOrderQty", "删除一个WorkOrderQty")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderQtyService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) SelectOne(user *models.Usertab, workOrderId string) (*model.WorkOrderQty, error) {

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "SelectOneWorkOrderQty", "查询一个WorkOrderQty")

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderQtyService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderQty) error {

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "UpdateOneWorkOrderQty", "修改一个WorkOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkOrderId)
	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/om/WorkOrderQtyService.Insert
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkOrderQty) error {

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "InsertWorkOrderQty", "插入多个WorkOrderQty")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkOrderId)
		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/om/WorkOrderQtyService.Delete
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkOrderQty) error {

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "DeleteWorkOrderQty", "删除多个WorkOrderQty")

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

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

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "SelectWorkOrderQty", "查询WorkOrderQty")

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

	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "UpdateWorkOrderQty", "修改多个WorkOrderQty")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkOrderId)
		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/om/WorkOrderQtyService.ChangeQty
 *
 ******************************************************************************/
func (impl *WorkOrderQtyServiceImplement) ChangeQty (user *models.Usertab, li []model.WorkOrderQtyChange) error {
	grmi.Log(user, "/services/om/implments/WorkOrderQty.service.impl.go", "ChangeQty", "修改多个WorkOrderQty")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	err := session.Begin()
	if err != nil {
		return err
	}
	defer session.Close()
	dao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	recDao := dal.NewWorkOrderQtyRecLstDAO(session,user.Pid, user.Userid)
	for _, entity := range li {
		record, err := dao.SelectOne(entity.WorkOrderId)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
		if entity.AccomplishQty != record.ActQty {
			if entity.AccomplishQty > record.PlanQty {
				return grmi.NewBusinessError("报工完成数量大于订单数量，workOrderId:" + entity.WorkOrderId)
			}
			if entity.RunningQty > (record.PlanQty - entity.AccomplishQty) {
				return grmi.NewBusinessError("报工生成中数量大于剩余生产数量，workOrderId:" + entity.WorkOrderId)
			}

			maxPos, err := recDao.GetMaxPos(entity.WorkOrderId)
			if err != nil {
				session.Rollback()
				return err
			}
			rec := model.WorkOrderQtyRecLst{
				WorkOrderId: entity.WorkOrderId,
				Pos: maxPos+1,
				ActQty: record.ActQty,
			}
			err = recDao.InsertOne(&rec)
			if err != nil {
				session.Rollback()
				return err
			}
			record.ActQty = entity.AccomplishQty
			record.ActQty1 = entity.RunningQty
			err = dao.UpdateOne(record)
			if err != nil {
				session.Rollback()
				return err
			}
		}
	}
	return nil
}