package com.sptg.gateway.controller.cms;

import com.alibaba.fastjson.JSONObject;
import com.sptg.common.core.util.AssertUtil;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.gateway.util.DataCheck;
import com.sptg.permissions.api.entity.Conf;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * @author xuanyan
 * @date 2020/7/15 14:48
 */
@Slf4j
@RestController
@RequestMapping("/app/cms")
public class CmsController {

//    private String url = "http://arhv.cn:7090/api/articleView/pageH5";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrgFeign orgFeign;

    @Autowired
    private ApiDockingConfFeign apiDockingConfFeign;

    @Autowired
    private ConfFeign confFeign;


    @PostMapping("/showList")
    public Map<String, Object> showListPage(@RequestBody Map<String,String> m) {
        Map<String, Object> hashMap = new HashMap<>();
        //result_code:0-失败  1-成功
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "查询数据失败");
        try {
            String org_id =  m.get("org_id");
            String token = m.get("token");
            String pageNum = m.get("pageNum");
            String pageSize = m.get("pageSize");
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            if (StringUtils.isEmpty(m.get("pageNum"))) {
                hashMap.put("result_msg", "分页页码不能为空");
                return hashMap;
            }
            if (StringUtils.isEmpty(m.get("pageSize"))) {
                hashMap.put("result_msg", "分页大小不能为空");
                return hashMap;
            }
            Conf conf = confFeign.getEntityByToken("cms.request.url");
            AssertUtil.isTrue(StringUtils.isNotEmpty(conf.getConfValue()),"获取请求路径失败");
            String url = conf.getConfValue();
            String requestUrl = url + "?current=" + pageNum + "&size=" + pageSize;
            Map result = this.restTemplate.getForObject(requestUrl, Map.class);

            if (result.get("code").toString().equals("200")  && result.get("success").equals(true)){
                String data = JsonUtils.objectToJson(result.get("data"));
                JSONObject jsonObject = JSONObject.parseObject(data);
                if(jsonObject != null){
                    hashMap.put("list",jsonObject.get("records"));
                    hashMap.put("pages",jsonObject.get("pages"));
                }
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "数据查询成功");
            }
        } catch (Exception exception) {
            log.error("异常", exception);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
