package cn.hnthyy.thmz.service.impl.thmz;

import cn.hnthyy.thmz.entity.zz.WxPayOrder;
import cn.hnthyy.thmz.service.thmz.PaidForZZService;
import cn.hnthyy.thmz.vo.PageViewVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

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

@Slf4j
@Service
public class PaidForZZServiceImpl implements PaidForZZService {

    @Value("${wxPayOrderServiceUrl}")
    private String wxPayOrderServiceUrl;

    @Override
    public PageViewVo listWxPayOrder(WxPayOrder wxPayOrder) {
        PageViewVo pageViewVo = new PageViewVo();
        try {
            Map<String, Object> params = new HashMap();
            if (StringUtils.isNotBlank(wxPayOrder.getPatientId())) {
                params.put("patientId", wxPayOrder.getPatientId());
            }
            if (StringUtils.isNotBlank(wxPayOrder.getPatientName())) {
                params.put("patientName", wxPayOrder.getPatientName());
            }
            if (wxPayOrder.getOrderType() != null) {
                params.put("orderType", wxPayOrder.getOrderType());
            }
            if (wxPayOrder.getPayStatus() != null) {
                params.put("payStatus", wxPayOrder.getPayStatus());
            }
            if (StringUtils.isNotBlank(wxPayOrder.getBeginTime())) {
                params.put("start", wxPayOrder.getBeginTime().substring(0, 10));
            }
            if (StringUtils.isNotBlank(wxPayOrder.getEndTime())) {
                params.put("end", wxPayOrder.getEndTime().substring(0, 10));
            }
            if (wxPayOrder.getPageIndex() != null) {
                params.put("currentPage", wxPayOrder.getPageIndex() + 1);
            }
            if (wxPayOrder.getPageSize() != null) {
                params.put("pageSize", wxPayOrder.getPageSize());
            }
//           String result= HttpUtil.httpPost(wxPayOrderServiceUrl + "/wxRefund/selectOrdersByConditions", params.toString(), "application/json");
//           Map<String,Object> resultMap= (Map<String,Object>)JsonUtil.jsontoObject(result,Map.class);
            RestTemplate restTemplate = new RestTemplate();
            Map<String, Object> resultMap = restTemplate.postForObject(wxPayOrderServiceUrl + "/wxRefund/selectOrdersByConditions", params, Map.class);
            if (resultMap != null) {
                Map<String, Object> data = (Map<String, Object>) resultMap.get("data");
                if (data != null) {
                    pageViewVo.setTotal((Integer) data.get("totalSize"));
                    pageViewVo.setData((List) data.get("list"));
                    return pageViewVo;
                }
            }
            log.info("{}", resultMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        pageViewVo.setTotal(0);
        return pageViewVo;
    }

    @Override
    public Map<String,Object> refundOrder(WxPayOrder wxPayOrder) {
        try {
            wxPayOrder.setRefundOpDatetime(new Date());
            wxPayOrder.setRefundReason("收费员发起退款！");
            RestTemplate restTemplate = new RestTemplate();
            Map<String, Object> resultMap = restTemplate.postForObject(wxPayOrderServiceUrl + "/wxRefund/refundOrder", wxPayOrder, Map.class);
            log.info("{}", resultMap);
            return  resultMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
