package org.jeecg.modules.ctop.controller;

import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.vo.SheetInfoVo;
import cn.com.ctop.toutiao.modules.report.entity.BytedanceVideoEtlInfo;
import cn.com.ctop.toutiao.modules.report.service.IBytedanceVideoEtlInfoService;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.excelutil.entity.BytedanceVideoEtlInfoEntity;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 头条视频信息清洗
 * @author jeecg-boot
 * @date   2020-12-25
 * @version V1.0
 */
@Slf4j
@RestController
@RequestMapping("/ctop/bytedanceVideoEtlInfo")
public class BytedanceVideoEtlInfoController {
	@Autowired
	private IBytedanceVideoEtlInfoService bytedanceVideoEtlInfoService;

	/**
	 * 分页列表查询
	 * @param bytedanceVideoEtlInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<IPage<BytedanceVideoEtlInfo>> queryPageList(BytedanceVideoEtlInfo bytedanceVideoEtlInfo,
															  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															  HttpServletRequest req) {
		Result<IPage<BytedanceVideoEtlInfo>> result = new Result<>();
		String type = bytedanceVideoEtlInfo.getType();
		bytedanceVideoEtlInfo.setType(null);
		String startDate = bytedanceVideoEtlInfo.getStartDate();
		bytedanceVideoEtlInfo.setStartDate(null);
		String endDate = bytedanceVideoEtlInfo.getEndDate();
		bytedanceVideoEtlInfo.setEndDate(null);
		QueryWrapper<BytedanceVideoEtlInfo> queryWrapper = QueryGenerator.initQueryWrapper(bytedanceVideoEtlInfo, req.getParameterMap());
		if(null!=type&&!"".equals(type)){
			if("valid".equals(type.trim())){
				//有效
				if(null!=startDate&&!"".equals(startDate.trim())){
					queryWrapper.ge("effect_date",startDate);
				}
				if(null!=endDate&&!"".equals(endDate.trim())){
					queryWrapper.le("effect_date",endDate);
				}
				queryWrapper.le("effect_day_num",7);
			}else if("hot".equals(type.trim())){
				//爆款
				if(null!=startDate&&!"".equals(startDate.trim())){
					queryWrapper.ge("faddish_date",startDate);
				}
				if(null!=endDate&&!"".equals(endDate.trim())){
					queryWrapper.le("faddish_date",endDate);
				}
				queryWrapper.le("faddish_day_num",14);
			}else {
				//上线
				if(null!=startDate&&!"".equals(startDate.trim())){
					queryWrapper.ge("state_date",startDate);
				}
				if(null!=endDate&&!"".equals(endDate.trim())){
					queryWrapper.le("state_date",endDate);
				}
			}
		}
		queryWrapper.orderByDesc("total_cost");
		Page<BytedanceVideoEtlInfo> page = new Page<BytedanceVideoEtlInfo>(pageNo, pageSize);
		IPage<BytedanceVideoEtlInfo> pageList = bytedanceVideoEtlInfoService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 *   添加
	 * @param bytedanceVideoEtlInfo
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<BytedanceVideoEtlInfo> add(@RequestBody BytedanceVideoEtlInfo bytedanceVideoEtlInfo) {
		Result<BytedanceVideoEtlInfo> result = new Result<>();
		try {
			bytedanceVideoEtlInfoService.save(bytedanceVideoEtlInfo);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 *  编辑
	 * @param bytedanceVideoEtlInfo
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<BytedanceVideoEtlInfo> edit(@RequestBody BytedanceVideoEtlInfo bytedanceVideoEtlInfo) {
		Result<BytedanceVideoEtlInfo> result = new Result<BytedanceVideoEtlInfo>();
		BytedanceVideoEtlInfo bytedanceVideoEtlInfoEntity = bytedanceVideoEtlInfoService.getById(bytedanceVideoEtlInfo.getId());
		if(bytedanceVideoEtlInfoEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = bytedanceVideoEtlInfoService.updateById(bytedanceVideoEtlInfo);
			if(ok) {
				result.success("修改成功!");
			}
		}

		return result;
	}


	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<BytedanceVideoEtlInfo> queryById(@RequestParam(name="id",required=true) String id) {
		Result<BytedanceVideoEtlInfo> result = new Result<>();
		BytedanceVideoEtlInfo bytedanceVideoEtlInfo = bytedanceVideoEtlInfoService.getById(id);
		if(bytedanceVideoEtlInfo==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(bytedanceVideoEtlInfo);
			result.setSuccess(true);
		}
		return result;
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param response
	 */
	@PostMapping(value = "/exportXls")
	public void exportXls(BytedanceVideoEtlInfo bytedanceVideoEtlInfo,
						  HttpServletRequest request, HttpServletResponse response)throws Exception {
		// Step.1 组装查询条件
		String type = bytedanceVideoEtlInfo.getType();
		bytedanceVideoEtlInfo.setType(null);
		String startDate = bytedanceVideoEtlInfo.getStartDate();
		bytedanceVideoEtlInfo.setStartDate(null);
		String endDate = bytedanceVideoEtlInfo.getEndDate();
		bytedanceVideoEtlInfo.setEndDate(null);
		QueryWrapper<BytedanceVideoEtlInfo> queryWrapper = QueryGenerator.initQueryWrapper(bytedanceVideoEtlInfo, request.getParameterMap());
		if(null!=type&&!"".equals(type)){
			if("valid".equals(type.trim())){
				//有效
				if(null!=startDate&&!"".equals(startDate.trim())){
					queryWrapper.ge("effect_date",startDate);
				}
				if(null!=endDate&&!"".equals(endDate.trim())){
					queryWrapper.le("effect_date",endDate);
				}
				queryWrapper.le("effect_day_num",7);
			}else if("hot".equals(type.trim())){
				//爆款
				if(null!=startDate&&!"".equals(startDate.trim())){
					queryWrapper.ge("faddish_date",startDate);
				}
				if(null!=endDate&&!"".equals(endDate.trim())){
					queryWrapper.le("faddish_date",endDate);
				}
				queryWrapper.le("faddish_day_num",14);
			}else {
				//上线
				if(null!=startDate&&!"".equals(startDate.trim())){
					queryWrapper.ge("state_date",startDate);
				}
				if(null!=endDate&&!"".equals(endDate.trim())){
					queryWrapper.le("state_date",endDate);
				}
			}
		}
		queryWrapper.orderByDesc("total_cost");
		List<BytedanceVideoEtlInfo> pageList = bytedanceVideoEtlInfoService.list(queryWrapper);

		//查询总的消耗信息
		SheetInfoVo totalSheetVo = new SheetInfoVo<BytedanceVideoEtlInfoEntity>();
		List<BytedanceVideoEtlInfoEntity> details = new ArrayList<>();
		if(null!=pageList&&!pageList.isEmpty()){
			pageList.forEach(etlInfo->{
				details.add(new BytedanceVideoEtlInfoEntity(etlInfo));
			});
			totalSheetVo.setDetails(details);
		}
		totalSheetVo.setSheetName("头条视频数据统计列表");
		OutputStream outputStream = response.getOutputStream();
		String date = DateUtils.formatDate(new Date());
		HttpUtils.setResponseHeader(response, "头条视频数据统计列表" + date + ".xlsx");
		ExcelWriter excelWriter = EasyExcelFactory.getWriter(outputStream);

		Sheet sheet = new Sheet(1, 0, BytedanceVideoEtlInfoEntity.class);
		sheet.setSheetName(totalSheetVo.getSheetName());
		excelWriter.write(totalSheetVo.getDetails(), sheet);
		excelWriter.finish();
		outputStream.flush();
		outputStream.close();
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> 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(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<BytedanceVideoEtlInfo> listBytedanceVideoEtlInfos = ExcelImportUtil.importExcel(file.getInputStream(), BytedanceVideoEtlInfo.class, params);
				bytedanceVideoEtlInfoService.saveBatch(listBytedanceVideoEtlInfos);
				return Result.ok("文件导入成功！数据行数:" + listBytedanceVideoEtlInfos.size());
			} catch (Exception e) {
				log.error(e.getMessage(),e);
				return Result.error("文件导入失败:"+e.getMessage());
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return Result.ok("文件导入失败！");
	}
}
