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

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.common.annotion.BussinessLog;
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.model.response.SuccessResponseData;
import com.casic.missiles.modular.system.dict.BusConfigDict;
import com.casic.missiles.modular.system.dto.BusConfigDTO;
import com.casic.missiles.modular.system.dto.DeviceTypeEnum;
import com.casic.missiles.modular.system.service.IBusConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/busConfig")
public class WellCoverController   extends ExportController {

    @Resource
    private AbstractPermissionContext service;
    @Qualifier("busWellCoverServiceImpl")
    @Autowired
    private IBusConfigService busWellCoverService;

    /**
     * 井盖状态监测仪下发配置接口
     */
    @RequestMapping(value = "/wellcoverConfig/config")
    @BussinessLog(value = "井盖状态监测仪下发配置接口", key = "deviceId,uploadPeriod,collectInterval,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
    public Object wellcoverConfig(@RequestBody BusConfigDTO busConfigDTO) {
        try {
            busWellCoverService.addConfig(busConfigDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
        }
        return ResponseData.success();
    }

    /**
     * 井盖状态监测仪下发配置接口
     */
    @RequestMapping(value = "/wellcoverConfig/batchImport")
    @ResponseBody
    @BussinessLog(value = "井盖状态监测仪批量导入设备配置", key = "deviceId,uploadPeriod,collectInterval,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
    public Object wellcoverConfigBatchImport(@RequestParam("file") MultipartFile uploadFile) throws Exception {
        SuccessResponseData resultData = new SuccessResponseData();
        if (null == uploadFile) {
            resultData.setCode(400);
            resultData.setMessage("上传失败，无法找到文件！");
            return resultData;
        }
        try {
            List<BusConfigDTO> busConfigDTOList = this.importExcel(BusConfigDTO.class, uploadFile, null, null);
            resultData = busWellCoverService.imp(busConfigDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultData.setCode(400);
            resultData.setMessage("导入失败");
        }
        return resultData;
    }

    /**
     * 井盖状态监测仪下发配置接口
     */
    @RequestMapping(value = "/wellcoverConfig/list")
    @ResponseBody
    @BussinessLog(value = "获取井盖状态监测仪配置表", key = "deviceId,uploadPeriod,collectInterval,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
    public Object wellcoverConfigList(@RequestParam(required = false) String keywords,
                                      @RequestParam(required = false) String deptid)  {
        SuccessResponseData successResult = new SuccessResponseData();
        try {
            Page<BusConfigDTO> page = PageFactory.defaultPage();//page使用
            DataScope dataScope = service.getAuthService().getLoginUserDataScope();
            List<BusConfigDTO> busConfigDTOList = busWellCoverService.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.Well.getIndex()), dataScope);
            page.setRecords(busConfigDTOList);
            successResult.setData(super.packForBT(page));
            successResult.setMessage("success");
        } catch (Exception e) {
            e.printStackTrace();
            successResult.setCode(500);
            successResult.setMessage("false");
        }
        return successResult;
    }
    /**
     * 井盖状态监测仪下发配置接口
     */
    @RequestMapping(value = "/wellcoverConfig/batchExport")
    @ResponseBody
    @BussinessLog(value = "获取井盖状态监测仪配置表", key = "deviceId,uploadPeriod,collectInterval,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
    public void wellcoverConfigBatchExport(@RequestParam(required = false) String keywords,
                                             @RequestParam(required = false) String deptid,
                                             HttpServletResponse response) {
        try {
            Page<BusConfigDTO> page = PageFactory.defaultPage();
            page.setCurrent(1);
            page.setSearchCount(false);
            page.setSize(Integer.MAX_VALUE);
            DataScope dataScope = service.getAuthService().getLoginUserDataScope();
            List<BusConfigDTO> busConfigDTOList = busWellCoverService.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.Well.getIndex()), dataScope);
            busWellCoverService.exp(busConfigDTOList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
