package com.ytpm.controller;

import cn.hutool.core.collection.CollUtil;
import com.ytpm.agent.model.YtPlatformBanned;
import com.ytpm.agent.view.AgentUserInfo;
import com.ytpm.app.param.AppQueryUserTodayTimeParam;
import com.ytpm.general.Result;
import com.ytpm.general.ResultTable;
import com.ytpm.risk.param.RiskBannedListParam;
import com.ytpm.risk.param.RiskBannedParam;
import com.ytpm.risk.param.RiskDeblockingListParam;
import com.ytpm.risk.param.RiskRelativeAppParam;
import com.ytpm.risk.param.RiskUserStatusParam;
import com.ytpm.risk.view.RiskBannedListView;
import com.ytpm.risk.view.RiskDeblockingListView;
import com.ytpm.service.RiskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "风控管理模块")
@RestController
@RequestMapping("/riskManage")
public class RiskManageController {

    @Resource
    private RiskService riskService;

    /**
     * 用户禁登
     */
    @ApiOperation("禁止用户登录")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path")
    @GetMapping("/forbidLogin")
    public Result<?> forbidLogin(@RequestParam("userId") String userId,@RequestParam("appId")String appId, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        return riskService.lockUser(userId,appId, userInfo);
    }

    /**
     * 封禁用户
     */
    @ApiOperation("封禁用户")
    @PostMapping("/banned")
    public Result<?> banned(@RequestBody RiskBannedParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        param.setOperator(userInfo.getUserId());
        param.setOperatorName(userInfo.getNickName());
        param.setAgentId(userInfo.getUserId());
        return riskService.banned(param);
    }

    /**
     * 查询风控记录
     */
    @ApiOperation("查询风控用户记录")
    @PostMapping("/bannedList")
    public ResultTable<RiskBannedListView> bannedList(@RequestBody RiskBannedListParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        param.setAgentId(userInfo.getUserId());
        return riskService.bannedList(param);
    }

    /**
     * 查询解封记录
     */
    @ApiOperation("查询解封记录")
    @PostMapping("/deblockingList")
    public ResultTable<RiskDeblockingListView> deblockingList(@RequestBody RiskDeblockingListParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        param.setAgentId(userInfo.getUserId());
        return riskService.deblockingList(param);
    }

    /**
     * 修改用户状态
     */
    @ApiOperation("修改用户状态")
    @PostMapping("/changeUserStatus")
    public Result<?> changeUserStatus(@RequestBody RiskUserStatusParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        param.setOperator(userInfo.getUserId());
        param.setOperatorName(userInfo.getNickName());
        param.setAgentId(userInfo.getUserId());
        return riskService.changeUserStatus(param);
    }

    /**
     * 风控配置关联应用
     */
    @ApiOperation("风控配置关联应用")
    @PostMapping("/relativeApp")
    public Result<?> relativeApp(@RequestBody RiskRelativeAppParam param, @ApiIgnore @AuthenticationPrincipal AgentUserInfo userInfo) {
        param.setOperator(userInfo.getUserId());
        param.setOperatorName(userInfo.getNickName());
        return riskService.relativeApp(param);
    }

    @ApiOperation("查询今日每小时的封禁数和封控数（按封禁记录时间）")
    @PostMapping ("/queryBannedByHourToday")
    public Result<Map<String, int[]>> queryBannedByHourToday(@RequestBody AppQueryUserTodayTimeParam appQueryUserTodayTimeParam) {
        Map<String, int[]> resultMap = new HashMap<>();
        int[] lockCounts = new int[24];
        int[] riskCounts = new int[24];

        // 获取今天的开始和结束时间
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        LocalDateTime endOfDay = startOfDay.plusDays(1).minusSeconds(1);

        Date startTime = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endTime = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        // 查询今天封禁记录（根据封禁时间字段过滤）
        List<YtPlatformBanned> bannedList = riskService.queryBannedRecord(startTime, endTime,appQueryUserTodayTimeParam.getAppIdList());

        if (CollUtil.isNotEmpty(bannedList)) {
            for (YtPlatformBanned banned : bannedList) {
                Date bannedTime = banned.getBannedTime(); // 替换为你实际的封禁时间字段
                if (bannedTime != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(bannedTime);
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    if (hour >= 0 && hour < 24) {
                          lockCounts[hour]++;
                        }
                }
            }
        }
        resultMap.put("lockHour", lockCounts);
        resultMap.put("riskHour", riskCounts);
        return Result.resultObjOk(resultMap);
    }




}
