package com.sptg.gateway.controller;

import com.sptg.gateway.util.DataCheck;
import com.sptg.permissions.api.entity.AppPackage;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.AppPackageFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * title: AppPackageController
 * package: com.sptg.gateway.controller
 * description: APP升级
 *
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/appPackage")
public class AppPackageController {
    private final AppPackageFeign appPackageFeign;
    private final OrgFeign orgFeign;
    private final ApiDockingConfFeign apiDockingConfFeign;
    private final ConfFeign confFeign;

    /**
     * app检查更新
     * @param map {
     *            platform      :   平台标识
     *            version       :   版本号
     *            org_id        :   机构授权org_id，机构id
     *            token         :   对接令牌
     * }
     * @return
     */
    @PostMapping(value = "/appPackageCheck")
    public Map<String,Object> appPackageCheck(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code",0);
        hashMap.put("result_msg","检查更新失败");
        try {
            String version = map.get("version");
            String org_id = map.get("org_id");
            String token = map.get("token");
            String platform = map.get("platform");
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if(StringUtils.isBlank(platform)){
                hashMap.put("result_msg", "platform不能为空");
                return hashMap;
            }
            AppPackage check = new AppPackage();
            check.setPlatform(platform);
            check.setVersion(version);
            AppPackage appPackage = appPackageFeign.appPackageCheck(check);
            if (appPackage == null){
                hashMap.put("result_code",1);
                hashMap.put("result_msg","当前版本已为最新版本");
            }else {
                hashMap.put("result_code",1);
                hashMap.put("result_msg","检查到新版本");
                if(!platform.equals("ios")){
                    appPackage.setDownloadUrl(confFeign.getFileServerHost() + appPackage.getDownloadUrl());
            }
                hashMap.put("appPackage",appPackage);
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
