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

import com.baomidou.mybatisplus.plugins.Page;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.base.response.SuccessResponseData;
import com.casic.missiles.core.common.annotion.BussinessLog;
import com.casic.missiles.core.common.constant.factory.PageFactory;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
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 io.swagger.annotations.ApiOperation;
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.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author dev
 * @Date 2019-05-16 15:55:07
 */
@Controller
@RequestMapping("/busConfigBak")
public class BusConfigControllerBak extends BaseController {


    @Qualifier("busConfigServiceImpl")
    @Autowired
    private IBusConfigService busConfigService;

    @Qualifier("busMethaneConfigServiceImpl")
    @Autowired
    private IBusConfigService busMethaneConfigService;

    @Qualifier("busWasteGasConfigServiceImpl")
    @Autowired
    private IBusConfigService busWasteGasConfigService;

    @Qualifier("busTempHumiConfigServiceImpl")
    @Autowired
    private IBusConfigService busTempHumiConfigService;


    @Qualifier("busDigConfigServiceImpl")
    @Autowired
    private IBusConfigService busDigConfigService;

    @Autowired
    private ICommonPermissionService service;

    private Map<Integer, IBusConfigService> busConfigServiceHashMap;

    public void init() {
        if (busConfigServiceHashMap!=null) return;
        busConfigServiceHashMap = new HashMap<Integer, IBusConfigService>();
        {
            busConfigServiceHashMap.put(DeviceTypeEnum.Liquid.getIndex(), busConfigService);
            busConfigServiceHashMap.put(DeviceTypeEnum.Methane.getIndex(), busMethaneConfigService);
            busConfigServiceHashMap.put(DeviceTypeEnum.WasteGas.getIndex(), busWasteGasConfigService);
            busConfigServiceHashMap.put(DeviceTypeEnum.TempHumi.getIndex(), busTempHumiConfigService);
            busConfigServiceHashMap.put(DeviceTypeEnum.Dig.getIndex(), busDigConfigService);
        }
    }

    /**
     * 获取列表deptid
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String keywords,
                       @RequestParam(required = false) String deptid,
                       @RequestParam(required = true) Long deviceType) {
        SuccessResponseData successResult = new SuccessResponseData();
        try {
            init();
            IBusConfigService iBusConfigService =
                    busConfigServiceHashMap.get(deviceType.intValue());
            Page<BusConfigDTO> page = new PageFactory<BusConfigDTO>().defaultPage();//page使用
            DataScope dataScope = service.getCurrUserDataScope();
            List<BusConfigDTO> busConfigDTOList = iBusConfigService.getConfigList(page, keywords, deptid, "", deviceType, 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 = "/config")
    @ResponseBody
    @BussinessLog(value = "新增配置", key = "deviceId,percent,installHeight,retryTimes," +
            "uploadPeriod,collectInterval,collectCount,collectTime,sleepStartTime" +
            "attemptsMax,ip,port", dict = BusConfigDict.class)
    public Object add(BusConfigDTO busConfigDTO) {
        try {
            init();
            IBusConfigService iBusConfigService =
                    busConfigServiceHashMap.get(busConfigDTO.getDeviceType().intValue());
            if (null != iBusConfigService) {
                iBusConfigService.addConfig(busConfigDTO);
            } else
                return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
        }
        return ResponseData.success();
    }
//    /**
//     * 新增燃气配置
//     */
//    @RequestMapping(value = "/configMethane")
//    @ResponseBody
//    @BussinessLog(value = "新增燃气配置", key = "deviceId,uploadPeriod,collectInterval,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
//    public Object addMethane(BusConfigDTO busConfigDTO) {
//        try {
//            init();
//            IBusConfigService iBusConfigService = busConfigServiceHashMap.get(busConfigDTO.getDeviceType().intValue());
//            if (null != iBusConfigService) {
//                iBusConfigService.addConfig(busConfigDTO);
//            } else
//                return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
//        }
//        return ResponseData.success();
//    }


    /**
     * 删除
     */
    @ApiOperation("删除配置")
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除配置", key = "busConfigId", dict = BusConfigDict.class)
    @ResponseBody
    public Object delete(@RequestParam String busConfigId) {
        SuccessResponseData successResult = new SuccessResponseData();
        try {
            busConfigService.deleteById(busConfigId);
            successResult.setCode(200);
            successResult.setMessage("success");
        } catch (Exception e) {
            e.printStackTrace();
            successResult.setCode(500);
            successResult.setMessage("false");
        }
        return successResult;
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/batchDelete")
    @ResponseBody
    public Object delete(@RequestParam("busConfigIds") List<String> busConfigIds) {
        busConfigService.deleteBatchIds(busConfigIds);
        return ResponseData.success();
    }

//    /**
//     * 修改
//     */
//    @RequestMapping(value = "/update")
//    @BussinessLog(value = "更新配置", key = "deviceId,percent,installHeight,retryTimes,attemptsMax,ip,port", dict = BusConfigDict.class)
//    @ResponseBody
//    public Object update(BusConfigDTO busConfigDTO) {
//        SuccessResponseData successResult = new SuccessResponseData();
//        try {
//            init();
//            IBusConfigService iBusConfigService =
//                    busConfigServiceHashMap.get(busConfigDTO.getDeviceType().intValue());
//            if(null!=iBusConfigService)
//                iBusConfigService.updateConfig(busConfigDTO);
//            successResult.setCode(200);
//            successResult.setMessage("success");
//        } catch (Exception e) {
//            e.printStackTrace();
//            successResult.setCode(500);
//            successResult.setMessage("false");
//        }
//        return successResult;
//    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{busConfigId}")
    @ResponseBody
    public Object detail(@PathVariable("busConfigId") String busConfigId) {
        SuccessResponseData resultData = new SuccessResponseData();
        resultData.setCode(200);
        resultData.setMessage("查询详情成功");
        resultData.setData(busConfigService.selectById(busConfigId));
        return resultData;
    }


    /****
     * 配置批量导入
     * @param uploadFile
     * @param deviceType 设备类型
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/batchImport", method = RequestMethod.POST)
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile uploadFile,
                         @RequestParam(required = true) Long deviceType) throws Exception {
        SuccessResponseData resultData = new SuccessResponseData();
        if (null == uploadFile) {
            resultData.setCode(400);
            resultData.setMessage("上传失败，无法找到文件！");
            return resultData;
        }
        init();
        IBusConfigService iBusConfigService = busConfigServiceHashMap.get(deviceType.intValue());
        resultData = iBusConfigService.imp(uploadFile);
        return resultData;
    }


    /***
     * 配置导出
     * @param keywords
     * @param deptid
     * @param deviceType 设备类型id
     * @param response
     */
    @ApiOperation("配置列表导出")
    @RequestMapping(value = "/batchExport")
    @ResponseBody
    public void export(@RequestParam(required = false) String keywords,
                       @RequestParam(required = false) String deptid,
                       @RequestParam(required = true) Long deviceType,
                       HttpServletResponse response) {
        try {
            init();
            IBusConfigService iBusConfigService =
                    busConfigServiceHashMap.get(deviceType.intValue());
            Page<BusConfigDTO> page = new PageFactory<BusConfigDTO>().defaultPage();//page使用
            page.setCurrent(1);
            page.setSearchCount(false);
            page.setSize(Integer.MAX_VALUE);
            DataScope dataScope = service.getCurrUserDataScope();
            List<BusConfigDTO> busConfigDTOList = iBusConfigService.getConfigList(page, keywords, deptid, "", deviceType, dataScope);
            iBusConfigService.exp(busConfigDTOList,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
