package com.casic.missiles.modular.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.enums.DeviceExportEnum;
import com.casic.missiles.modular.system.model.GasDectorDataDto;
import com.casic.missiles.modular.system.model.PantiltDataDto;
import com.casic.missiles.modular.system.service.ThirdPartyDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

@Slf4j
@RestController
public class ThirdPartyController extends ExportController {

    private final AbstractPermissionContext permissionService;

    private final ThirdPartyDataService thirdPartyService;

    public ThirdPartyController(AbstractPermissionContext permissionService, ThirdPartyDataService thirdPartyService) {
        this.permissionService = permissionService;
        this.thirdPartyService = thirdPartyService;
    }

    /**
     * 第三方燃气列表
     */
    @RequestMapping("/gasdector/list-page")
    public Object getGasDectorListPage(String keywords, String endTime, String beginTime, String deptid) {
        Page<GasDectorDataDto> page = PageFactory.defaultPage();
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        ResponseData responseData = thirdPartyService.getGasDectorListPage(page, dataScope, deptid, endTime, beginTime, keywords);
        return ResponseData.success(super.packForBT(page));
    }

    /**
     * 第三方燃气导出
     */
    @RequestMapping("/gasdector/export")
    public void getGasDectorExport(String keywords, String endTime, String beginTime, String deptid) {
        Page<GasDectorDataDto> page = PageFactory.defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        thirdPartyService.getGasDectorListPage(page, dataScope, deptid, endTime, beginTime, keywords);
        try {
            super.exportExcel(GasDectorDataDto.class, page.getRecords(), DeviceExportEnum.GASDATA_EXPORT.getFileName());
        } catch (IOException iex) {
            log.error("主题，第三方燃气数据导出异常,关键字{}，部门编号{},异常信息{}", keywords, dataScope, iex);
        }
    }

    /**
     * 第三方甲烷列表
     */
    @RequestMapping("/pantilt/list-page")
    public Object getPantiltDataListPage(String keywords, String endTime, String beginTime, String deptid) {
        Page<PantiltDataDto> page = PageFactory.defaultPage();
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        ResponseData responseData = thirdPartyService.getPantiltDataListPage(page, dataScope, deptid, endTime, beginTime, keywords);
        responseData.setData(super.packForBT(page));
        return responseData;
    }

    /**
     * 第三方甲烷导出
     */
    @RequestMapping("/pantilt/export")
    public void getPantiltDataExport(String keywords, String endTime, String beginTime, String deptid) {
        Page<PantiltDataDto> page = PageFactory.defaultPage();
        page.setCurrent(1);
        page.setSearchCount(false);
        page.setSize(Integer.MAX_VALUE);
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        thirdPartyService.getPantiltDataListPage(page, dataScope, deptid, endTime, beginTime, keywords);
        try {
            super.exportExcel(PantiltDataDto.class, page.getRecords(), DeviceExportEnum.METHANE_EXPORT.getFileName());
        } catch (IOException iex) {
            log.error("主题，第三方甲烷数据导出异常,关键字{}，部门编号{},异常信息{}", keywords, dataScope, iex);
        }
    }

    /**
     * 上传数据总记录数
     */
    @RequestMapping("/dataStatics/upLoadCountByTime")
    public Object countThirdPartyData(String endTime, String beginTime) {
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        return thirdPartyService.countThirdPartyData(dataScope, endTime, beginTime);
    }

    /**
     * 上传数据总记录数
     */
    @RequestMapping("/dataStatics/upLoadCountToday")
    public Object countThirdPartyDataToday() {
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        return thirdPartyService.countThirdPartyDataToday(dataScope);
    }

}
