package org.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.stm.spider.entity.WbTmpCfg;
import org.stm.spider.entity.WbTmpData;
import org.stm.spider.service.IWbTmpCfgService;
import org.stm.spider.service.IWbTmpDataService;

import javax.servlet.http.HttpServletRequest;
import javax.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
 */
@Api(tags="数据源配置")
@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 = "数据源配置-分页列表查询")
	@ApiOperation(value="数据源配置-分页列表查询", notes="数据源配置-分页列表查询")
	@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 = "数据源配置-添加")
	@ApiOperation(value="数据源配置-添加", notes="数据源配置-添加")
	@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 = "数据源配置-编辑")
	@ApiOperation(value="数据源配置-编辑", notes="数据源配置-编辑")
	@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删除")
	@ApiOperation(value="数据源配置-通过id删除", notes="数据源配置-通过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 = "数据源配置-批量删除")
	@ApiOperation(value="数据源配置-批量删除", notes="数据源配置-批量删除")
	@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查询")
	@ApiOperation(value="数据源配置-通过id查询", notes="数据源配置-通过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")
	 @ApiOperation(value="数据抓取配置-通过id", notes="数据抓取配置-通过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 = "抓取最新数据")
	@ApiOperation(value="数据抓取", notes="抓取最新数据")
	@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("完成!");
	}

 }
