package com.sptg.gateway.controller.auth;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.dto.AccessRecordsDto;
import com.sptg.estate.api.dto.ApplyUserDto;
import com.sptg.estate.api.dto.GuestAuthorizeDto;
import com.sptg.estate.api.dto.UserDto;
import com.sptg.estate.api.feigns.*;
import com.sptg.permissions.api.dto.DeviceAuthorizeDto;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import com.sptg.permissions.api.feigns.DeviceAuthorizeFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * title: IndexController
 * description:
 *
 *                  
 *            
 */
@RestController
@RequiredArgsConstructor
public class IndexController {

    private final SecurityServiceInterface<DeviceAdminUser> securityServiceInterface;
    private final AccessRecordsFeign accessRecordsFeign;
    private final GuestAuthorizeFeign guestAuthorizeFeign;
    private final UserFeign userFeign;
    private final ApplyUserFeign applyUserFeign;
    private final DeviceAuthorizeFeign deviceAuthorizeFeign;

    /**
     * @return 获取今日开门次数
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/index/getTodayOpenDoorCount")
    public ResultBean<Integer> getTodayOpenDoorCount(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        AccessRecordsDto dto = new AccessRecordsDto();
        dto.setResidentialId(onLineUser.getXqId());
        Integer count = this.accessRecordsFeign.getAccessRecordsCountByDay(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,count);
    }

    /**
     * @return 获取今日访客数量
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/index/getTodayVisitorNumber")
    public ResultBean<Integer> getTodayVisitorNumber(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        GuestAuthorizeDto dto = new GuestAuthorizeDto();
        dto.setResidentialId(onLineUser.getXqId());
        Integer count = this.guestAuthorizeFeign.getGuestAuthorizeCountByDay(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,count);
    }

    /**
     * @return 获取住户数量
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/index/getHouseUserCount")
    public ResultBean<Integer> getHouseUserCount(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        UserDto dto = new UserDto();
        dto.setResidentialId(onLineUser.getXqId());
        Integer count = this.userFeign.findUserTotalCount(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,count);
    }

    /**
     * @return 获取待审核用户数量
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/index/getWaitAuditUserCount")
    public ResultBean<Integer> getWaitAuditUserCount(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        ApplyUserDto dto = new ApplyUserDto();
        dto.setResidentialId(onLineUser.getXqId());
        dto.setCheckState(0);
        Integer count = this.applyUserFeign.getApplyUserCountByState(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,count);
    }

    /**
     * @return 获取设备数量
     */
    @Security(format = DataFormatEnum.JSON)
    @PostMapping(value = "/auth/index/getDeviceCount")
    public ResultBean<Integer> getDeviceCount(){
        DeviceAdminUser onLineUser = this.securityServiceInterface.getOnLineUser();
        DeviceAuthorizeDto dto = new DeviceAuthorizeDto();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        Long count = this.deviceAuthorizeFeign.getDeviceCount(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,count.intValue());
    }




}
