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.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

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

@Controller
@RequestMapping("/busConfig")
public class GasLiquidController extends ExportController {


    @Qualifier("busGasLiquidConfigServiceImpl")
    @Autowired
    private IBusConfigService busGasLiquidConfigService;

    @Resource
    private AbstractPermissionContext service;

    /**
     * 燃气液位一体化获取配置列表
     */
    @RequestMapping(value = "/liquidGasConfig/list")
    @ResponseBody
    public Object liquidGasConfigList(@RequestParam(required = false) String keywords,
                                      @RequestParam(required = false) String deptid) {
        SuccessResponseData successResult = new SuccessResponseData();
        try {
            //page使用
            Page<BusConfigDTO> page = PageFactory.defaultPage();
            DataScope dataScope = service.getAuthService().getLoginUserDataScope();
            List<BusConfigDTO> busConfigDTOList = busGasLiquidConfigService.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.LG.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 = "/liquidGasConfig/batchExport")
    @ResponseBody
    public Object liquidGasConfigBatchExport(@RequestParam(required = false) String keywords,
                                             @RequestParam(required = false) String deptid,
                                             HttpServletResponse response) {
        SuccessResponseData successResult = new SuccessResponseData();
        try {
            //page使用
            Page<BusConfigDTO> page = PageFactory.defaultPage();
            page.setCurrent(1);
            page.setSearchCount(false);
            page.setSize(Integer.MAX_VALUE);
            DataScope dataScope = service.getAuthService().getLoginUserDataScope();
            List<BusConfigDTO> busConfigDTOList = busGasLiquidConfigService.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.LG.getIndex()), dataScope);
            busGasLiquidConfigService.exp(busConfigDTOList, response);
            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 = "/liquidGasConfig/config")
    @ResponseBody
    @BussinessLog(value = "新增燃气液位一体化配置", key = "deviceId,uploadPeriod,collectInterval,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
    public Object addGasLiquid(BusConfigDTO busConfigDTO) {
        try {
            busGasLiquidConfigService.addConfig(busConfigDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
        }
        return ResponseData.success();
    }


    /****
     * 燃气液位配置批量导入
     * @param uploadFile
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/gasLiquidConfig/batchImport", method = RequestMethod.POST)
    @ResponseBody
    public Object gasLiuqidConfig(@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 = busGasLiquidConfigService.imp(busConfigDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultData.setCode(400);
            resultData.setMessage("导入失败");
        }
        return resultData;
    }


}
