package com.sckw.example.controller;

import com.sckw.core.model.constant.Global;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.example.service.TransportService;
import com.sckw.redis.config.RedisLockUtil;
import com.sckw.redis.utils.RedissonUtils;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.EntCacheResDto;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.redisson.api.RSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @desc: 测试
 * @author: lfdc
 * @date: 2023-09-08 14:20
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/transportTest")
public class TransportController {

    @Autowired
    private TransportService transportService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService systemService;

    @Autowired
    private RedisLockUtil redisLockUtil;

    /**
     * 统计承运接单
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/statisticsLogistics", method = RequestMethod.GET)
    public Integer statisticsLogistics() {
        EntCacheResDto entCacheResDto = systemService.queryEntTreeById(LoginUserHolder.getEntId());
        Long topEntId = entCacheResDto.getId();
        Integer integer = transportService.statisticsLogistics(topEntId);
        return integer;
    }

    /**
     * 统计调度派车
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/statisticsWaybillSendCar", method = RequestMethod.GET)
    public Integer statisticsWaybillSendCar() {
        EntCacheResDto entCacheResDto = systemService.queryEntTreeById(LoginUserHolder.getEntId());
        Long topEntId = entCacheResDto.getId();
        Integer integer = transportService.statisticsWaybillSendCar(topEntId);
        return integer;
    }

    /**
     * 统计待核运单
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/statisticsPendingVerificationWaybill", method = RequestMethod.GET)
    public Integer statisticsPendingVerificationWaybill() {
        EntCacheResDto entCacheResDto = systemService.queryEntTreeById(LoginUserHolder.getEntId());
        Long topEntId = entCacheResDto.getId();
        Integer integer = transportService.statisticsPendingVerificationWaybill(topEntId);
        return integer;
    }

    @RequestMapping(value = "/refreshScopeDemo", method = RequestMethod.GET)
    public void refreshScopeDemo() {
        transportService.refreshScopeDemo();
    }

    @RequestMapping(value = "/getRedissionDemo", method = RequestMethod.GET)
    public void getRedissionDemo() {
        long l = System.currentTimeMillis();
        RedissonUtils.putSet(String.valueOf(l),new ArrayList<Long>(){{add(789456L);add(456789L);add(123456789L);}});
        RSet<Object> set = RedissonUtils.getSet(String.valueOf(l));
        long l1 = System.currentTimeMillis();
        RedissonUtils.put(String.valueOf(l1),new ArrayList<Long>(){{add(789456L);add(456789L);add(123456789L);}}, Global.APP_TOKEN_EXPIRE);
        RSet<Object> set1 = RedissonUtils.getSet(String.valueOf(l1));

        //redis  set方式
        long l2 = System.currentTimeMillis();
        RedissonUtils.putSet(String.valueOf(l2),new ArrayList<Long>(){{add(789456L);add(456789L);add(123456789L);}});
        RSet<Object> set2 = RedissonUtils.getSet(String.valueOf(l2));

        //redis  set方式 -带有时间
        long l24 = System.currentTimeMillis();
        long l241 = System.currentTimeMillis();
        RedissonUtils.putSet(String.valueOf(l24),new ArrayList<Long>(){{add(789456L);add(456789L);add(123456789L);}});

        RSet<Object> set24 = RedissonUtils.getSet(String.valueOf(l24));
        RedissonUtils.putSetSeconds(String.valueOf(l241),new ArrayList<Long>(){{add(789456L);add(456789L);add(123456789L);}},Global.PC_BACKGROUND_TOKEN_EXPIRE);
        RSet<Object> set241 = RedissonUtils.getSet(String.valueOf(l241));
        boolean b = redisLockUtil.tryLock("20231019A", 1);
        if (b) {
            System.out.println("获取到锁");
        }
        boolean b1 = redisLockUtil.tryLock("20231019A", 6000000);
        if (b1) {
            System.out.println("没获取到锁");
        }
    }
}
