package com.iformall.b;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iformall.domain.po.WxCouponSend;
import com.iformall.enums.EnumCouponSendSendType;
import lombok.extern.slf4j.Slf4j;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("dev")
@AutoConfigureMockMvc
@FixMethodOrder(MethodSorters.JVM)
public class CouponsendTest {

    private static final String TOKEN = "c906835d-08c5-47b1-8843-179918f3b312";

    @Autowired
    private MockMvc mockMvc;

    /**
     * 商户注券::卡券列表::全部
     */
    @Test
    public void listAll() throws Exception {
        //时间排序
        MvcResult result = mockMvc.perform(
                MockMvcRequestBuilders.get("/api/couponSend/list")
                        .header("token", TOKEN)
                        .param("pageNum", "1")
                        .param("pageSize", "10")
                        .param("sortColumn", "createDate")
                        .param("sortOrder", "desc")
                        .param("expired", "0")
                        .param("sendType", EnumCouponSendSendType.MERCHANT.getCode() + "")
        )
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String responseStr = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> respMap = mapper.readValue(responseStr, Map.class);
        log.info("respMap -> {}", JSON.toJSONString(respMap));
        // 商家排序规则
        //MvcResult result1 = mockMvc.perform(
        //        MockMvcRequestBuilders.get("/api/couponSend/list")
        //                .header("token", TOKEN)
        //                .param("pageNum", "1")
        //                .param("pageSize", "10")
        //                .param("sortColumn", WxCouponSend.SORT_MERCHANT_SEND)
        //                .param("sortOrder", "desc")
        //                //.param("expired", "1")
        //                .param("sendType", EnumCouponSendSendType.MERCHANT.getCode() + "")
        //)
        //        .andExpect(MockMvcResultMatchers.status().isOk())
        //        .andDo(MockMvcResultHandlers.print())
        //        .andReturn();
        //String responseStr1 = result1.getResponse().getContentAsString();
        //ObjectMapper mapper1 = new ObjectMapper();
        //Map<String, String> respMap1 = mapper1.readValue(responseStr1, Map.class);
        //log.info("respMap -> {}", JSON.toJSONString(respMap1));
    }

    /**
     * 商户注券::卡券列表::有效
     */
    @Test
    public void listUnExpired() throws Exception {
        MvcResult result = mockMvc.perform(
                MockMvcRequestBuilders.get("/api/couponSend/list")
                        .header("token", TOKEN)
                        .param("pageNum", "1")
                        .param("pageSize", "10")
                        .param("expired", "0")
                        //.param("status", "0")
                        .param("sendType", EnumCouponSendSendType.MERCHANT.getCode() + "")
        )
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String responseStr = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> respMap = mapper.readValue(responseStr, Map.class);
        log.info("respMap -> {}", JSON.toJSONString(respMap));
    }

    /**
     * 商户注券::商户注券
     */
    @Test
    public void handSel() throws Exception {
        Map<String, Object> pamas = new HashMap<>();
        pamas.put("wxCouponSendIds", Arrays.asList("327321450622582784"));
        pamas.put("cUserId", "321555911759462400");
        MvcResult result = mockMvc.perform(
                MockMvcRequestBuilders.post("/api/couponSend/handSel")
                        .header("token", TOKEN)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(JSON.toJSONString(pamas))
        )
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String responseStr = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> respMap = mapper.readValue(responseStr, Map.class);
        log.info("respMap -> {}", JSON.toJSONString(respMap));
    }

    /**
     * 商户注券::注券记录
     */
    @Test
    public void actionLog() throws Exception {
        MvcResult result = mockMvc.perform(
                MockMvcRequestBuilders.get("/api/couponSend/actionLog")
                        .header("token", TOKEN)
                        .param("pageNum", "1")
                        .param("pageSize", "10")
                        .param("channelType", String.valueOf(EnumCouponSendSendType.MERCHANT.getCode()))
                //.param("beginDate", "2019-01-01")
                //.param("endDate", "2019-09-01")
        )
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andDo(MockMvcResultHandlers.print())
                .andReturn();
        String responseStr = result.getResponse().getContentAsString();
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> respMap = mapper.readValue(responseStr, Map.class);
        log.info("respMap -> {}", JSON.toJSONString(respMap));
    }

}
