package com.mosty.wzzx.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mosty.base.model.dto.wzzx.SswzQuery;
import com.mosty.base.model.entity.wzzx.TbWzRywz;
import com.mosty.base.model.entity.wzzx.TbWzSswz;
import com.mosty.base.model.entity.wzzx.TbWzXfwz;
import com.mosty.base.model.query.wzzx.TbWzRywzSearchDto;
import com.mosty.base.model.query.wzzx.TbWzSswzSearchDto;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.Constant;
import com.mosty.base.utils.DateUtils;
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.core.business.entity.SysDept;
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.remote.TbBaseAdaptRemoteService;
import com.mosty.wzzx.service.TbWzSswzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.units.qual.A;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 位置-人员位置
 *
 * @author
 * @since 2022-07-14 15:37:38
 */
@AllArgsConstructor
@RestController
@Api(tags = "人员位置相关接口")
@RequestMapping("/gxga/rywz")
public class TbWzRywzController {

    private final RedisTemplate redisTemplate;
    private final RedisService redisService;
    private final TbWzSswzService tbWzSswzService;

    @GetMapping("/list")
    @ApiOperation(value = "人员位置redis数据列表")
    @Log(title = "人员位置redis数据列表", businessType = BusinessType.OTHER)
    @JwtSysUser
    public ResponseResult<List<TbWzRywz>> getList(SswzQuery dto) {
        List<TbWzRywz> list = new ArrayList<>();
        String key = Constant.WZ_RYWZ + "*";
        Set<String> sets = redisTemplate.keys(key);
        if (!CollectionUtils.isEmpty(sets)) {
            list = redisTemplate.opsForValue().multiGet(sets);
            // 权限
            UserInfo user = UserInfoManager.get();
            String ids = "";
            if (user != null && user.getUserPermissionsInfo() != null) {
                ids = user.getUserPermissionsInfo().getIds();
            }
            if (!CollectionUtils.isEmpty(list)) {
                if (!StringUtils.isEmpty(ids)) {
                    String finalIds = ids;
                    list = list.stream().filter(item -> finalIds.contains(item.getSsbmid())).collect(Collectors.toList());
                }
                // 关键字筛选
                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.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);
    }

    @GetMapping("/getRygj")
    @ApiOperation(value = "获取redis中的人员的轨迹信息")
    public ResponseResult<TbWzRywz> getXfgj(String sfzh) {
        if (StringUtils.isEmpty(sfzh)) return null;
        String redisKey = Constant.WZ_RYWZ + sfzh + "-"
                + DateUtils.getQueryDateString(new Date(), "01");
        TbWzRywz rywz = redisService.getCacheObject(redisKey);
        return ResponseResult.success(rywz);
    }

    @GetMapping("/getRygjList")
    @ApiOperation(value = "获取时间段类的人员轨迹信息俩表")
    public ResponseResult<IPage<TbWzSswz>> getRygjList(TbWzSswzSearchDto dto) {
        return ResponseResult.success(this.tbWzSswzService.getRygjList(dto));
    }

    @GetMapping("/getRywz")
    @ApiOperation(value = "查询某一天的人员位置信息")
    public ResponseResult<TbWzRywz> getRywz(TbWzRywzSearchDto dto) {
        return ResponseResult.success(this.tbWzSswzService.getRywz(dto));
    }
}


