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

/******************************************************************************
 *
 * @Description              : OPCBasicComInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
var DefaultConditionOfOPCBasicComInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCBasicComInfo_PO.UrlParameterName:          grmi.NewConditionItem(meta.OPCBasicComInfo_PO, grmi.Equal, true),
		meta.OPCBasicComInfo_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.OPCBasicComInfo_StepNo, grmi.Equal, true),
		meta.OPCBasicComInfo_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCBasicComInfo_StepDesc, grmi.Equal, false),
		meta.OPCBasicComInfo_StepType.UrlParameterName:    grmi.NewConditionItem(meta.OPCBasicComInfo_StepType, grmi.Equal, false),
		meta.OPCBasicComInfo_BrandName.UrlParameterName:   grmi.NewConditionItem(meta.OPCBasicComInfo_BrandName, grmi.Equal, false),
		meta.OPCBasicComInfo_MachineType.UrlParameterName: grmi.NewConditionItem(meta.OPCBasicComInfo_MachineType, grmi.Equal, false),
		meta.OPCBasicComInfo_IpAddress.UrlParameterName:   grmi.NewConditionItem(meta.OPCBasicComInfo_IpAddress, grmi.Equal, false),
		meta.OPCBasicComInfo_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.OPCBasicComInfo_LastModify, grmi.Approximate, false),
		meta.OPCBasicComInfo_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.OPCBasicComInfo_LastUser, grmi.Equal, false),
		meta.OPCBasicComInfo_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.OPCBasicComInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCBasicComInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
var DefaultConditionOfOPCBasicComInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCBasicComInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCBasicComInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCBasicComInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
type OPCBasicComInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCBasicComInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCBasicComInfoDAOImplement实例
 *
 * @Return Value             : OPCBasicComInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-27 13:55:54
 *
 ******************************************************************************/
func NewOPCBasicComInfoServiceImplement() *OPCBasicComInfoServiceImplement {
	return &OPCBasicComInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) InsertOne(user *models.Usertab, entity *model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	processDao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	stepDao := dal.NewStepTypeDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PO, entity.StepNo)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	poLi, err := processDao.Select([]grmi.Predicate{meta.Process_PO.NewPredicate(grmi.Equal, entity.PO)}, nil)
	if err != nil {
		return err
	}
	if len(poLi) == 0 {
		return grmi.NewBusinessError("不存在对应的工序")
	}
	step, err := stepDao.SelectOne(entity.StepType)
	if err != nil {
		return err
	}
	if step == nil {
		return grmi.NewBusinessError("不存在对应的操作类型")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) DeleteOne(user *models.Usertab, pO int, stepNo int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(pO, stepNo)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCComRule_PO.NewPredicate(grmi.Equal, pO), meta.OPCComRule_StepNo.NewPredicate(grmi.Equal, stepNo)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCComDetailRule_PO.NewPredicate(grmi.Equal, pO), meta.OPCComDetailRule_StepNo.NewPredicate(grmi.Equal, stepNo)})
	if err != nil {
		_ = session.Rollback()
	}
	err = dao.DeleteOne(pO, stepNo)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) SelectOne(user *models.Usertab, pO int, stepNo int) (*model.OPCBasicComInfo, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) UpdateOne(user *models.Usertab, entity *model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.PO, entity.StepNo)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	processDao := dal.NewProcessDAO(session, user.Pid, user.Userid)
	stepDao := dal.NewStepTypeDAO(session, user.Pid, user.Userid)
	if record.PO != entity.PO {
		poLi, err := processDao.Select([]grmi.Predicate{meta.Process_PO.NewPredicate(grmi.Equal, entity.PO)}, nil)
		if err != nil {
			return err
		}
		if len(poLi) == 0 {
			return grmi.NewBusinessError("不存在对应的工序")
		}
	}
	if record.StepType != entity.StepType {
		step, err := stepDao.SelectOne(entity.StepType)
		if err != nil {
			return err
		}
		if step == nil {
			return grmi.NewBusinessError("不存在对应的操作类型")
		}
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.Insert
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Insert(user *models.Usertab, entities *[]model.OPCBasicComInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PO, entity.StepNo)
		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/base/OPCBasicComInfoService.Delete
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Delete(user *models.Usertab, entities *[]model.OPCBasicComInfo) error {

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

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

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

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

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

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

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

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

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