package com.huimv.management.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.huimv.management.entity.ThresholdManagementEntity;
import com.huimv.management.service.ThresholdManagementService;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.R;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 报警阈值管理Controller
 * </p>
 *
 * @author yinhao
 * @version 1.0
 * @date 2021-06-15 13:01:37
 */
@Slf4j
@Api(tags = "报警阈值管理Controller")
@RestController
@RequestMapping("management/thresholdManagement")
public class ThresholdManagementController {

    @Autowired
    private ThresholdManagementService thresholdManagementService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = thresholdManagementService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        ThresholdManagementEntity thresholdManagement = thresholdManagementService.getById(id);

        return R.ok().put("thresholdManagement", thresholdManagement);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ThresholdManagementEntity thresholdManagement) {
        thresholdManagementService.save(thresholdManagement);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody ThresholdManagementEntity thresholdManagement) {
        thresholdManagementService.updateById(thresholdManagement);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        thresholdManagementService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/selectByType/{type}")
    public Map<String,Double> selectByType(@PathVariable("type") Short type) {
        return thresholdManagementService.selectByType(type);
    }

    @GetMapping("/exportXls")
    public ModelAndView exportXls(ThresholdManagementEntity thresholdManagementEntity, HttpServletRequest request) {

        LambdaQueryWrapper<ThresholdManagementEntity> queryWrapper = Wrappers.lambdaQuery(thresholdManagementEntity);
        List<ThresholdManagementEntity> list = thresholdManagementService.list(queryWrapper);

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "阈值列表");
        mv.addObject(NormalExcelConstants.CLASS, ThresholdManagementEntity.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("阈值列表数据", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, list);

        return mv;
    }


    /*@PostMapping("/importExcel")
    @Transactional(rollbackFor = Throwable.class)
    public R importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            // 获取上传文件对象
            MultipartFile file = entity.getValue();
            ImportParams params = new ImportParams();
            params.setTitleRows(1);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<ThresholdManagementEntity> list = ExcelImportUtil.importExcel(file.getInputStream(), ThresholdManagementEntity.class, params);
                for (ThresholdManagementEntity thresholdManagementEntity : list) {
                    String thresholdType = thresholdManagementEntity.getThresholdType();
                    switch (thresholdType) {
                        case 1:
                        case 2:
                        case 3:
                            thresholdManagementEntity.setBelongTo((short) 1);
                            break;
                        case 4:
                            thresholdManagementEntity.setBelongTo((short) 4);
                            break;
                        case 5:
                        case 6:
                        case 7:
                            thresholdManagementEntity.setBelongTo((short) 2);
                            break;
                        case 8:
                        case 9:
                        case 10:
                            thresholdManagementEntity.setBelongTo((short) 3);
                            break;
                        default:
                            break;
                    }
                    thresholdManagementService.save(thresholdManagementEntity);
                }
            } catch (Exception e) {
                log.error(e.getMessage(),e);
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    log.error(e.getMessage(),e);
                }
            }
        }
        return R.ok();

    }*/

}
