package com.sptg.gateway.controller.statistics;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.dto.ResidentialDto;
import com.sptg.estate.api.dto.UserDto;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.estate.api.feigns.UserFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.statistics.api.constant.StatisticsSecurityToken;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * title: StatisticsUserController
 * package: com.sptg.gateway.controller.statistics
 * description: 住户统计-网关控制器
 *
 *                
 *                 
 */
@RestController
@RequestMapping(value = "/statistics/statisticsUser")
@AllArgsConstructor
public class StatisticsUserController {
    private final UserFeign userFeign;
    private final ResidentialFeign residentialFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;

    /**
     * 功能描述:小区住户统计页面
     * @return ModelAndView
     *                
     *                       
     */
    @RequestMapping("/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_USER_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("statistics/statistics-user");
        return modelAndView;
    }

    /**
     * 功能描述:统计 总住户数(userTotalCount)，上月新增住户数(lastMonthNewUserCount)，租客数(tenantUserCount)，已认证小程序住户数(weiXinUserCount)
     * @return ResultBean<Map<String, Object>>
     *                
     *                       
     */
    @RequestMapping("/statisticsUserCount")
    @Security(token = StatisticsSecurityToken.STATISTICS_USER_MANAGE)
    public ResultBean<Map<String, Object>> statisticsUserCount(){
        UserDto userDto=new UserDto();
        //机构位置
        Account account=this.securityServiceInterface.getOnLineUser();
        userDto.setOrgPosition(account.getOrgPosition());
        ResultBean<Map<String, Object>> resultBean = userFeign.statisticsUserCount(userDto);
        return resultBean;
    }

    /**
     * 功能描述:查询按小区统计人数
     * @param residentialDto
     * @return ResultBean<Page<Residential>>
     *                
     *                       
     */
    @RequestMapping("/statisticsUserCountList")
    @Security(token = StatisticsSecurityToken.STATISTICS_USER_MANAGE)
    public ResultBean<Page<Residential>> statisticsUserCountList(ResidentialDto residentialDto, Page page){
        residentialDto.setPage(page);
        //机构位置
        Account account=this.securityServiceInterface.getOnLineUser();
        residentialDto.setOrgPosition(account.getOrgPosition());
        ResultBean<Page<Residential>> resultBean = residentialFeign.statisticsUserCountList(residentialDto);
        return resultBean;
    }

    @RequestMapping(value = "/exportUser")
    @Security(token = StatisticsSecurityToken.STATISTICS_USER_EXPORT)
    public void exportUser(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/7");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
