package org.ta.pddserver.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import org.ta.commoncore.model.User;
import org.ta.pddserver.entity.UserEntity;
import org.ta.pddserver.mapper.UserMapper;
import org.ta.pddserver.model.api.request.LogOrderListRequest;
import org.ta.pddserver.model.api.request.UserRequest;
import org.ta.pddserver.model.api.response.LogOrderListResponse;
import org.ta.pddserver.model.api.response.UserListResponse;
import org.ta.pddserver.service.UserService;
import org.ta.pddserver.utils.JsonResponse;
import org.ta.pddserver.utils.OnlineUserData;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {


    public UserEntity getInfoByAccount(String account) {
        return baseMapper.getInfoByAccount(account);
    }

    public Map<String, Object> getListByRequest(UserRequest param) {
        Integer count = baseMapper.countUsable(param);
        param.checkPage(count,20);
        List<UserListResponse> list = baseMapper.getListByRequest(param);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("list", list);
        map.put("page", param.getPage());
        return map;
    }

    public JSONObject checkUser(HttpServletRequest request) {
        UserEntity userEntity=baseMapper.selectById(this.getUserIDFromOnlineData(request));
        Map<String, Object> result = new HashMap<>();
        result.put("user",userEntity);
        return JsonResponse.generateLocalCommonSuccessResponse(result,"");
    }


    protected String getUserIDFromOnlineData(HttpServletRequest request) {
        OnlineUserData onlineUserData = (OnlineUserData) request.getSession().getAttribute(OnlineUserData.ONLINE_CLIENT_DATA);
        if (onlineUserData == null) {
            return null;
        }
        return onlineUserData.getUserId();
    }
}