package org.springblade.capcancel.controller;


import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springblade.capcancel.entity.CapNoKcYes;
import org.springblade.capcancel.service.ICapNoKcYesService;
import org.springblade.capcancel.style.CellColorSheetWriteHandler;
import org.springblade.capcancel.vo.CapNoKcYesVO;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 本地有CAP没有 前端控制器
 * </p>
 *
 * @author zbc
 * @since 2021-08-26
 */

@RestController
@RequestMapping("/capNoKcYes")
@Api(value = "本地有CAP没有", tags = "本地有CAP没有")
public class CapNoKcYesController {

	@Autowired
	private ICapNoKcYesService capNoKcYesService;


	@GetMapping("/page")
	@ApiOperation(value = "列表")
	public R page(String month, Query query) {
		if(StringUtils.isBlank(month)){
			return R.fail("参数异常");
		}
		IPage<CapNoKcYes> page = Condition.getPage(query);
		IPage<CapNoKcYes> pages = capNoKcYesService.page(page, new LambdaQueryWrapper<CapNoKcYes>()
			.eq(CapNoKcYes::getThemonth,month)
			.eq(CapNoKcYes::getOutput,1)
			.orderByAsc(CapNoKcYes::getWeight)
		);
		return R.data(pages);
	}


	@GetMapping("/export")
	@ApiOperation(value = "导出")
	public void export(String month, HttpServletResponse response) throws IOException {
		List<CapNoKcYes> list = capNoKcYesService.list(new LambdaQueryWrapper<CapNoKcYes>()
			.eq(CapNoKcYes::getThemonth, month)
			.eq(CapNoKcYes::getOutput, 1)
			.orderByAsc(CapNoKcYes::getWeight)
		);
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("cap", "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		List<Integer> columnIndexs = Arrays.asList(0,1,2,3);
		List<Integer> rowIndexs = new ArrayList<Integer>();
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getIsbold() == 1){
				rowIndexs.add(i+1);
			}
		}

		CellColorSheetWriteHandler colorSheetWriteHandler = new CellColorSheetWriteHandler(rowIndexs, columnIndexs, IndexedColors.RED.index);
		ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(colorSheetWriteHandler).build();
		List<CapNoKcYesVO> voList = new ArrayList<CapNoKcYesVO>();

		if(list != null && !list.isEmpty()) {
			list.forEach(item -> voList.add(BeanUtil.copyProperties(item, CapNoKcYesVO.class)));
		}

		WriteSheet build = EasyExcel.writerSheet(0,"sheet").head(CapNoKcYesVO.class).build();
		excelWriter.write(voList,build);
		excelWriter.finish();
	}



}

