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.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.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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

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

    @Resource
    private AbstractPermissionContext service;

    @Qualifier("busH2sConfigServiceImpl")
    @Autowired
    private IBusConfigService busH2sConfigServiceImpl;

    /**
     * 硫化氢配置列表
     */
    @RequestMapping(value = "/H2sConfig/config")
    @ResponseBody
    public Object H2sConfigCommand(BusConfigDTO busConfigDTO) {
        SuccessResponseData successResult = new SuccessResponseData();
        try {
            busH2sConfigServiceImpl.addConfig(busConfigDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "配置失败");
        }
        return ResponseData.success();
    }


    /**
     * 硫化氢配置列表
     */
    @RequestMapping(value = "/H2sConfig/batchExport")
    @ResponseBody
    public void H2sConfigBatchExport(@RequestParam(required = false) String keywords,
                                       @RequestParam(required = false) String deptid,
                                       HttpServletResponse response) {
        try {
            //page使用
            Page<BusConfigDTO> page = PageFactory.defaultPage();
            DataScope dataScope = service.getAuthService().getLoginUserDataScope();
            List<BusConfigDTO> busConfigDTOList = busH2sConfigServiceImpl.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.H2s.getIndex()), dataScope);
            busH2sConfigServiceImpl.exp(busConfigDTOList, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 硫化氢配置列表
     */
    @RequestMapping(value = "/H2sConfig/batchImport")
    @ResponseBody
    public Object H2sConfigBatchImport(@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 = busH2sConfigServiceImpl.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.H2s.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 = "/H2sConfig/list")
    @ResponseBody
    public Object H2sConfig(@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 = busH2sConfigServiceImpl.getConfigList(page, keywords,
                    deptid, Long.valueOf(DeviceTypeEnum.H2s.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;
    }

}
