package com.reyun.saas.report.data.service.impl;

import cn.hutool.core.util.StrUtil;
import com.reyun.saas.mob.user.dao.UserMapper;
import com.reyun.saas.mob.user.domain.App;
import com.reyun.saas.mob.user.domain.User;
import com.reyun.saas.mob.user.service.AuthService;
import com.reyun.saas.report.data.dao.DropDownReportMapper;
import com.reyun.saas.report.data.service.DropDownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc
 */
@Service
public class DropDownServiceImpl implements DropDownService {
    @Autowired
    private DropDownReportMapper dropDownReportMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private AuthService authServiceImp;


    @Override
    public List<String> getVersion(Long loginUserId, String os, String type) {
        //appVersion:App版本,sdkVersion:SDK版本,sysVersion:操作系统版本,model:设备型号,brand:设备品牌
        String column = null;
        switch (type){
            case "appVersion":column = "app_version";break;
            case "sdkVersion":column = "sdk_version";break;
            case "sysVersion":column = "system_version";break;
            case "model":column = "model_equip";break;
            case "brand":column = "brand_equip";break;
            default:return null;
        }
        User user = userMapper.selectByPrimaryKey(loginUserId);
        List<App> apps = authServiceImp.getAppByUserAndOs(user,os,false);
        if(CollectionUtils.isEmpty(apps)){
            return null;
        }
        List<String> appkeys = new ArrayList<>();
        for (App app : apps) {
            appkeys.add(new StringBuilder("'").append(app.getAppkey()).append("'").toString());
        }
        return dropDownReportMapper.getVersion(column,StrUtil.join(",",appkeys),os);

    }
}
