package com.iformall.service.test;

import com.alibaba.fastjson.JSON;
import com.iformall.domain.po.*;
import com.iformall.domain.po.msg.WxMsgRecord;
import com.iformall.domain.vo.WxBillAll;
import com.iformall.enums.*;
import com.iformall.mapper.WxBillSettleMapper;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.*;
import com.iformall.utils.Constant;
import com.iformall.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.subject.WebSubject;
import org.junit.Before;
import org.junit.FixMethodOrder;
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.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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

    private static final String TENANT_ID = "456";

    @Resource
    private org.apache.shiro.mgt.SecurityManager securityManager;
    @Resource
    private WebApplicationContext webApplicationContext;
    private MockHttpServletRequest mockHttpServletRequest;
    private MockHttpServletResponse mockHttpServletResponse;

    @Autowired
    WxLevelConfigService levelConfigService;

    @Autowired
    WxCouponSendService wxCouponSendService;

    @Autowired
    WxScoreRulesService wxScoreRulesService ;

    private MockMvc mockMvc;

    @Autowired
    MqBaseProducer mqBaseProducer;

    @Autowired
    private WxBillSettleService wxBillSettleService;
    @Autowired
    private WxBillAllService wxBillAllService;
    @Autowired
    private WxBillSettleMapper wxBillSettleMapper;
    @Autowired
    private WxBillSettleRecordService wxBillSettleRecordService;

    @Autowired
    private WxBillPropertyService wxBillPropertyService;

    @Before
    public void setUp() {
        mockHttpServletRequest = new MockHttpServletRequest(webApplicationContext.getServletContext());
        mockHttpServletResponse = new MockHttpServletResponse();
        MockHttpSession mockHttpSession = new MockHttpSession(webApplicationContext.getServletContext());
        mockHttpServletRequest.setSession(mockHttpSession);
        SecurityUtils.setSecurityManager(securityManager);
        mockMvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .build();
        login("yftest", "123456zz");
    }

    private void login(String username, String password) {
        Subject subject = new WebSubject.Builder(mockHttpServletRequest, mockHttpServletResponse)
                .buildWebSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, true);
        subject.login(token);
        ThreadContext.bind(subject);
    }

    @org.junit.Test
    public void levelList() {
//        WxBillSettle settle = wxBillSettleService.getById(328411851323572224l);
//        log.debug("data:{}", JSON.toJSONString(settle));
//        WxBillAll wxBillAll = new WxBillAll();
//        wxBillAll.setMerchantId(324754110547066880l);
//        wxBillAll.setTenantId("456");
//        wxBillAll.setBillTypeValue(10);
//        log.debug("data:{}", JSON.toJSONString( wxBillAllService.listBill(wxBillAll)));

//        WxBillSettle wxBillSettle = wxBillSettleService.getById(330875180045271040l);
//
//
//        WxBillSettleRecord settleRecord = new WxBillSettleRecord();
//        settleRecord.setSettleId(wxBillSettle.getId());
//        settleRecord.setSettleMoney(wxBillSettle.getBalance());
//        settleRecord.setSettleTime(new Date());
//        settleRecord.setSettleWay(EnumSettleRecordWay.WEBCAT.getCode());
//        settleRecord.setTenantId(wxBillSettle.getTenantId());
//
//        BigDecimal money = new BigDecimal(settleRecord.getSettleMoney()).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
//        settleRecord.setDetail(EnumSettleRecordWay.getEnum(settleRecord.getSettleWay()).getMessage()+"收款"+money.toString()+"元");
//        wxBillSettleRecordService.saveOrUpdate(settleRecord,null,wxBillSettle.getId());
//
//        wxBillSettle.setUpdatetime(new Date());
//        wxBillSettle.setStatus(EnumSettleStatus.FINISH.getCode());
//        wxBillSettleMapper.updateByPrimaryKeySelective(wxBillSettle);
    }

    @org.junit.Test
    public void testmq(){
        Map<String,String> dynamicContentMap = new HashMap<>();
        dynamicContentMap.put("page", Constant.adminPage);
        WxMsgRecord wxMsgRecord = new WxMsgRecord();
        wxMsgRecord.setMsgType(EnumMsgRecordType.SMS.getCode());
        wxMsgRecord.setModelType(EnumMsgModel.FLOW_ASSIGNEE_NODIFY.getCode());
        wxMsgRecord.setReceiver("18601973448");
        wxMsgRecord.setTenantId("456");
        wxMsgRecord.setDynamicContentMap(dynamicContentMap);
        mqBaseProducer.sendMessage(wxMsgRecord, EnumMsgMqTopic.DEFAULT.getCode(),EnumMsgMqTag.DEFAULT.getCode(),EnumMsgMqKey.DEFAULT.getCode());
    }

    @org.junit.Test
    public void testmq2(){

    }

    @org.junit.Test
    public void testPro(){
        System.out.println("--------"+JsonUtil.obj2Json(wxBillPropertyService.detail(329481088662077445l)));

    }
}
