package implments

import (
	"LAPP_LF_MOM_BACKEND/container"
	"LAPP_LF_MOM_BACKEND/grmi"
)

var workplaceMapping = map[string][]string{
	"WL001": {
		"WP01001",
		"WP01002",
		"WP01003",
		"WP01004",
	},
	"WL002": {
		"WP02001",
		"WP02002",
		"WP02003",
		"WP02004",
	},
}

/******************************************************************************
 *
 * @Struct Name              : SignServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 签到签退服务实现
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2021-03-25
 *
 ******************************************************************************/
type SignServiceImplement struct {
	session *container.SessionContext
}

func (impl *SignServiceImplement) SetSession(value *container.SessionContext) {

	impl.session = value
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SignService.SelectOneUser
 *
 ******************************************************************************/
func (impl *SignServiceImplement) SelectOneUser(_ *container.RequestContext, userCode string) (string, error) {

	grmi.Log(impl.session.User(), "/services/base/implments/Sign.service.impl.go", "SelectOneUser", "查找指定人员的用户名")

	if userCode != "admin" {
		return "", grmi.NewBusinessError("用户不存在!")
	}

	return "管理员", nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SignService.SignIn
 *
 ******************************************************************************/
func (impl *SignServiceImplement) SignIn(_ *container.RequestContext, userCode string, _ string) error {

	grmi.Log(impl.session.User(), "/services/base/implments/Sign.service.impl.go", "SignIn", "签到")

	if userCode != "admin" {
		return grmi.NewBusinessError("用户不存在!")
	}

	// TODO 检查工位是否在某个产线上
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SignService.GetWorklines
 *
 ******************************************************************************/
func (impl *SignServiceImplement) GetWorklines(_ *container.RequestContext) ([]string, error) {

	grmi.Log(impl.session.User(), "/services/base/implments/Sign.service.impl.go", "GetWorklines", "获取全部产线")

	codes := make([]string, 0, len(workplaceMapping))

	for code := range workplaceMapping {
		codes = append(codes, code)
	}

	return codes, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SignService.GetWorkplacesOfWorkline
 *
 ******************************************************************************/
func (impl *SignServiceImplement) GetWorkplacesOfWorkline(_ *container.RequestContext, worklineId string) ([]string, error) {

	grmi.Log(impl.session.User(), "/services/base/implments/Sign.service.impl.go", "GetWorkplacesOfWorkline", "返回产线工位信息")

	codes, ok := workplaceMapping[worklineId]

	if !ok {
		return nil, grmi.NewBusinessError("产线不存在!")
	}

	return codes, nil
}

/******************************************************************************
 *
 * @Function Name            : NewSignServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SignServiceImplement实例
 *
 * @Return Value             : SignServiceImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-03-23 10:43:27
 *
 ******************************************************************************/
func NewSignServiceImplement() *SignServiceImplement {
	return &SignServiceImplement{}
}
