package com.sptg.gateway.controller;

import com.sptg.ad.api.dto.AdPushAppDto;
import com.sptg.ad.api.entity.AdPushApp;
import com.sptg.ad.api.feigns.AdPushAppFeign;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.estate.api.entity.AccessRecords;
import com.sptg.gateway.entity.AccessRecordsApi;
import com.sptg.gateway.entity.AdPushAppApi;
import com.sptg.gateway.util.DataCheck;
import com.sptg.gateway.util.PojoToApi;
import com.sptg.permissions.api.feigns.ApiDockingConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: AdPushAppController
 * package: com.sptg.estate.biz.service
 * description: 移动端广告管理控制器
 *
 *               
 *                     
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/adPushApp")
public class AdPushAppController {

    private final ApiDockingConfFeign apiDockingConfFeign;
    private final OrgFeign orgFeign;
    private final AdPushAppFeign adPushAppFeign;

    /**
     * 功能描述: 通过会员id获取用户密码授权的开门记录
     * @param map {
     *             org_id        :   机构授权org_id，机构id
     *             token         :   对接令牌
     *            }
     * @return ResultBean<hashMap> 返回结果
     *               
     *                     
     */
    @PostMapping(value = "/getList")
    public Map<String,Object> getList(@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 org_id =  map.get("org_id");
            String token = map.get("token");
            if (DataCheck.checkOrgId(hashMap, org_id, orgFeign)) {
                return hashMap;
            }
            if(DataCheck.checkToken(hashMap, org_id, token,apiDockingConfFeign)){
                return hashMap;
            }
            AdPushAppDto dto = new AdPushAppDto();
            ResultBean<AdPushApp> resultBean = this.adPushAppFeign.getList(dto);
            if(resultBean.getCode().equals("200")){
                hashMap.put("result_code", 1);
                List<AdPushApp> list = resultBean.getList();
                List<AdPushAppApi> apis = new ArrayList<>();
                for(AdPushApp a:list){
                    apis.add(PojoToApi.adPushAppToApi(a));
                }
                hashMap.put("list", apis);
                if(apis.size()==0){
                    hashMap.put("result_msg", "没有相关数据");
                }else{
                    hashMap.put("result_msg", "获取相关数据成功");
                }
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
