package com.ruoyi.web.controller.backstage;



import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.system.domain.SysScenic;
import com.ruoyi.system.domain.dto.*;
import com.ruoyi.system.domain.vo.AppUserInfoVo;
import com.ruoyi.system.domain.vo.AppUserSetVo;
import com.ruoyi.system.service.*;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.PageUtils.startPage;


@RestController
@RequestMapping("/system/appUser")
public class SysAppUserController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IUserInfoService userInfoService;


    // 统计用户每天的数据
    @Autowired
    private ISysUserStService sysUserStService;

    // 景点相关
    @Autowired
    private ISysScenicService sysScenicService;

    // 景点每天统计数据
    @Autowired
    private IDailyScenicService dailyScenicService;

    // 系统每天统计数据
    @Autowired
    private IDailySysService dailySysService;


    // 当前这里还没写权限管理, 传递参数SysUser user---搜索时使用
    @Log(title = "用户管理--获取所有app用户列表", businessType = BusinessType.GET)
    @GetMapping("/getAllAppUserInfo")
    public TableDataInfo getAllAppUserInfo(SysUser user){

        // 分页
        startPage();
        List<AppUserInfoVo> userInfoList = userInfoService.getAllAppUserInfo(user);

        return getDataTable(userInfoList);


    }

    @Log(title = "用户管理--获取指定id用户信息", businessType = BusinessType.GET)
    @GetMapping("/{userId}")
    public AjaxResult getUserInfoByUserId(@PathVariable  Long userId){

        // 这里面是不是就没有user_type啊,直接解码的token不包含user_type
        // 分页

        AppUserSetVo userSetVo = sysUserService.getUserSetByUserId(userId);

        return AjaxResult.success(userSetVo);

    }

    @Log(title = "用户管理--修改用户状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeUserStatus")
    public AjaxResult changeUserState(@RequestBody UpdateUserStateDto updateUserStateDto){


            Long userId = updateUserStateDto.getUserId();
            String status = updateUserStateDto.getStatus();

            SysUser user = new SysUser();

            user.setUserId(userId);
            user.setStatus(status);

            int back = sysUserService.updateUserStatus(user);

            if(back>0){
                return AjaxResult.success("修改用户账号状态成功");
            }
            else{
                return AjaxResult.error("修改用户账号状态失败, 请联系管理员");
            }
    }

    @Log(title = "用户管理--获取指定id用户信息", businessType = BusinessType.GET)
    @GetMapping("/user/{userId}")
    public AjaxResult getUserByUserId(@PathVariable  Long userId){


        AppUserInfoVo userInfoVo =  userInfoService.getAppUserInfo(userId);

        return AjaxResult.success(userInfoVo);

    }


    // 获取前一天用户总数和大前天用户总数
    @GetMapping("/getUserCount")
    public AjaxResult getUserCount(){

//        // --------------------------------真实数据--------------------------------------
//
//        LocalDate now = LocalDate.now();
//        LocalDate startDate = now.minusDays(2);
//        LocalDate endDate = now.minusDays(1);
//        List<SeUserCountDto> seUserCount = sysUserStService.getUserCountByTime(startDate, endDate);
//
//        SeUserCountDto seUserCountDto1 = seUserCount.get(0);
//        SeUserCountDto seUserCountDto2 = seUserCount.get(1);
//
//        // 当前用户总数
//        Long userCount2 = seUserCountDto2.gettUser();
//
//        // 前一天用户总数
//        Long userCount1 = seUserCountDto1.gettUser();
//
//        Long change = userCount2 - userCount1;
//        AjaxResult ajax = new AjaxResult();
//        ajax.put("userCount", userCount2);
//        ajax.put("change", change);
//        //---------------------------------真实数据end------------------------------------------

        AjaxResult ajax = new AjaxResult();
        ajax.put("userCount", 1234L);
        ajax.put("change", 233L);

        return ajax;

    }

    //获取收入总额和较前天增长的数据
    @GetMapping("/getTodayIncome")
    public AjaxResult getTodayIncome(){

        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(2);
        LocalDate endDate = now.minusDays(1);

//        //--------------------开发环境数据不全-------------------------
//
//        List<SysIncomeDto> sysIncomeDtoList = dailySysService.getSysIncome(startDate, endDate);
//
//        SysIncomeDto sysIncomeDto1 = sysIncomeDtoList.get(0);
//        SysIncomeDto sysIncomeDto2 = sysIncomeDtoList.get(1);
//        Long income2 = sysIncomeDto2.gettIncome();
//        Long income1 = sysIncomeDto1.gettIncome();
//        Long change = income2 - income1;
//        //------------------------开发环境数据不全end----------------------------------------------------

        //------------------------------测试的编造数据-----------------------------------------------
        Long income2 = 100L;
        Long income1 = 90L;
        Long change = income2 - income1;

        AjaxResult ajax = new AjaxResult();
        ajax.put("income", income2);
        ajax.put("change", change);
        //-------------------------------测试的编造数据end--------------------------------------------

        return ajax;

    }

    //获取用户自定参数的在线时长
    @GetMapping("/getOnlineTime/{day}")
    public AjaxResult getOnlineTime(@PathVariable int day){
        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(day+1);
        LocalDate endDate = now.minusDays(1);

        // 实际要返回的方法
        //        List<SeOnlineTimeDto> seOnlineTime = sysUserStService.gettOnlineTime(startDate, endDate);

        //-----------------------测试保证返回数组的长度为day-----------------------
        List<SeOnlineTimeDto> seOnlineTime = new ArrayList<>();
        for(int i=0;i<day;i++){
            SeOnlineTimeDto seOnlineTimeDto = new SeOnlineTimeDto();
            seOnlineTimeDto.setDate(startDate.plusDays(i+1));

            Random random = new Random();
            long minValue = 1000L;
            long maxValue = 999999999999999L;
            long range = maxValue - minValue + 1;
            long randomLong = minValue + (long) (random.nextDouble() * range);

            seOnlineTimeDto.settOnlineTime(randomLong);
            seOnlineTime.add(seOnlineTimeDto);
        }

        //-------------------------测试end---------------------------------------
        return AjaxResult.success(seOnlineTime);
    }

    //获取用户近7天的每天在线时长
    @GetMapping("/getSOnlineTime")
    public AjaxResult getSOnlineTime(){

        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(8);
        LocalDate endDate = now.minusDays(1);

        // 实际要用的返回方法
//        List<SeOnlineTimeDto> seOnlineTime = sysUserStService.gettOnlineTime(startDate, endDate);

        //----------测试保证返回数组的长度为7-----------------------

        List<SeOnlineTimeDto> seOnlineTime = new ArrayList<>();
        for(int i=0;i<7;i++){
            SeOnlineTimeDto seOnlineTimeDto = new SeOnlineTimeDto();
            seOnlineTimeDto.setDate(startDate.plusDays(i+1));
            seOnlineTimeDto.settOnlineTime(123456789L);
            seOnlineTime.add(seOnlineTimeDto);
        }

        //-----------测试结束--------------------------------------------

        return AjaxResult.success(seOnlineTime);

    }

    //获取用户近30天的每天在线时长
    @GetMapping("/getThOnlineTime")
    public AjaxResult getThOnlineTime(){

        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(31);
        LocalDate endDate = now.minusDays(1);

        // 实际要用的返回方法, 根据起止时间决定返回内容
//        List<SeOnlineTimeDto> seOnlineTime = sysUserStService.gettOnlineTime(startDate, endDate);

        //----------测试保证返回数组的长度为7-----------------------

        List<SeOnlineTimeDto> seOnlineTime = new ArrayList<>();
        for(int i=0;i<30;i++){
            SeOnlineTimeDto seOnlineTimeDto = new SeOnlineTimeDto();
            seOnlineTimeDto.setDate(startDate.plusDays(i+1));
            seOnlineTimeDto.settOnlineTime(123456789L);
            seOnlineTime.add(seOnlineTimeDto);
        }

        //-----------测试结束--------------------------------------------

        return AjaxResult.success(seOnlineTime);

    }

    // 获取指定城市,指定天数的每个资源位MR总在线时长
    @PostMapping("/getMRTime")
    public AjaxResult getMRTime(@RequestParam String city, @RequestParam int day){

        System.out.println("传递参数city是" + city + " 传递参数day是 "+ day);

        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(day+1);
        LocalDate endDate = now.minusDays(1);

        //初始化查询对象
        SysScenic sysScenic = new SysScenic();
        sysScenic.setCity(city);

        //获取城市所有景点
        List<SysScenic> sysScenics = sysScenicService.selectSysScenicList(sysScenic);

        List<List<MRTimeDto>> mrTimeDtos = new ArrayList<>();

        //        //------------------------------在线环境代码-------------------------------------------
//        // 查询每个资源位对应的每日统计
//        for(SysScenic sysScenic1:sysScenics){
//            Long scenicId = sysScenic1.getScenicId();
//            List<MRTimeDto> mrTimeDto = dailyScenicService.getMRTime(scenicId, startDate, endDate);
//
//            for(MRTimeDto mrTimeDto1:mrTimeDto){
//                mrTimeDto1.setScenic(sysScenic1.getScenic());
//            }
//
//
//            mrTimeDtos.add(mrTimeDto);
//
//        }
//        return AjaxResult.success(mrTimeDtos);
//
//        //-------------------------------在线环境的测试代码----------------------------------

        //---------------------------------测试用接口--------------------------------------------

        for(SysScenic sysScenic1:sysScenics){
            Long scenicId = sysScenic1.getScenicId();
            List<MRTimeDto> mrTimeDto = new ArrayList<>();
            for(int i=0;i<day;i++){
                MRTimeDto mrTimeDto1 = new MRTimeDto();
                mrTimeDto1.setDate(startDate.plusDays(i+1));

                Random random = new Random();
                long minValue = 1000L;
                long maxValue = 999999999L;
                long range = maxValue - minValue + 1;
                long randomLong = minValue + (long) (random.nextDouble() * range);

                mrTimeDto1.setMrTime(randomLong);
                mrTimeDto1.setScenic(sysScenic1.getScenic());
                mrTimeDto.add(mrTimeDto1);
            }

            mrTimeDtos.add(mrTimeDto);

        }
        return AjaxResult.success(mrTimeDtos);


    }

    //获取资源位近7天指定景点每个资源位MR总在线时长
    @PostMapping("/getSMRTime")
    public AjaxResult getSMRTime(@RequestParam String city){
        System.out.println("传递参数city是" + city);

        LocalDate now = LocalDate.now();
        LocalDate startDate = now.minusDays(8);
        LocalDate endDate = now.minusDays(1);

        //初始化查询对象
        SysScenic sysScenic = new SysScenic();
        sysScenic.setCity(city);

        //获取城市所有景点
        List<SysScenic> sysScenics = sysScenicService.selectSysScenicList(sysScenic);

        List<List<MRTimeDto>> mrTimeDtos = new ArrayList<>();




//        //------------------------------在线环境代码-------------------------------------------
//        // 查询每个资源位对应的每日统计
//        for(SysScenic sysScenic1:sysScenics){
//            Long scenicId = sysScenic1.getScenicId();
//            List<MRTimeDto> mrTimeDto = dailyScenicService.getMRTime(scenicId, startDate, endDate);
//
//            for(MRTimeDto mrTimeDto1:mrTimeDto){
//                mrTimeDto1.setScenic(sysScenic1.getScenic());
//            }
//
//
//            mrTimeDtos.add(mrTimeDto);
//
//        }
//        return AjaxResult.success(mrTimeDtos);
//
//        //-------------------------------在线环境的测试代码----------------------------------


        //---------------------------------测试用接口--------------------------------------------

        for(SysScenic sysScenic1:sysScenics){
            Long scenicId = sysScenic1.getScenicId();
            List<MRTimeDto> mrTimeDto = new ArrayList<>();
            for(int i=0;i<7;i++){
                MRTimeDto mrTimeDto1 = new MRTimeDto();
                mrTimeDto1.setDate(startDate.plusDays(i+1));
                mrTimeDto1.setMrTime(123456789L);
                mrTimeDto1.setScenic(sysScenic1.getScenic());
                mrTimeDto.add(mrTimeDto1);
            }

            mrTimeDtos.add(mrTimeDto);

        }
        return AjaxResult.success(mrTimeDtos);


        //---------------------------------测试用接口结束----------------------------------------



    }




    private TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
}
