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.kuaishou.modules.report.entity.KuaishouVideoEtlInfo;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouVideoEtlInfoService;
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 io.swagger.annotations.Api;
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.jeecg.modules.excelutil.entity.KuaishouVideoEtlInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

 /**
 * 快手视频信息清洗
 * @author jeecg-boot
 * @date   2020-12-25
 * @version V1.0
 */
@Slf4j
@Api(tags="快手视频信息清洗")
@RestController
@RequestMapping("/ctop/kuaishouVideoEtlInfo")
public class KuaishouVideoEtlInfoController {
	@Autowired
	private IKuaishouVideoEtlInfoService kuaishouVideoEtlInfoService;

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

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

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

		return result;
	}

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

  /**
      * 导出excel
   *
   * @param request
   * @param response
   */
  @PostMapping(value = "/exportXls")
  public void exportXls(KuaishouVideoEtlInfo kuaishouVideoEtlInfo,
								HttpServletRequest request, HttpServletResponse response) throws Exception{
	  // Step.1 组装查询条件
	  // Step.1 组装查询条件
	  String type = kuaishouVideoEtlInfo.getType();
	  kuaishouVideoEtlInfo.setType(null);
	  String startDate = kuaishouVideoEtlInfo.getStartDate();
	  kuaishouVideoEtlInfo.setStartDate(null);
	  String endDate = kuaishouVideoEtlInfo.getEndDate();
	  kuaishouVideoEtlInfo.setEndDate(null);
	  QueryWrapper<KuaishouVideoEtlInfo> queryWrapper = QueryGenerator.initQueryWrapper(kuaishouVideoEtlInfo, request.getParameterMap());
	  if(null!=type&&!type.equals("")){
		  if(type.trim().equals("valid")){
			  //有效
			  if(null!=startDate&&!"".equals(startDate.trim())){
				  queryWrapper.ge("effect_date",startDate);
			  }
			  if(null!=endDate&&!"".equals(endDate.trim())){
				  queryWrapper.le("effect_date",endDate);
			  }
			  queryWrapper.lt("effect_day_num",7);
		  }else if(type.trim().equals("hot")){
			  //爆款
			  if(null!=startDate&&!"".equals(startDate.trim())){
				  queryWrapper.ge("faddish_date",startDate);
			  }
			  if(null!=endDate&&!"".equals(endDate.trim())){
				  queryWrapper.le("faddish_date",endDate);
			  }
			  queryWrapper.lt("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<KuaishouVideoEtlInfo> pageList = kuaishouVideoEtlInfoService.list(queryWrapper);

	  //查询总的消耗信息
	  SheetInfoVo totalSheetVo = new SheetInfoVo<KuaishouVideoEtlInfoEntity>();
	  List<KuaishouVideoEtlInfoEntity> details = new ArrayList<>();
	  if(null!=pageList&&!pageList.isEmpty()){
		  pageList.forEach(etlInfo->{
			  details.add(new KuaishouVideoEtlInfoEntity(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, KuaishouVideoEtlInfoEntity.class);
	  sheet.setSheetName(totalSheetVo.getSheetName());
	  excelWriter.write(totalSheetVo.getDetails(), sheet);
	  excelWriter.finish();
	  outputStream.flush();
	  outputStream.close();
  }
}
