package employee_config

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"wechat-api/ent/employeeconfig"
	"wechat-api/internal/utils/dberrorhandler"

	"wechat-api/internal/svc"
	"wechat-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetEmployeeListConfigLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetEmployeeListConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetEmployeeListConfigLogic {
	return &GetEmployeeListConfigLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx}
}

func (l *GetEmployeeListConfigLogic) GetEmployeeListConfig(req *types.EmployeeConfigListReq) (*types.EmployeeConfigListResp, error) {
	resp := &types.EmployeeConfigListResp{}
	resp.Code = 0
	resp.Msg = errormsg.Success

	sceneList, err := l.svcCtx.DB.EmployeeConfig.Query().Where(employeeconfig.StypeEQ("scene")).All(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	switchInList, err := l.svcCtx.DB.EmployeeConfig.Query().Where(employeeconfig.StypeEQ("switch_in")).All(l.ctx)
	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	}

	for _, val := range sceneList {
		resp.Data.Scene = append(resp.Data.Scene, types.EmployeeConfigInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &val.ID,
				CreatedAt: pointy.GetPointer(val.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(val.UpdatedAt.UnixMilli()),
			},
			Stype: &val.Stype,
			Title: &val.Title,
			Photo: &val.Photo,
		})
	}
	for _, val := range switchInList {
		resp.Data.SwitchIn = append(resp.Data.SwitchIn, types.EmployeeConfigInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:        &val.ID,
				CreatedAt: pointy.GetPointer(val.CreatedAt.UnixMilli()),
				UpdatedAt: pointy.GetPointer(val.UpdatedAt.UnixMilli()),
			},
			Stype: &val.Stype,
			Title: &val.Title,
			Photo: &val.Photo,
		})
	}

	return resp, nil
}
