package com.izouma.dingdong.service;

import com.izouma.dingdong.domain.merchant.MerchantSettings;
import com.izouma.dingdong.dto.MerchantDTO;
import com.izouma.dingdong.dto.PageQuery;
import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
import com.izouma.dingdong.service.merchant.MerchantService;
import com.izouma.dingdong.service.merchant.MerchantSettingsService;
import com.izouma.dingdong.utils.MapUtils;
import com.izouma.dingdong.web.merchant.MerchantController;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MerchantServiceTest {

    @Autowired
    private MerchantService merchantService;
    @Autowired
    private MerchantController merchantController;
    @Autowired
    private MerchantSettingsService merchantSettingsService;
    @Autowired
    private MerchantSettingsRepo merchantSettingsRepo;

    @Test
    public void testChange() {
        MerchantDTO merchantDTO = MerchantDTO.builder()
                .mid(189L)
                .automaticOrder(true)
                .build();
        merchantService.change(merchantDTO);
    }

    @Test
    public void testAll() {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPage(0);
        pageQuery.setSize(50);
        pageQuery.setSearch(null);
        pageQuery.setSort("");
        Map<String, Object> map = new HashMap<>();
        map.put("startTime", "2020-05-14 13:49:11,2020-5-30 23:59:59");
        pageQuery.setQuery(map);
        //System.out.println(merchantController.all(pageQuery));
    }

    @Test
    public void saveDTO() {
        MerchantDTO dto = new MerchantDTO();
        dto.setMid(48L);
        //Category category = categoryRepo.findById(733L).orElse(null);
        //dto.setCategory(Collections.singleton(category));
        dto.setCategoryId("733");
        System.out.println(merchantController.saveDTO(dto));
    }

    @Test
    public void testTag() {
        System.out.println("--------------->" + merchantSettingsService.timeMerchant(145L).size());
    }

    @Test
    public void test() {
        //广告园 经度longitude 纬度latitude
        double lon3 = 118.734661;
        double lat3 = 31.981746;

        //康润园
        double lon4 = 118.754795;
        double lat4 = 31.990671;

        //六合
        double lon5 = 118.738041;
        double lat5 = 32.152372;


/*        double radLat1 = lat3 * Math.PI / 180.0;
        double radLat2 = lat5 * Math.PI / 180.0;
        double a = radLat1 - radLat2;
        double b = lon3 * Math.PI / 180.0 - lon5 * Math.PI / 180.0;

        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137;
        s = Math.round(s * 1000);
        System.out.println(s);*/

        System.out.println(MapUtils.distance(lon3, lat3, lon5, lat5));
        System.out.println(MapUtils.distance(lon3, lat3, lon4, lat4));


    }

    @Test
    public void testdis() {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPage(0);
        pageQuery.setSize(50);
        pageQuery.setSearch(null);
        pageQuery.setSort("");
        //118.738275 31.991961
        List<MerchantDTO> list = merchantService.showAll(pageQuery, 1.0,1.0 , "首单立减", 82L);

        System.out.println(list.size());
    }

    @Test
    public void testAll2() {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPage(0);
        pageQuery.setSize(50);
        pageQuery.setSearch(null);
        pageQuery.setSort("");
        //118.738275 31.991961
        List<MerchantDTO> list = merchantSettingsService.showAll(pageQuery, 1.0,1.0 , 1L, 82L);

        System.out.println(list.size());
    }

/*    @Test
    public void testapi() {
        //LocalDateTime time = LocalDateTime.now().minusDays(7);
        LocalDate d = LocalDate.now().minusDays(7);


        LocalTime parse = LocalTime.parse("00:00:00");
        LocalDateTime of = LocalDateTime.of(d, parse);
        System.out.println(of);
        System.out.println(merchantRepo.findAllByEstablishTimeAfter(of).size());

        //System.out.println(TencentLocationUtils.distanceBetween(31.981746, 118.734661, 31.990671, 118.754795));
    }*/

    @Test
    public void testIndex(){
        Map<String, Object> index = merchantSettingsService.index(118.00, 31.0);
//        System.out.println(index.get("banner"));
//        System.out.println(index.get("categories"));
//        System.out.println(index.get("timeTag"));
//        System.out.println(index.get("promote1"));
//        System.out.println(index.get("promote2"));
//        System.out.println(index.get("newMerchants"));
        System.out.println(index);
    }

    @Test
    public void testFull(){
        //System.out.println(fullReductionRepo.findAllByMerchantId(189L));
        System.out.println(merchantSettingsService.getDTO(189L));
    }

    @Test
    public void testWeek(){
        MerchantSettings settings = merchantSettingsRepo.findById(306L).orElse(null);
      //  System.out.println(settings);
        System.out.println(settings.getWeek().contains(LocalDateTime.now().getDayOfWeek().toString()));

        //System.out.println(LocalDateTime.now().getDayOfWeek());
    }
}
