package com.ruoyi.tc.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.tc.entity.AssetCurrent;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.request.AssetCurrentPageRequest;
import com.ruoyi.tc.entity.request.GeneralQueryRequest;
import com.ruoyi.tc.service.AssetCurrentService;
import com.ruoyi.tc.service.UnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 提供外部接口
 *
 * @author wu
 * @since 2024/11/28 16:41
 */
@Api(tags = "提供外部接口")
@RestController
@RequestMapping("/tc/without")
public class WithoutController extends BaseController {

    @Resource
    private AssetCurrentService assetCurrentService;

    @Resource
    private UnitService unitService;

    @Resource
    private RedisCache redisCache;

    /**
     * 根据查询条件查询所有数据(带子表)
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @ApiOperation(value = "查询所有数据(带子表)", response = AssetCurrent.class)
    @PostMapping("/selectAllAsset")
    public AjaxResult selectAllAsset(@RequestBody AssetCurrentPageRequest as, HttpServletRequest request) {
        if (!"U2FsdGVkX199X+bZRylJnFMCCCrHgQw7OJ0DIV7jDQHnrX//trM2UNQx00jKvg4Oi16bAi2VWcg=".equals(request.getHeader("token"))) {
            throw new ServiceException("签名验证失败");
        }
        if (!"OB4J7ZcpC8aLDuH0InPRhYjxNx0TGaRK".equals(request.getHeader("signature"))) {
            throw new ServiceException("签名验证失败");
        }
        return success(assetCurrentService.selectAllAsset(as));
    }

    /**
     * 根据查询条件查询所有单位列表
     *
     * @param req 查询条件
     * @return 单位
     */
    @ApiOperation(value = "获取所有单位列表", response = Unit.class)
    @PostMapping("/selectAllUnit")
    public AjaxResult selectAllUnit(@RequestBody GeneralQueryRequest req, HttpServletRequest request) {
        if (!"U2FsdGVkX199X+bZRylJnFMCCCrHgQw7OJ0DIV7jDQHnrX//trM2UNQx00jKvg4Oi16bAi2VWcg=".equals(request.getHeader("token"))) {
            throw new ServiceException("签名验证失败");
        }
        if (!"OB4J7ZcpC8aLDuH0InPRhYjxNx0TGaRK".equals(request.getHeader("signature"))) {
            throw new ServiceException("签名验证失败");
        }
        return AjaxResult.success(unitService.selectAllUnit(req));
    }

    /**
     * 根据查询条件查询所有单位列表
     *
     * @return 单位
     */
    @ApiOperation(value = "获取所有单位列表213", response = Unit.class)
    @GetMapping("/test")
    public void test() {
        sendSms("123", "17638176947", "120ErrorMes");
    }

    /**
     * 发送短信
     *
     * @param content  短信内容
     * @param tel      手机号多个使用,拼接
     * @param errorMes 异常通知的类别 即redis的key
     */
    public void sendSms(String content, String tel, String errorMes) {
        if (errorMes != null) {
            Integer minutes = redisCache.getCacheObject(errorMes);
            if (minutes == null) {
                redisCache.setCacheObject(errorMes, 1, 10, TimeUnit.MINUTES);
                return;
            }
            if (minutes < 10) {
                long expireTime = redisCache.getExpire(errorMes);
                if (expireTime == -1 || expireTime == -2) {
                    redisCache.setCacheObject(errorMes, 1, 10, TimeUnit.MINUTES);
                } else {
                    redisCache.setCacheObject(errorMes, minutes + 1, (int) expireTime, TimeUnit.SECONDS);
                }
                return;
            }
        }
        // 限制每种tel每天只能发一次短信
        Map<String, Object> dataKey = redisCache.getCacheMap("dataKey");
        if (CollectionUtil.isNotEmpty(dataKey)) {
            String sendDateStr = (String) dataKey.get(content);
            LocalDate sendDate = LocalDate.parse(sendDateStr);
            if (LocalDate.now().equals(sendDate)) {
                return;
            } else {
                dataKey.put(content, LocalDate.now());
                redisCache.setCacheMap("dataKey", dataKey);
            }
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put(content, LocalDate.now());
            redisCache.setCacheMap("dataKey", map);
        }
        System.err.println("已经发送短信");
    }
}
