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.BasThresholdConfig;
import cn.cecep.talroad.service.analysis.BasThresholdConfigService;
import cn.cecep.talroad.vo.query.analysis.BasThresholdConfigQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Api(tags = "参数管理--阈值设置")
@RestController("basThresholdConfigNewController")
@RequestMapping("/thresholdConfigAnalysis")
public class BasThresholdConfigController {

    @Autowired
    @Qualifier("basThresholdConfigNewService")
    private BasThresholdConfigService service;

    @ApiOperation(value = "查询阈值设置列表")
    @GetMapping("/list")
    public AjaxResults<IPage<BasThresholdConfig>> list(BasThresholdConfigQuery param){
        Page<BasThresholdConfig> page = new Page<>(param.getPageNum(), param.getPageSize());
        IPage<BasThresholdConfig> list  = service.page(page, BasThresholdConfigQuery.createLambdaQueryWrapper(param));
        return  AjaxResults.success(list);
    }

    @ApiOperation(value = "导出阈值设置")
    @Log(title = "导出阈值设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody BasThresholdConfigQuery query) {
        query.setPageSize(Integer.MAX_VALUE);
        List<BasThresholdConfig> list = service.list(BasThresholdConfigQuery.createLambdaQueryWrapper(query));
        ExcelUtil<BasThresholdConfig> util = new ExcelUtil<>(BasThresholdConfig.class);
        util.hideColumnNative(0);
        util.exportExcel(response, list, "阈值设置列表");
    }

    @ApiOperation(value = "修改阈值设置")
    @Log(title = "修改阈值设置", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public void update(@RequestBody BasThresholdConfig bean) {
        bean.setUpdateTime(new Date());
        service.updateById(bean);
    }


    @ApiOperation(value = "导入")
    @PostMapping("/import")
    @Log(title = "导入---批量修改阈值设置", businessType = BusinessType.IMPORT)
    public AjaxResults<Boolean> importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasThresholdConfig> util = new ExcelUtil<>(BasThresholdConfig.class);
        List<BasThresholdConfig> voList = util.importExcel(file.getInputStream());
        List<BasThresholdConfig> datas = voList.stream().filter(v -> v.getDataId()!=null).collect(Collectors.toList());
        if(Collections.isEmpty(datas)){
            return AjaxResults.success(true);
        }
        int quantityPerTime=2500;
        int limit=(datas.size()+quantityPerTime-1)/quantityPerTime;
        //分组
        List<List<BasThresholdConfig>> 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<BasThresholdConfig> d : dataList) {
            d.forEach(v -> {
                v.setUpdateTime(new Date());
            });
            service.updateBatchSelective(d);
        }
        return AjaxResults.success(true);
    }
}
