package com.mosty.wzzx.controller;

import com.mosty.base.model.dto.wzzx.SswzQuery;
import com.mosty.base.model.entity.wzzx.TbWzRywz;
import com.mosty.base.model.entity.wzzx.TbWzSbsswz;
import com.mosty.base.utils.Constant;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.redis.service.RedisService;
import com.mosty.common.token.JwtSysUser;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.wzzx.service.TbWzSblswzService;
import com.mosty.wzzx.service.TbWzSbsswzService;
import com.mosty.wzzx.udp.UdpLzEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 位置-巡防报备控制层
 *
 * @author
 * @since 2022-07-14 15:37:38
 */
@RestController
@Api(tags = "设备实时位置相关接口")
@RequestMapping("/gxga/sbsswz")
public class TbWzSbsswzController {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private TbWzSbsswzService tbWzSbsswzService;
    @Resource
    private TbWzSblswzService tbWzSblswzService;

    @PostMapping("/locationHandleLz")
    @ApiOperation(value = "测试udp数据")
    @JwtSysUser
    public ResponseResult<Void> locationHandleLz(@RequestBody UdpLzEntity dto) {
        TbWzSbsswz sswz = this.tbWzSbsswzService.saveSbsswz(dto);
        this.tbWzSblswzService.saveSblswz(sswz);
        return ResponseResult.success();
    }


    @GetMapping("/list")
    @ApiOperation(value = "获取设备实时redis数据列表")
    @Log(title = "获取设备实时redis数据列表", businessType = BusinessType.OTHER)
    @JwtSysUser
    public ResponseResult<List<TbWzSbsswz>> getList(SswzQuery dto) {
        List<TbWzSbsswz> list = new ArrayList<>();
        String key = Constant.WZ_SBWZ + "*";
        Set<String> sets = redisTemplate.keys(key);
        if (!CollectionUtils.isEmpty(sets)) {
            list = redisTemplate.opsForValue().multiGet(sets);
            if (!CollectionUtils.isEmpty(list)) {
                // 关键字筛选
                if (!StringUtils.isEmpty(dto.getKeyword())) {
                    list = list.stream().filter(item ->
                            (!StringUtils.isEmpty(item.getYhXm()) && item.getYhXm().contains(dto.getKeyword())) ||
                                    (!StringUtils.isEmpty(item.getYhSfzh()) && item.getYhSfzh().contains(dto.getKeyword())) ||
                                    (!StringUtils.isEmpty(item.getSsbm()) && item.getSsbm().contains(dto.getKeyword())) ||
                                    (!StringUtils.isEmpty(item.getSbmc()) && item.getSbmc().contains(dto.getKeyword())) ||
                                    (!StringUtils.isEmpty(item.getSbid()) && item.getSbid().contains(dto.getKeyword())) ||
                                    (!StringUtils.isEmpty(item.getYhId()) && item.getYhId().contains(dto.getKeyword()))
                    ).collect(Collectors.toList());
                }
                // 排序
                list.sort((a, a1) -> (int) (a1.getXtCjsj().getTime() - a.getXtCjsj().getTime()));
                list.forEach(item -> item.setZb(null));
            }
        }
        return ResponseResult.success(list);
    }

}


