package com.zcloud.modules.api.service.info;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.common.exception.ZException;
import com.zcloud.common.middleware.MiddlewareBean;
import com.zcloud.common.utils.HttpRequestUtil;
import com.zcloud.common.utils.MD5;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.modules.api.service.ApiService;
import com.zcloud.modules.custormerInfo.dao.BusCustomerInfoDao;
import com.zcloud.modules.custormerInfo.entity.BusCustomerInfoEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class ApiServiceInfo implements ApiService {

    @Resource
    private BusCustomerInfoDao busCustomerInfoDao;

    @Resource
    private MiddlewareBean middlewareBean;

    @Override
    public PageUtils curriculumList(Map<String, Object> params) {

        BusCustomerInfoEntity customerinfoId = busCustomerInfoDao.selectById(params.get("customerinfoId").toString());

        String SECRET_KEY = MD5.md5(customerinfoId.getSecretKey() + customerinfoId.getDomain());
        Map<String, Object> request = new HashMap<>();
        request.put("SECRET_KEY", SECRET_KEY);
        request.put("id", customerinfoId.getNumber());
        request.put("currentPage", params.get("curPage").toString());
        request.put("showCount", params.get("limit").toString());

        String userResponse = HttpRequestUtil.doPost(middlewareBean.getUrl() + "/liananzhong/verifyCustomer", JSONObject.toJSONString(request));

        JSONObject jsonObject = JSONObject.parseObject(userResponse);
        JSONArray curriculumList = jsonObject.getJSONArray("curriculumList");
        JSONObject page = jsonObject.getJSONObject("page");

        return new PageUtils(curriculumList, page.getInteger("totalResult"), page.getInteger("showCount"), page.getInteger("currentPage"));

    }

    @Override
    public JSONObject curriculumInfo(Map<String, Object> params) {

        BusCustomerInfoEntity customerinfoId = busCustomerInfoDao.selectById(params.get("customerinfoId").toString());

        String SECRET_KEY = MD5.md5(customerinfoId.getSecretKey() + customerinfoId.getDomain());
        Map<String, Object> request = new HashMap<>();
        request.put("SECRET_KEY", SECRET_KEY);
        request.put("id", customerinfoId.getNumber());
        request.put("CURRICULUM_ID", params.get("curriculumId").toString());

        String userResponse = HttpRequestUtil.doPost(middlewareBean.getUrl() + "/liananzhong/getCurInfo", JSONObject.toJSONString(request));
        JSONObject jsonObject = JSONObject.parseObject(userResponse);

        Integer code = jsonObject.getInteger("code");
        if (code == 2008) {
            throw new ZException("课程已过期");
        } else if (code == 2009) {
            throw new ZException("课程已停用");
        } else if (code == 2010) {
            throw new ZException("课程已删除");
        } else if (code == 2007) {
            throw new ZException("课程不存在");
        }

        return jsonObject;
    }

}
