package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.dto.decoration.DecorationDTO;
import com.cool.store.dto.decoration.DesignRowsDTO;
import com.cool.store.dto.decoration.ProjectDTO;
import com.cool.store.dto.decoration.RowsDTO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mq.util.HttpRestTemplateService;
import com.cool.store.service.YlfService;
import com.cool.store.utils.poi.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author suzhuhong
 * @Date 2024/4/28 14:26
 * @Version 1.0
 */
@Service
@Slf4j
public class YlfServiceImpl implements YlfService {

    static String url = "https://hzly.cloudcubic.net/ajaxHandle/synchronization/JCallBackSynchronizationHandler.ashx?action=app&controller=GetProjectDetails&projectid=";

    @Resource
    private HttpRestTemplateService httpRestTemplateService;

    @Override
    public DecorationDTO getDecoration(Long id) {
        String detailUrl = String.format("%s%d", url, id);
        try {
            String forObject = httpRestTemplateService.getForObject(detailUrl, String.class, new HashMap<>());
            Integer status = (Integer) JSONObject.parseObject(forObject, JSONObject.class).get("status");
            if (status != 200) {
                String msg = (String) JSONObject.parseObject(forObject, JSONObject.class).get("msg");
                log.info("获取云立方装修公司信息失败,id:{}", id);
                throw new ServiceException(msg);
            }
            Object data = JSONObject.parseObject(forObject, JSONObject.class).get("data");
            if (data == null) {
                return null;
            }
            RowsDTO rowsDTOS = JSONObject.parseObject(JSONObject.toJSONString(data), RowsDTO.class);
            List<DecorationDTO> list = rowsDTOS.getRows();
            if (CollectionUtils.isNotEmpty(list)) {
                return list.get(0);
            }
        } catch (Exception e) {
            if ("无对应数据！".equals(((ServiceException)e).getErrorMessage())){
                throw new ServiceException(ErrorCodeEnum.YLF_DATA_IS_NULL);
            }
            throw new ServiceException(ErrorCodeEnum.YLF_ERROR,((ServiceException)e).getErrorMessage());
        }
        return null;
    }

    @Override
    public ProjectDTO getProjectList(String storeNum) {
        log.info("getProjectList param:{}", storeNum);
        if (Objects.isNull(storeNum)) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        String url = String.format(Constants.GET_PROJECT_LIST, storeNum);
        log.info("CoolStoreStartFlowServiceImpl#getOrder, url:{}", url);
        try {
            String jsonString = httpRestTemplateService.getForObject(url, String.class, new HashMap<>());
            JSONObject jsonObject = JSONObject.parseObject(jsonString);
            Integer status = (Integer) jsonObject.get("status");
            if (status != 200) {
                log.info("获取云立方装修公司信息失败,storeNum:{}", storeNum);
                String msg = (String) jsonObject.get("msg");
                throw new ServiceException(msg);
            }
            JSONObject data = jsonObject.getJSONObject("data");
            log.info("CoolStoreStartFlowServiceImpl#getOrder,jsonObject:{}", jsonObject);
            if (Objects.nonNull(data)) {
                DesignRowsDTO designRowsDTO = JSONObject.parseObject(JSONObject.toJSONString(data), DesignRowsDTO.class);
                List<ProjectDTO> rows = designRowsDTO.getRows();
                if (CollectionUtils.isNotEmpty(rows)) {
                    Collections.sort(rows, new Comparator<ProjectDTO>() {
                        @Override
                        public int compare(ProjectDTO p1, ProjectDTO p2) {
                            return Long.compare(p2.getProjectId(), p1.getProjectId()); // 降序排序
                        }
                    });   // 降序排序
                    return rows.get(0);
                }
            } else {
                log.info("获取云立方装修公司信息失败,shopNum:{}", storeNum);
                throw new ServiceException(ErrorCodeEnum.YLF_ERROR);
            }
        } catch (Exception e) {
            log.info("调用云立方获取项目列表异常,getProjectList error:{}", e);
            if ("无对应数据！".equals(((ServiceException)e).getErrorMessage())){
                throw new ServiceException(ErrorCodeEnum.YLF_DATA_IS_NULL);
            }
            throw new ServiceException(ErrorCodeEnum.YLF_ERROR,((ServiceException)e).getErrorMessage());
        }

        return null;
    }


}
