package com.sptg.gateway.controller.statistics;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.dto.DeviceAuthorizeDto;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.feigns.DeviceAuthorizeFeign;
import com.sptg.statistics.api.constant.StatisticsSecurityToken;
import com.sptg.statistics.api.dto.StatisticsDeviceDto;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: StatisticsDeviceController
 * package: com.sptg.gateway.controller.statistics
 * description: 统计设备-网关控制器
 *
 *                
 *                 
 */
@RestController
@RequestMapping(value = "/statistics/statisticsDevice")
@AllArgsConstructor
public class StatisticsDeviceController {
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final DeviceAuthorizeFeign deviceAuthorizeFeign;

    /**
     * 功能描述:设备统计页面
     * @return ModelAndView
     *                
     *                       
     */
    @RequestMapping("/index")
    @Security(token = StatisticsSecurityToken.STATISTICS_DEVICE_MANAGE)
    public ModelAndView index(){
        ModelAndView modelAndView=new ModelAndView("statistics/statistics-device");
        return modelAndView;
    }

    /**
     * 功能描述:统计  设备总数(deviceCount)  小区门口设备总数(residentialDeviceCount) 单元设备总数(unitDeviceCount)  停车场设备总数(parkDeviceCount)
     *
     * @return ResultBean<Map<String,Object>>
     *                
     *                       
     */
    @RequestMapping("/statisticsDeviceTotal")
    @Security(token = StatisticsSecurityToken.STATISTICS_DEVICE_MANAGE)
    public ResultBean<Map<String,Object>> statisticsDeviceTotal(){
        //用户
        Account account= this.securityServiceInterface.getOnLineUser();
        //获取机构位置
        DeviceAuthorizeDto deviceAuthorizeDto=new DeviceAuthorizeDto();
        deviceAuthorizeDto.setOrgPosition(account.getOrgPosition());
        //小区门口设备总数(residentialDeviceCount) 单元设备总数(unitDeviceCount)
        Map<String, Object> deviceCountForInstallPathTypeMap = deviceAuthorizeFeign.getDeviceCountForInstallPathType(deviceAuthorizeDto);
        // 设备总数(deviceCount)
        long deviceCount = deviceAuthorizeFeign.getDeviceCount(deviceAuthorizeDto);
        deviceCountForInstallPathTypeMap.put("deviceCount",deviceCount);
        return new ResultBean<>(ResultEnum.SUCCESS,deviceCountForInstallPathTypeMap);
    }

    /**
     * 功能描述:统计每月新增设备/设备总数
     * @param statisticsDeviceDto
     * @return ResultBean<Map>
     *                
     *                       
     */
    @RequestMapping("/statisticsMonthAddDeviceAndTotal")
    @Security(token = StatisticsSecurityToken.STATISTICS_DEVICE_MANAGE)
    public ResultBean<Map> statisticsMonthAddDeviceAndTotal(StatisticsDeviceDto statisticsDeviceDto){
        //用户
        Account account= this.securityServiceInterface.getOnLineUser();
        Map<String,Object> resultMap=new HashMap<String,Object>(OperationConstant.TWO);
        DeviceAuthorizeDto deviceAuthorizeDto=new DeviceAuthorizeDto();
        deviceAuthorizeDto.setOrgPosition(account.getOrgPosition());
        deviceAuthorizeDto.setDeviceRegionProvince(statisticsDeviceDto.getRegionProvince());
        deviceAuthorizeDto.setDeviceRegionCity(statisticsDeviceDto.getRegionCity());
        deviceAuthorizeDto.setDeviceRegionArea(statisticsDeviceDto.getRegionArea());
        //统计类型
        int statisticsType=statisticsDeviceDto.getStatisticsType();
        if(statisticsType==0&& StringUtils.isNotBlank(statisticsDeviceDto.getYear())){
            //某年设备总数
            //上一年
            int upYear=Integer.parseInt(statisticsDeviceDto.getYear())-1;
            deviceAuthorizeDto.setSelectYear(upYear+"");
            long deviceCountYear=deviceAuthorizeFeign.getDeviceCount(deviceAuthorizeDto);
            resultMap.put("deviceCountYear",deviceCountYear);
            //下面要统计当年的数据
            deviceAuthorizeDto.setSelectYear(statisticsDeviceDto.getYear());
        }
        //某年统计每月新增设备数量
        List<Map<String, Object>> maps = deviceAuthorizeFeign.getDeviceCountForMonth(deviceAuthorizeDto);
        resultMap.put("addDeviceCountList",maps);

        return new ResultBean<>(ResultEnum.SUCCESS,resultMap);
    }
}
