package com.bohuikeji.delivery.backstage.controller.test;

import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.constants.WxAppEm;
import com.bohuikeji.delivery.backstage.constants.WxTmplEm;
import com.bohuikeji.delivery.backstage.entity.TCourier;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.delivery.backstage.service.impl.TCourierSaveDetailServiceImpl;
import com.bohuikeji.delivery.backstage.util.WxUtils;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.socket.Smsg;
import com.bohuikeji.frame.module.admin.filter.WebLogFilter;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import jodd.net.MimeTypes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import static com.bohuikeji.delivery.backstage.controller.open.KeRuYunApi.*;

/**
 * @author ：mmzs
 * @date ：Created in 2021/5/13 17:34
 * @description：日志测试
 * @modified By：
 * @version: $
 */
@Api(tags = {"日志提交测试"})
@Slf4j
@RestController
@RequestMapping("/api/test/log")
@RequiredArgsConstructor
public class LogTestApi extends ApiController {
    final ITCourierService courierService;
    final ITCourierSaveDetailService saveDetailService;
    final ITCourierWithdrawService withdrawService;
    final ITOrderService orderService;

    @ApiOperation("发送实时日志")
    @GetMapping("/txt")
    APIEntity showTxt(String txt,String type,boolean save){
        if (StringUtils.isBlank(type)){
            type="info";
        }
        switch (type){
            case "info":{
                log.info(txt);break;
            }
            case "warn":{
                log.warn(txt);break;
            }
            case "error":{
                log.error(txt);break;
            }
            default:
                return APIEntity.fail("无法识别的日志类型："+type);
        }
        return APIEntity.success();
    }
    @ApiOperation("投保测试")
    @GetMapping("/baoxian")
    APIEntity TouBao(Integer courierId){
       return APIEntity.success(saveDetailService.insure(courierId));
    }

    @GetMapping("/baoxina/query")
    APIEntity test2(String cardNo){
        JSONObject body = new JSONObject();
        body.put("cardNo",cardNo);
        String res=HttpRequest.post("http://test.tdbxjj.com/api/paycenter/getPolicyMsg").bodyText(body.toJSONString(), MimeTypes.MIME_APPLICATION_JSON,"UTF-8")
                .send()
                .contentType("application/json","utf-8").bodyText();
        log.info(res);
        return APIEntity.success(res);
    }


    @GetMapping("/callback/url")
    APIEntity returnURL(String cpcNoticeNo,String payAmount,HttpServletRequest request){

        log.info("流程回调进入参数：{}", WebLogFilter.getRequestParams(request));
        return APIEntity.success();
    }

    /**
     * 通知回调
     * @param request
     * @return
     */
    @GetMapping("/notice/url")
    APIEntity NoticeURL(String cpcNoticeNo,String payAmount,HttpServletRequest request){

        log.info("通知回调进入参数：{}", WebLogFilter.getRequestParams(request));
        //saveDetailService.insureSuccess(cpcNoticeNo,payAmount);
        return APIEntity.success();
    }

    @PostMapping("/test")
    public Object test(String openid) throws MalformedURLException {
        return NumberUtils.get("A");
        /*WxTmplEm tmplEm=WxTmplEm.COURIER_ORDER_NEW;
        tmplEm.setUrl("weixin://dl/business/?t=KyRZsUBqcJi");
        Map<String,Object> params=new HashMap<>();
        params.put("first","在您附近发现新的订单，猛戳抢单！");
        params.put("keyword1","测试");
        params.put("keyword2","15771335697".replaceAll("(\\d{3})\\d{5}(\\d{3})", "$1*****$2"));
        params.put("keyword3","呼和浩特市");
        params.put("remark","点击立即抢单！");
        return WxUtils.sendTmplMsg(WxAppEm.GZH_TCJS,tmplEm,openid,params);*/
        /*return withdrawService.sendToAliPay(desc,new BigDecimal(price),courierId);*/
    }


}
