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.model.Context;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.UserPdu;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.feign.user.IApiUserFeign;
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.util.ObjectUtils;
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 IApiUserFeign userFeign;

    @Autowired
    private ISiteFeign siteFeign;

    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");

      //  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 = "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 {
            String resp = siteFeign.info(siteId);
            JSONObject respJson = JSON.parseObject(resp);
            Integer code = respJson.getInteger("code");
            if (code == YesNoEnum.YES.getValue()) {
                jsonObject.put(KEY_RESULT_DATA, respJson.getJSONObject("data"));
            }
            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();
    }

}