package com.mortals.xhx.module.sitestat.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.pdu.user.UserPdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.feign.user.IUserFeign;
import com.mortals.xhx.module.sitestat.model.SitestatEntity;
import com.mortals.xhx.module.sitestat.service.SitestatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 站点统计
 *
 * @author zxfei
 * @date 2022-06-27
 */
@RestController
@RequestMapping("sitestat")
@Slf4j
public class SitestatController extends BaseCRUDJsonBodyMappingController<SitestatService, SitestatEntity, Long> {

    @Autowired
    private IAuthTokenService authTokenService;


    @Autowired
    private ISiteFeign siteFeign;
    @Autowired
    private IUserFeign userFeign;

    public SitestatController() {
        super.setModuleDesc("站点统计");
    }


    @Override
    protected void init(Map<String, Object> model, Context context) {
        super.init(model, context);

        //todo 模拟登陆鉴权
//        UserPdu userPdu = new UserPdu();
//        userPdu.setLoginName("admin");
//        userPdu.setPassword("admin");
//        userPdu.setSecurityCode("8888");
//        String loginResp = userFeign.portalLogin(userPdu);
//        JSONObject loginRespJson = JSON.parseObject(loginResp);
//        String token = loginRespJson.getJSONObject("data").getString("token");
//        log.info("token:{}", token);
//        //  String token = authTokenService.getToken(request);
//        String resp = siteFeign.siteTree(token);
//
//        log.info("siteTree:{}", resp);

//        JSONObject jsonObject = JSON.parseObject(resp);
//
//        Integer code = jsonObject.getInteger("code");
//        if (code == YesNoEnum.YES.getValue()) {
//            JSONArray siteTreeArray = jsonObject.getJSONObject("data").getJSONArray("siteTree");
//            model.put("siteTree", siteTreeArray);
//        }




    }


    /**
     * 构建站点树
     */
    @GetMapping(value = "siteTree")
    public String siteTree() {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = this.getModuleDesc() + "构建站点树";
        try {
            String siteTree = this.service.getSiteTree(getContext());

            return siteTree;
        } catch (Exception e) {
            log.error("构建站点树异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }


    /**
     * 站点列表
     */
    @GetMapping(value = "maplist")
    public String maplist() {
        JSONObject jsonObject = new JSONObject();
        String busiDesc = this.getModuleDesc() + "构建站点列表";
        try {

            // TODO: 2022/7/6  管理员角色 获取所有站点列表
            SitePdu sitePdu = new SitePdu();
            sitePdu.setPage(1);
            sitePdu.setSize(-1);
            Rest<RespData<List<SitePdu>>> resp = siteFeign.list(sitePdu);
            return JSON.toJSONString(resp);
        } catch (Exception e) {
            log.error("站点异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }


    /**
     * 站点信息
     */
    @GetMapping(value = "siteInfo")
    public String siteInfo(Long siteId) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Object> model = new HashMap<>();
        String busiDesc = this.getModuleDesc() + "获取站点";
        jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        try {
            Rest<SitePdu> resp = siteFeign.info(siteId);
            if (resp.getCode() == YesNoEnum.YES.getValue()) {
                jsonObject.put(KEY_RESULT_DATA, resp.getData());
            }
            jsonObject.put(KEY_RESULT_MSG, busiDesc + "成功！");
        } catch (Exception e) {
            log.error("获取站点异常", e);
            jsonObject.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            jsonObject.put(KEY_RESULT_MSG, super.convertException(e));
        }
        return jsonObject.toJSONString();
    }

}