package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.domain.analysis.BasDeviceParam;
import cn.cecep.talroad.service.analysis.BasDeviceParamService;
import cn.cecep.talroad.vo.query.analysis.BasDeviceParamQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.lang.Collections;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lixianghou
 */
@Api(tags = "参数管理--设备参数分析设置")
@RestController("basDeviceParamNewController")
@RequestMapping("/deviceParamAnalysis")
public class BasDeviceParamController {

    @Autowired
    @Qualifier("basDeviceParamNewService")
    private BasDeviceParamService service;

    @ApiOperation(value = "查询设备参数分析设置列表")
    @GetMapping("/list")
    public AjaxResults<IPage<BasDeviceParam>> list(BasDeviceParamQuery param){

        IPage<BasDeviceParam> list  = service.pageService(param);
        return  AjaxResults.success(list);
    }

    @ApiOperation(value = "导出设备参数分析设置")
    @Log(title = "导出设备参数分析设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody BasDeviceParamQuery query) {
        query.setPageSize(Integer.MAX_VALUE);
        List<BasDeviceParam> list = service.pageService(query).getRecords();
        list.forEach(item -> {
            item.setDataType(item.getDataType().trim());
        });
        ExcelUtil<BasDeviceParam> util = new ExcelUtil<>(BasDeviceParam.class);
        util.hideColumnNative(0);
        util.exportExcel(response, list, "设备参数分析设置列表");
    }

    @ApiOperation(value = "修改设备参数分析设置")
    @Log(title = "修改设备参数分析设置", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public void update(@RequestBody BasDeviceParam bean) {
        service.updateById(bean);
    }


    @ApiOperation(value = "导入")
    @PostMapping("/import")
    @Log(title = "导入---批量修改设备参数分析设置", businessType = BusinessType.IMPORT)
    public AjaxResults<Boolean> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasDeviceParam> util = new ExcelUtil<>(BasDeviceParam.class);
        List<BasDeviceParam> voList = util.importExcel(file.getInputStream());
        List<BasDeviceParam> datas = voList.stream().filter(v -> v.getId()!=null).collect(Collectors.toList());
        if(Collections.isEmpty(datas)){
            return AjaxResults.success(true);
        }
        int quantityPerTime=2500;
        int limit=(datas.size()+quantityPerTime-1)/quantityPerTime;
        //分组
        List<List<BasDeviceParam>> dataList = Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(a ->
                datas.stream().skip(a * quantityPerTime).limit(quantityPerTime).parallel().collect(Collectors.toList())).collect(Collectors.toList());
        //每组 quantityPerTime 条批次
        for (List<BasDeviceParam> d : dataList) {
            service.updateBatchSelective(d);
        }
        return AjaxResults.success(true);
    }
}
