import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.gic.haoban.manage.api.qdto.chat.LinkHmFromWxaQDTO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gic.api.base.commons.BasePageInfo;
import com.gic.haoban.manage.api.dto.DealParamMqDTO;
import com.gic.haoban.manage.api.dto.hm.HmLinkDTO;
import com.gic.haoban.manage.api.dto.hm.HmLinkStoreDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmBatchModifyQDTO;
import com.gic.haoban.manage.api.dto.qdto.hm.HmLinkSearchQDTO;
import com.gic.haoban.manage.api.enums.SyncTaskStatusEnum;
import com.gic.haoban.manage.api.service.DealSyncOperationApiService;
import com.gic.haoban.manage.api.service.KeyDataApiService;
import com.gic.haoban.manage.api.service.hm.HmLinkApiService;
import com.gic.haoban.manage.service.service.QywxSendService;
import com.gic.haoban.manage.service.service.hm.HmClerkRelationService;
import com.gic.haoban.manage.service.service.hm.HmLinkStoreService;
import com.gic.haoban.manage.service.util.QwFriendLimitCountUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-conf.xml" })
public class HmLinkTest {
    @Autowired
    private HmLinkApiService hmLinkApiService;

    @Autowired
    private HmClerkRelationService hmClerkRelationService;

    @Autowired
    private KeyDataApiService keyDataApiService;

    @Autowired
    private HmLinkStoreService hmLinkStoreService;
    @Autowired
    private QywxSendService  qywxSendService ;

    @Autowired
    private DealSyncOperationApiService dealSyncOperationApiService ;
    
     volatile int x = 0 ;
    @Test
    public void test() throws IOException, InterruptedException {
        String taskId = "c7e319c16ba942eea8a3b91d6a763518" ;

                DealParamMqDTO dealParamMqDTO = new DealParamMqDTO();
                dealParamMqDTO.setTaskId(taskId);
                dealParamMqDTO.setData(517064205447217236l+"");
                dealParamMqDTO.setType(SyncTaskStatusEnum.modify_hm.getVal());


        this.dealSyncOperationApiService.departmentDealMq(JSON.toJSONString(dealParamMqDTO)) ;
        if(true) {
            return ;
        }

        HmBatchModifyQDTO qdto = new HmBatchModifyQDTO();
        qdto.setHmIdList(Arrays.asList(517064205447217236L,457922550308036636L));
        qdto.setWxEnterpriseId("ca66a01b79474c40b3e7c7f93daf1a3b");
        //qdto.setOptStaffId("fefd1c81641711e69d0818c58a146fd2");
        qdto.setOptName("达摩管理员");
        qdto.setOptType(1);
        qdto.setClerkIdList(Arrays.asList("4a566818111a45bbae9f962580df82c0"));
        this.hmClerkRelationService.batchModify(qdto);
        
        
        
            System.in.read();
        
        
        /*RRateLimiter rateLimiter = RedisUtil.getRedisClient().getRateLimiter("hm:limit:xx");
        rateLimiter.trySetRate(RateType.OVERALL, 10, 1, RateIntervalUnit.SECONDS);
        
        try {
            Thread.sleep(10000l);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        long start = System.currentTimeMillis() ;
  for(int i=0;i<100;i++)
      System.out.println(rateLimiter.tryAcquire());
  System.out.println("----" +  (System.currentTimeMillis() - start));*/
      /*  
        HmQrcodeListQDTO qdto =new HmQrcodeListQDTO();
        qdto.setClerkSelect("1");
        qdto.setStoreSelect("sele");
        qdto.setHmSelect("1");
        qdto.setHmTypeInt(1);
        qdto.setStatusFlagInt(1);
        qdto.setStartTime(new Date());
        qdto.setEndTime(new Date());
        List<String> storeids = Arrays.asList("11");
        qdto.setStoreId(storeids);
        this.hmQrcodeApiService.listHmIdForIndex(qdto) ;
        System.in.read();*/
    }
    
    @Test
    public void getLinkHmFromWxa(){
       LinkHmFromWxaQDTO qq  = new LinkHmFromWxaQDTO();
        qq.setLatitude("30.281719563802085");
        qq.setDistrictCode("3301");
        qq.setLongitude("120.00284342447917");
        qq.setJustLocationFlag(0);
        this.hmLinkApiService.getLinkHmFromWxa("ff8080815dacd3a2015dacd3ef5c0000", null, null, null,
                "roFgte", "", "","ff8080819763a714019767096103000f",qq) ;
    }
    
    @Test
    public void saveLinkStore() {
        HmLinkStoreDTO dto = new HmLinkStoreDTO();
        dto.setStoreName("11111");
        dto.setStoreId("1");
        List<HmLinkStoreDTO> list = new ArrayList<HmLinkStoreDTO>();

        list.add(dto);

        HmLinkStoreDTO dto1 = new HmLinkStoreDTO();
        dto1.setStoreName("2222");
        dto1.setStoreId("2");
        list.add(dto1);

        this.hmLinkStoreService.saveStore(1000L, list);
    }

    @Test
    public void getLink() {
        System.out.println(JSON.toJSONString(this.hmLinkApiService.getByLinkId(null,null,417077902036099073L), true));
    }

    @Test
    public void getPage() {
        HmLinkSearchQDTO qdto  = new HmLinkSearchQDTO() ; 
        qdto.setWxEnterpriseId("111");
        qdto.setEnterpriseId("2222");
        qdto.setLinkType(1);
        qdto.setSearchParams("LJ01417077902036099073");
        BasePageInfo basePageInfo = new BasePageInfo() ;
        //无效接口
//        System.out.println(JSON.toJSONString(this.hmLinkApiService.listPage(qdto, basePageInfo), true));
    }
    
    
    
    @Test
    public void saveLink() {
        HmLinkDTO link = new HmLinkDTO();
        // dto.setLinkId(417044789037756417L);
        link.setLinkType(1);
        link.setName("打的费2");
        link.setCreatorId("creatorid");
        link.setCreatorName("创建人");
        link.setModifierId("xiugren");
        link.setModifierName("修改人");
        link.setWxEnterpriseId("111");
        link.setEnterpriseId("2222");
        link.setRemark("说明");
        link.setWelcomeId("欢迎语");
        link.setPageId(1000l);
        link.setCustomRuleJson("{}");
        link.setStoreRuleJson("{}");
        link.setMemberLabelId("MEML");

        HmLinkStoreDTO dto = new HmLinkStoreDTO();
        dto.setStoreName("11111");
        dto.setStoreId("1");
        List<HmLinkStoreDTO> list = new ArrayList<HmLinkStoreDTO>();

        list.add(dto);

        HmLinkStoreDTO dto1 = new HmLinkStoreDTO();
        dto1.setStoreName("2222");
        dto1.setStoreId("2");
        list.add(dto1);

        link.setStoreList(list);

        this.hmLinkApiService.save(link);

        // System.out.println(this.hmLinkApiService.listForSms("111", "2222",
        // "打的费")); ;
    }

    @Test
    public void test3() {
        this.keyDataApiService.saveMobileData("112","10000", "15999") ;

        //System.out.println(this.keyDataApiService.getDataByKey("679f17d8c2e940a28dc484689c0339db", KeyDataEnum.MOBILE));
    }

    @Test
    public void test2() {
       //  this.hmLinkVisitLogApiService.save("unionid", 100L);
    }

    @Test
    public void test1() {
        System.out.println(QwFriendLimitCountUtil.getLimit(false));
    }

}
