package cn.com.ctop.kuaishou.modules.batch.service.impl;

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouAppList;
import cn.com.ctop.kuaishou.modules.batch.entity.KuaiShouCreateAppTemplate;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouAppListMapper;
import cn.com.ctop.kuaishou.modules.batch.mapper.KuaiShouCreateAppTemplateMapper;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaiShouCreateAppTemplateService;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description: 创建应用
 * @Author: jeecg-boot
 * @Date: 2019-08-06
 * @Version: V1.0
 */
@Service
public class KuaiShouCreateAppTemplateServiceImpl extends ServiceImpl<KuaiShouCreateAppTemplateMapper, KuaiShouCreateAppTemplate> implements IKuaiShouCreateAppTemplateService {

    @Autowired
    private ICtopOauthTokenService oauthTokenService;
    @Autowired
    private IKuaishouInterfaceService interfaceService;

    @Autowired
    private KuaiShouAppListMapper appListMapper;

    @Override
    public Map<String, Object> insert(KuaiShouCreateAppTemplate appTemplate) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Long accountId = appTemplate.getAccountId();
            CtopOauthToken cTopOauthToken = oauthTokenService.getOauthTokenByAccountId(String.valueOf(accountId));
            if (Check.isNull(cTopOauthToken)) {
                throw new Exception("未获取到token信息");
            }
            String accessToken = cTopOauthToken.getAccessToken();
            String appVersion = appTemplate.getAppVersion();
            QueryWrapper<KuaiShouAppList> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("app_version", appVersion);
            queryWrapper.eq("account_id", accountId);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.last("limit 1");
            KuaiShouAppList kuaiShouAppList = appListMapper.selectOne(queryWrapper);
            if (!Check.isNull(kuaiShouAppList)) {
                resultMap.put("success", false);
                resultMap.put("message", "应用标记重复");
                resultMap.put("code", -1);
            } else {
                if (!Check.isNull(appTemplate.getFile())) {
                    appTemplate.setFile(KuaishouInterfaceConstant.HTTPS_PREFIX + appTemplate.getFile());
                }
                if (!Check.isNull(appTemplate.getImageUrl())) {
                    appTemplate.setImageUrl(KuaishouInterfaceConstant.HTTPS_PREFIX + appTemplate.getImageUrl());
                }

                JSONObject apkJson = new JSONObject();
                String platform = appTemplate.getPlatform();
                apkJson.put("app_name", appTemplate.getAppName());
                apkJson.put("app_version", appTemplate.getAppVersion());
                apkJson.put("platform", platform);
                if (!Check.isNull(appTemplate.getPackageName())) {
                    apkJson.put("package_name", appTemplate.getPackageName());
                }
                if (!Check.isNull(appTemplate.getImageUrl()) && ("1".equals(platform) || "2".equals(platform))) {
                    Map<String, Object> imageMap = interfaceService.imageUpload(accountId, accessToken, (String) appTemplate.getImageUrl(), 1);
                    if ((Integer) imageMap.get("code") != 0) {
                        return imageMap;

                    }
                    apkJson.put("image_token", imageMap.get("imageToken"));
                }
                if ("1".equals(platform) && appTemplate.getUploadType() == 1) {
                    apkJson.put("file", appTemplate.getFile());

                    Map<String, Object> appMap = interfaceService.appCreate(accountId, accessToken, apkJson);
                    if ((Integer) appMap.get("code") != 0) {
                        return appMap;

                    }

                } else {
                    apkJson.put("url", appTemplate.getUrl());
                    Map<String, Object> appMap = interfaceService.urlAppCreate(accountId, accessToken, apkJson);
                    if ((Integer) appMap.get("code") != 0) {
                        return appMap;
                    }
                }
                resultMap.put("success", true);
                resultMap.put("message", "创建应用成功");
                resultMap.put("code", 0);
            }

        } catch (Exception e) {
            resultMap.put("success", false);
            resultMap.put("message", "创建应用失败");
            resultMap.put("code", -1);
        }
        return resultMap;

    }
}
