package org.jeecg.modules.stm.spider.controller;

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.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.spider.entity.WbTmpCfg;
import org.jeecg.modules.stm.spider.entity.WbTmpData;
import org.jeecg.modules.stm.spider.service.IWbTmpCfgService;
import org.jeecg.modules.stm.spider.service.IWbTmpDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @Description: 数据源配置
 * @Author: jeecg-boot
 * @Date:   2023-05-08
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/spider/wbTmpCfg")
@Slf4j
public class WbTmpCfgController extends JeecgController<WbTmpCfg, IWbTmpCfgService> {
	@Autowired
	private IWbTmpCfgService wbTmpCfgService;
	@Autowired
	private IWbTmpDataService wbTmpDataService;

	/**
	 * 分页列表查询
	 *
	 * @param wbTmpCfg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "数据源配置-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<WbTmpCfg>> queryPageList(WbTmpCfg wbTmpCfg,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<WbTmpCfg> queryWrapper = QueryGenerator.initQueryWrapper(wbTmpCfg, req.getParameterMap());
		Page<WbTmpCfg> page = new Page<WbTmpCfg>(pageNo, pageSize);
		IPage<WbTmpCfg> pageList = wbTmpCfgService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param wbTmpCfg
	 * @return
	 */
	@AutoLog(value = "数据源配置-添加")
	  
	@RequiresPermissions("spider:wb_tmp_cfg:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody WbTmpCfg wbTmpCfg) {
		wbTmpCfgService.save(wbTmpCfg);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param wbTmpCfg
	 * @return
	 */
	@AutoLog(value = "数据源配置-编辑")
	  
	@RequiresPermissions("spider:wb_tmp_cfg:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody WbTmpCfg wbTmpCfg) {
		wbTmpCfgService.updateById(wbTmpCfg);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "数据源配置-通过id删除")
	  
	@RequiresPermissions("spider:wb_tmp_cfg:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		wbTmpCfgService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "数据源配置-批量删除")
	  
	@RequiresPermissions("spider:wb_tmp_cfg:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.wbTmpCfgService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "数据源配置-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<WbTmpCfg> queryById(@RequestParam(name="id",required=true) String id) {
		WbTmpCfg wbTmpCfg = wbTmpCfgService.getById(id);
		if(wbTmpCfg==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(wbTmpCfg);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param wbTmpCfg
    */
    @RequiresPermissions("spider:wb_tmp_cfg:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WbTmpCfg wbTmpCfg) {
        return super.exportXls(request, wbTmpCfg, WbTmpCfg.class, "数据源配置");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("spider:wb_tmp_cfg:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WbTmpCfg.class);
    }

	 /**
	  *   通过id获取配置信息
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "数据抓取配置-通过id")
	   
	 @PostMapping(value = "/spiderData")
	 public Result<String> spiderData(@RequestParam(name="id",required=true) Integer id) {

		 try {
			 WbTmpCfg wbTmpCfg = wbTmpCfgService.getById(id);
			 List<Integer> existDocIdList = new ArrayList<>();
			 int oldPages = wbTmpCfg.getPages();
			 int pageInd = 0;
			 int s = 0;
			 do {
				 pageInd++;
				 int flag = wbTmpCfgService.processWbTmpCfg(wbTmpCfg, existDocIdList, pageInd,null);
				 if(flag==1) {
					 return Result.OK("完成!");
				 }
				 Thread.sleep(4000);
				 s = wbTmpCfg.getPages()- oldPages - pageInd;

			 } while(s>0);

		 } catch (Exception e) {
			 e.printStackTrace();
			 return Result.OK("服务器异常");
		 }
		 return Result.OK("完成!");
	 }


	@AutoLog(value = "抓取最新数据")
	  
	@PostMapping(value = "/spideAllNewData")
	public Result<String> spideAllNewData() {
		List<WbTmpCfg> wbTmpCfgList = wbTmpCfgService.list();
		try {
			List<WbTmpData> list = wbTmpDataService.list();
			List<Integer> listDocID = new ArrayList<>();
			for(int i=0;i<list.size();i++) {
				listDocID.add(list.get(i).getComplianeNo());
			}
			List<Integer> existDocIdList = new ArrayList<>();

			for (WbTmpCfg wbTmpCfg : wbTmpCfgList) {
				int pageInd = 3;
				int pages = wbTmpCfg.getPages();
				int s = 0;
				do {
					pageInd++;
					int flag = wbTmpCfgService.processWbTmpCfg(wbTmpCfg, existDocIdList, pageInd,listDocID);
					if(flag==1) {
						return Result.OK("完成!");
					}
					Thread.sleep(5000);
					s = wbTmpCfg.getPages()-pages-pageInd;

				} while(s>0);

			}
		} catch (Exception e) {
			e.printStackTrace();
			return Result.OK("服务器异常");
		}
		return Result.OK("完成!");
	}

 }
