package com.gci.boot.modules.customer.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.annotation.Query.handle.QueryGenerator;
import com.gci.boot.common.log.annotation.AddSysLog;
import com.gci.boot.common.util.DateUtils;
import com.gci.boot.common.util.MinioUtils;
import com.gci.boot.common.validated.SaveValidated;
import com.gci.boot.common.validated.UpdateValidated;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.customer.entity.TbCustomerFollowPart;
import com.gci.boot.modules.customer.service.ITbCustomerFollowPartService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 客户回访详情 前端控制器
 * </p>
 *
 * @author xushuhan
 * @since 2023-10-14
 */
@Slf4j
@RestController
@RequestMapping("/customer/customerFollowPart")
public class TbCustomerFollowPartController {

	@Autowired
	private ITbCustomerFollowPartService tbCustomerFollowPartService;
	@Autowired
    private MinioUtils minioUtils;

	@AddSysLog(value = "司机打卡-列表查询")
	@GetMapping("/list")
	public Result<?> list(TbCustomerFollowPart item) {
		LambdaQueryWrapper<TbCustomerFollowPart> queryWrapper = QueryGenerator.initQueryWrapper(TbCustomerFollowPart.class, item);
		queryWrapper.orderByDesc(TbCustomerFollowPart::getSort);
		List<TbCustomerFollowPart> list = tbCustomerFollowPartService.list(queryWrapper);
		return Result.ok(list);
	}

	@AddSysLog(value = "司机打卡-分页查询")
	@GetMapping("/page")
	public Result<?> page(TbCustomerFollowPart item,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
						  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

		LambdaQueryWrapper<TbCustomerFollowPart> queryWrapper = QueryGenerator.initQueryWrapper(TbCustomerFollowPart.class, item);
		queryWrapper.orderByDesc(TbCustomerFollowPart::getSort);
		Page<TbCustomerFollowPart> page = new Page<TbCustomerFollowPart>(pageNo, pageSize);
		IPage<TbCustomerFollowPart> pageList = tbCustomerFollowPartService.page(page,queryWrapper);
		return Result.ok(pageList);
	}

	@AddSysLog(value = "司机打卡-新增")
	@PostMapping("/add")
	public Result<?> add(@RequestBody @Validated(SaveValidated.class) TbCustomerFollowPart item){
		tbCustomerFollowPartService.save(item);
		return Result.ok();
	}

	@AddSysLog(value = "司机打卡-编辑")
	@PutMapping("/edit")
	public Result<?> edit(@RequestBody @Validated(UpdateValidated.class) TbCustomerFollowPart item){
		tbCustomerFollowPartService.updateById(item);
		return Result.ok();
	}

	@AddSysLog(value = "司机打卡-删除")
	@DeleteMapping("/delete")
	public Result<?> delete(TbCustomerFollowPart item){
		tbCustomerFollowPartService.removeById(item.getId());
		return Result.ok();
	}

	/**
	 *  simpleWrite在数据量不大的情况下可以使用（5000以内，具体也要看实际情况），数据量大时要 重复多次写入
	 * @param response
	 */
    @AddSysLog(value = "司机打卡-导出")
	@GetMapping("/simple/export")
	public Result<?> export(TbCustomerFollowPart item,HttpServletResponse response){
		try (OutputStream output = response.getOutputStream()){

			String fileName = "司机打卡.xlsx";
			try {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);

			Result<?> result = list(item);
			List<TbCustomerFollowPart> list = (List<TbCustomerFollowPart>)result.getResult();
			EasyExcel.write(output, TbCustomerFollowPart.class).sheet("sheet1").doWrite(list);

		} catch (IOException e) {
			log.error(e.getMessage(), e);
			return Result.error("导出错误！");
		}
		return Result.ok();
	}

    @AddSysLog(value = "司机打卡-分页导出")
	@GetMapping("/multi/export")
	public Result<?> multiExport(TbCustomerFollowPart item,HttpServletResponse response){
		try (OutputStream output = response.getOutputStream()){
			String fileName = "司机打卡.xlsx";
			try {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);

			int pages = 1;
			try (ExcelWriter excelWriter = EasyExcel.write(output, TbCustomerFollowPart.class).build()) {
				WriteSheet writeSheet = EasyExcel.writerSheet("sheet1").build();
				for (int i = 0; i < pages; i++) {
					Result<?> result = page(item,i+1, 10);
					IPage<TbCustomerFollowPart> page = (IPage<TbCustomerFollowPart>)result.getResult();
					List<TbCustomerFollowPart> data = page.getRecords();
					int temp = (int)page.getPages();
					if(pages<temp) {
						pages = temp;
					}
					excelWriter.write(data, writeSheet);
				}
			}
		} catch (IOException e) {
			log.error(e.getMessage(), e);
			return Result.error("导出错误！");
		}
		return Result.ok();
	}

}
