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.dto.LiquidBusConfigDTO;
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.*;
import org.springframework.web.multipart.MultipartFile;

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

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

    @Resource
    private AbstractPermissionContext service;
    @Qualifier("busConfigServiceImpl")
    @Autowired
    private IBusConfigService busConfigService;
    /**
     * 液位获取配置列表
     */
    @RequestMapping(value = "/liquidConfig/list")
    public Object liquidConfigList(@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 = busConfigService.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.Liquid.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 = "/liquidConfig/config")
    @BussinessLog(value = "新增配置", key = "deviceId,percent,installHeight,retryTimes," +
            "attemptsMax,ip,port", dict = BusConfigDict.class)
    public Object addLiquid(BusConfigDTO busConfigDTO) {
        try {
            busConfigService.addConfig(busConfigDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
        }
        return ResponseData.success();
    }


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


    /***
     * 液位配置导出
     * @param keywords
     * @param deptid
     * @param response
     */
    @RequestMapping(value = "/liquidConfig/batchExport")
    public void export(@RequestParam(required = false) String keywords,
                       @RequestParam(required = false) String deptid,
                       HttpServletResponse response) {
        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 = busConfigService.getConfigList(page, keywords, deptid, Long.valueOf(DeviceTypeEnum.Liquid.getIndex()), dataScope);
            busConfigService.exp(busConfigDTOList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
