package org.jeecg.modules.demo.linkouterchaindetection.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.externalinternalsite.entity.ExternalInternalSite;
import org.jeecg.modules.demo.externalinternalsite.service.IExternalInternalSiteService;
import org.jeecg.modules.demo.linkchainerrorlog.service.ILinkChainErrorLogService;
import org.jeecg.modules.demo.linkouterchaindetection.entity.LinkOuterChainDetection;
import org.jeecg.modules.demo.linkouterchaindetection.service.ILinkOuterChainDetectionService;

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.modules.demo.linkouterchainlog.service.ILinkOuterChainLogService;
import org.jeecg.modules.demo.linkouterouterstatistics.entity.LinkOuterouterStatistics;
import org.jeecg.modules.demo.linkouterouterstatistics.service.ILinkOuterouterStatisticsService;
import org.jeecg.modules.demo.linkwhitelist.entity.LinkWhiteList;
import org.jeecg.modules.demo.linkwhitelist.service.ILinkWhiteListService;
import org.jeecg.modules.demo.mainsite.entity.MainSite;
import org.jeecg.modules.demo.mainsite.service.IMainSiteService;
import org.jeecg.modules.spyderUtils.spider.*;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 外链检测
 * @Author: jeecg-boot
 * @Date:   2023-04-12
 * @Version: V1.0
 */
@Api(tags="外链检测")
@RestController
@RequestMapping("/linkouterchaindetection/linkOuterChainDetection")
@Slf4j
public class LinkOuterChainDetectionController extends JeecgController<LinkOuterChainDetection, ILinkOuterChainDetectionService> {
	@Autowired
	private ILinkOuterChainDetectionService linkOuterChainDetectionService;
	 @Autowired
	 private IMainSiteService iMainSiteService;

	 @Autowired
	 private IExternalInternalSiteService externalInternalSiteService;

	 @Autowired
	 private ILinkChainErrorLogService iLinkChainErrorLogService;

	 @Autowired
	 private ILinkOuterChainLogService linkOuterChainLogService;

	 @Autowired
	 private ILinkOuterouterStatisticsService iLinkOuterouterStatisticsService;

	 @Autowired
	 private ILinkWhiteListService linkWhiteListService;

	 @Autowired
	 private TrendsSpyder trendsSpyder;

	 @Autowired
	 private FangCunUtils fangCunUtils;

	 @Autowired
	 private LinkOrWordSaveUtils linkOrWordSaveUtils;

	 @Autowired
	 private JsoupUtils jsoupUtils;

	 @Autowired
	 private CrawlingUtils crawlingUtils;
	/**
	 * 分页列表查询
	 *
	 * @param linkOuterChainDetection
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "外链检测-分页列表查询")
	@ApiOperation(value="外链检测-分页列表查询", notes="外链检测-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LinkOuterChainDetection>> queryPageList(LinkOuterChainDetection linkOuterChainDetection,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LinkOuterChainDetection> queryWrapper = QueryGenerator.initQueryWrapper(linkOuterChainDetection, req.getParameterMap());
		Page<LinkOuterChainDetection> page = new Page<LinkOuterChainDetection>(pageNo, pageSize);
		IPage<LinkOuterChainDetection> pageList = linkOuterChainDetectionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 //@AutoLog(value = "错链检测-通过id查询")
	 @ApiOperation(value="错链检测-通过id查询", notes="错链检测-通过id查询")
	 @GetMapping(value = "/queryByIdIgnoreTrue")
	 public Result<LinkOuterChainDetection> queryByIdIgnoreTrue(@RequestParam(name="id",required=true) String id) {
		 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(id);
		 if(linkOuterChainDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkOuterChainDetection.setIsIgnore("1");
		 linkOuterChainDetectionService.updateById(linkOuterChainDetection);
		 return Result.OK(linkOuterChainDetection);
	 }

	 //@AutoLog(value = "错链检测-通过id查询")
	 @ApiOperation(value="错链检测-通过id查询", notes="错链检测-通过id查询")
	 @GetMapping(value = "/queryByIdIgnoreFalse")
	 public Result<LinkOuterChainDetection> queryByIdIgnoreFalse(@RequestParam(name="id",required=true) String id) {
		 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(id);
		 if(linkOuterChainDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkOuterChainDetection.setIsIgnore("2");
		 linkOuterChainDetectionService.updateById(linkOuterChainDetection);
		 return Result.OK(linkOuterChainDetection);
	 }
	 //@AutoLog(value = "错链检测-通过id查询")
	 @ApiOperation(value="错链检测-通过id查询", notes="错链检测-通过id查询")
	 @GetMapping(value = "/queryByIdModifyTrue")
	 public Result<LinkOuterChainDetection> queryByIdModifyTrue(@RequestParam(name="id",required=true) String id) {
		 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(id);
		 if(linkOuterChainDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkOuterChainDetection.setIsRevise("1");
		 linkOuterChainDetectionService.updateById(linkOuterChainDetection);
		 return Result.OK(linkOuterChainDetection);
	 }

	 //@AutoLog(value = "错链检测-通过id查询")
	 @ApiOperation(value="错链检测-通过id查询", notes="错链检测-通过id查询")
	 @GetMapping(value = "/queryByIdModifyFalse")
	 public Result<LinkOuterChainDetection> queryByIdModifyFalse(@RequestParam(name="id",required=true) String id) {
		 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(id);
		 if(linkOuterChainDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkOuterChainDetection.setIsRevise("2");
		 linkOuterChainDetectionService.updateById(linkOuterChainDetection);
		 return Result.OK(linkOuterChainDetection);
	 }
	 /**
	  * 同步外链数据
	  * @param linkOuterChainDetection
	  * @return
	  */
	 //@AutoLog(value = "同步外链数据-分页列表查询")
	 @ApiOperation(value="同步外链数据-分页列表查询", notes="同步外链数据-分页列表查询")
	 @GetMapping(value = "/updateListOuter")
	 public Result<String> updateListOuter(
			 LinkOuterChainDetection linkOuterChainDetection
	 ) {
		 //如果没有选择任何内容，对所有内容进行校验
		 List<MainSite> list = iMainSiteService.list();
		 for (MainSite mainSite : list) {
			 crawlingLinkOuter(mainSite);
		 }
		 return Result.OK("pageList");
	 }

	 private void crawlingLinkOuter(MainSite mainSite) {
		 //只统计外链
		 Integer count =  externalInternalSiteService.countSourceLinkOuter(mainSite.getMainSiteUrl());
		 Integer pageSize = 10;
		 Integer pageNum = (int)Math.ceil((double)count/pageSize);
		 Integer tenantId = null;
		 for(int i = 1; i <= pageNum;i+=1){
			 Page<ExternalInternalSite> page = new Page<ExternalInternalSite>(i, pageSize);
			 //只统计外链
			 IPage<ExternalInternalSite> pageList = externalInternalSiteService.pageSourceMainUrlLinkOuter(page,mainSite.getMainSiteUrl());
			 for (ExternalInternalSite externalInternalSite : pageList.getRecords()) {
				 tenantId = externalInternalSite.getTenantId();
				 LinkOuterChainDetection outerChainDetection = new LinkOuterChainDetection();
				 outerChainDetection.setId(externalInternalSite.getId());
				 outerChainDetection.setCreateBy(externalInternalSite.getMainUrl());
				 outerChainDetection.setCreateTime(new Date());
				 outerChainDetection.setUpdateBy(externalInternalSite.getMainUrl());
				 outerChainDetection.setUpdateTime(new Date());
				 QueryWrapper<MainSite> mainSiteQueryWrapper = new QueryWrapper<MainSite>();
				 String mainUrl1 = externalInternalSite.getMainUrl();
				 mainSiteQueryWrapper.eq("main_site_url",mainUrl1);
				 List<MainSite> mainSiteList = iMainSiteService.list(mainSiteQueryWrapper);
				 if(mainSiteList.size() >= 1){
					 MainSite mainSite1 = mainSiteList.get(0);
					 outerChainDetection.setMainSiteId(mainSite1.getId());
					 outerChainDetection.setMainSiteName(mainSite1.getMainSiteName());
					 outerChainDetection.setMainSiteUrl(mainSite1.getMainSiteUrl());
				 }
				 outerChainDetection.setOuterUrl(externalInternalSite.getUrl());
				 outerChainDetection.setOuterUrlType(externalInternalSite.getTypeUrl());
				 outerChainDetection.setOuterUrlState(externalInternalSite.getStateUrl());
				 //设置引用页数
				 QueryWrapper<ExternalInternalSite> siteQueryWrapper = new QueryWrapper<ExternalInternalSite>();
				 siteQueryWrapper.eq("url",externalInternalSite.getUrl());
				 long countSiteQuery = externalInternalSiteService.count(siteQueryWrapper);
				 outerChainDetection.setNumberApplicationPages(Integer.parseInt(String.valueOf(countSiteQuery)));
				 QueryWrapper<LinkOuterChainDetection> linkOuterChainDetectionQueryWrapper = new QueryWrapper<LinkOuterChainDetection>();
				 linkOuterChainDetectionQueryWrapper.eq("id",externalInternalSite.getId());
				 Page<LinkOuterChainDetection> pageListS = new Page<LinkOuterChainDetection>(1, 1);
				 Page<LinkOuterChainDetection> page1 = linkOuterChainDetectionService.page(pageListS,linkOuterChainDetectionQueryWrapper);
				 List<LinkOuterChainDetection> outerChainDetectionList = page1.getRecords();
				 LinkOuterChainDetection chainDetection = null;
				 if(outerChainDetectionList.size() >=1){
					 chainDetection = outerChainDetectionList.get(0);
				 }
				 if(!ObjectUtils.isEmpty(chainDetection)){
					 outerChainDetection.setUpdateTime(chainDetection.getUpdateTime());
					 Integer numberScans = chainDetection.getNumberScans();
					 numberScans = numberScans +1;
					 outerChainDetection.setNumberScans(numberScans);
					 outerChainDetection.setIsIgnore(chainDetection.getIsIgnore());
					 outerChainDetection.setCheckState(chainDetection.getCheckState());
					 outerChainDetection.setIsRevise(chainDetection.getIsRevise());
				 }else{
					 outerChainDetection.setNumberScans(1);
					 outerChainDetection.setIsIgnore("2");
					 outerChainDetection.setCheckState("2");
					 outerChainDetection.setIsRevise("2");
				 }
				 outerChainDetection.setTenantId(externalInternalSite.getTenantId());
				 linkOuterChainDetectionService.saveOrUpdate(outerChainDetection);
				 //记录日志信息
				 linkOrWordSaveUtils.savelinkOuterChainLog(outerChainDetection,externalInternalSite.getTenantId());
			 }
		 }
		 try {
			 LinkOuterouterStatistics linkOuterouterStatistics = iLinkOuterouterStatisticsService.listCountByMainUrl(mainSite.getMainSiteUrl());
			 if(!ObjectUtils.isEmpty(linkOuterouterStatistics)){
				 linkOuterouterStatistics.setId(IdWorker.getIdStr());
				 linkOuterouterStatistics.setScanTime(linkOuterouterStatistics.getUpdateTime());
				 linkOuterouterStatistics.setTestType("1");
				 linkOuterouterStatistics.setTenantId(tenantId);
				 iLinkOuterouterStatisticsService.save(linkOuterouterStatistics);
			 }
		 }catch (Exception e){
			 e.printStackTrace();
		 }
	 }
	 /*
	  * 复核外链数据
	  * */
	 @RequestMapping(value = "/onHandleCheckListLinkOuter")
	 public Result<String> onHandleCheckListLinkOuter(HttpServletRequest request,LinkOuterChainDetection linkOuterChainDetection) {
		 String selections = request.getParameter("selections");
		 QueryWrapper<LinkOuterChainDetection> queryWrapper = QueryGenerator.initQueryWrapper(linkOuterChainDetection, request.getParameterMap());
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 queryWrapper.in("id",selectionList);
			 List<LinkOuterChainDetection> exportList = linkOuterChainDetectionService.list(queryWrapper);
			 checkLinkOuter(exportList);
		 }else{
			 //如果没有选择任何内容，对所有内容进行校验
			 Long count =  linkOuterChainDetectionService.count();
			 Integer pageSize = 10;
			 Integer pageNum = (int)Math.ceil((double)count/pageSize);
			 for(int i = 1; i <= pageNum;i+=1){
				 Page<LinkOuterChainDetection> page = new Page<LinkOuterChainDetection>(i, pageSize);
				 IPage<LinkOuterChainDetection> pageList = linkOuterChainDetectionService.page(page, queryWrapper);
				 checkLinkOuter(pageList.getRecords());
			 }
		 }
		 //统计外链数据
		 List<MainSite> list = iMainSiteService.list();
		 for (MainSite mainSite : list) {
			 LinkOuterouterStatistics linkOuterouterStatistics = iLinkOuterouterStatisticsService.listCountByMainUrl(mainSite.getMainSiteUrl());
			 if(!ObjectUtils.isEmpty(linkOuterouterStatistics)){
				 linkOuterouterStatistics.setId(IdWorker.getIdStr());
				 linkOuterouterStatistics.setScanTime(linkOuterouterStatistics.getUpdateTime());
				 linkOuterouterStatistics.setTestType("2");
				 linkOuterouterStatistics.setTenantId(mainSite.getTenantId());
				 iLinkOuterouterStatisticsService.save(linkOuterouterStatistics);
			 }
		 }
		 return Result.OK("成功");
	 }
	 private void checkLinkOuter(List<LinkOuterChainDetection> exportList){
		 for (LinkOuterChainDetection linkOuterChainDetection : exportList) {
			 System.out.println(linkOuterChainDetection);
			 String outerUrl = linkOuterChainDetection.getOuterUrl();
			 String mainSiteId = linkOuterChainDetection.getMainSiteId();
			 MainSite mainSite = iMainSiteService.getById(mainSiteId);
			 QueryWrapper<ExternalInternalSite> internalSiteQueryWrapper = new QueryWrapper<ExternalInternalSite>();
			 internalSiteQueryWrapper.eq("url",outerUrl);
			 long count = externalInternalSiteService.count(internalSiteQueryWrapper);
			 Integer pageSize = 10;
			 Integer pageNum = (int)Math.ceil((double)count/pageSize);
			 int countError = 0;
			 for(int i = 1; i <= pageNum;i+=1){
				 Page<ExternalInternalSite> page = new Page<ExternalInternalSite>(i, pageSize);
				 IPage<ExternalInternalSite> pageList = externalInternalSiteService.page(page,internalSiteQueryWrapper);
				 List<ExternalInternalSite> records = pageList.getRecords();
				 for (ExternalInternalSite record : records) {
					 //爬取页面
					 Document document = null;
					 String grabPage = mainSite.getGrabPage();
					 try {
						 SpyderReturn spyderReturn = trendsSpyder.companyTrendsSpyderMessage(record.getSource(), grabPage);
						 document = spyderReturn.getDocument();
						 String baseUri = spyderReturn.getBaseUri();
						 document = jsoupUtils.setUrl(document, baseUri);
					 }catch (Exception e){
						 document = null;
					 }
					 if(!ObjectUtils.isEmpty(document)){
						 String sHtml = document.toString();
						 sHtml = sHtml.replaceAll("amp;","");
						 int i1 = sHtml.indexOf(outerUrl);
						 if(i1 > 0){
							 countError++;
						 }
					 }
				 }
			 }
			 if(countError > 0){
				 linkOuterChainDetection.setIsRevise("2");
				 linkOuterChainDetection.setCheckNumberApplicationPagesv(countError);
			 }
			 if(countError == 0){
				 linkOuterChainDetection.setIsRevise("1");
			 }
			 linkOuterChainDetection.setCheckTime(new Date());
			 linkOuterChainDetection.setCheckState("1");

			 linkOuterChainDetectionService.updateById(linkOuterChainDetection);
			 //记录日志信息
			 linkOrWordSaveUtils.savelinkOuterChainLog(linkOuterChainDetection,linkOuterChainDetection.getTenantId());

		 }
	 }

	 /**
	  * 批量忽略
	  * @param ids
	  * @return
	  */
	 @DeleteMapping(value = "/ignoreAll")
	 public Result<String> ignoreAllBatch(@RequestParam(name="ids",required=true) String ids) {
		 Page<MainSite> pageMainSite = new Page<MainSite>(1, 1);
		 IPage<MainSite> pageListMainSite = iMainSiteService.page(pageMainSite);
		 MainSite mainSite = null;
		 if(pageListMainSite.getRecords().size() >= 1){
			 mainSite = pageListMainSite.getRecords().get(0);
		 }
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(id);
				 outerChainDetectionServiceById.setIsIgnore("1");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
				 String outerUrl = outerChainDetectionServiceById.getOuterUrl();
				 try {
					 QueryWrapper<LinkWhiteList> linkWhiteListQueryWrapper = new QueryWrapper<LinkWhiteList>();
					 linkWhiteListQueryWrapper.eq("white_link",outerUrl);
					 linkWhiteListService.remove(linkWhiteListQueryWrapper);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
				 try {
					 String outerUrl1 = outerChainDetectionServiceById.getOuterUrl();
					 if(!ObjectUtils.isEmpty(outerUrl1)){
						 if(!ObjectUtils.isEmpty(mainSite)){
							 LinkWhiteList whiteList = new LinkWhiteList();
							 whiteList.setId(DigestUtils.md5DigestAsHex(outerUrl1.getBytes()));
							 whiteList.setCreateTime(new Date());
							 whiteList.setUpdateTime(new Date());
							 whiteList.setMainSiteId(mainSite.getId());
							 whiteList.setMainSiteName(mainSite.getMainSiteName());
							 whiteList.setMainSiteUrl(mainSite.getMainSiteUrl());
							 whiteList.setWhiteLink(outerUrl1);
							 boolean save = linkWhiteListService.saveOrUpdate(whiteList);
						 }
					 }
				 }catch (Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.OK("编辑成功!");
	 }
	 /**
	  * 批量取消忽略
	  * @param ids
	  * @return
	  */
	 @DeleteMapping(value = "/ignoreAllCancell")
	 public Result<String> ignoreAllCancell(@RequestParam(name="ids",required=true) String ids) {
		 Page<MainSite> pageMainSite = new Page<MainSite>(1, 1);
		 IPage<MainSite> pageListMainSite = iMainSiteService.page(pageMainSite);
		 MainSite mainSite = null;
		 if(pageListMainSite.getRecords().size() >= 1){
			 mainSite = pageListMainSite.getRecords().get(0);
		 }
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(id);
				 outerChainDetectionServiceById.setIsIgnore("2");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
				 String outerUrl = outerChainDetectionServiceById.getOuterUrl();
				 try {
					 QueryWrapper<LinkWhiteList> linkWhiteListQueryWrapper = new QueryWrapper<LinkWhiteList>();
					 linkWhiteListQueryWrapper.eq("white_link",outerUrl);
					 linkWhiteListService.remove(linkWhiteListQueryWrapper);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.OK("编辑成功!");
	 }
	 /**
	  * 批量已经修改
	  * @param ids
	  * @return
	  */
	 @DeleteMapping(value = "/ModifyTrueBatchDelete")
	 public Result<String> ModifyTrueBatchDelete(@RequestParam(name="ids",required=true) String ids) {
		 Page<MainSite> pageMainSite = new Page<MainSite>(1, 1);
		 IPage<MainSite> pageListMainSite = iMainSiteService.page(pageMainSite);
		 MainSite mainSite = null;
		 if(pageListMainSite.getRecords().size() >= 1){
			 mainSite = pageListMainSite.getRecords().get(0);
		 }
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(id);
				 outerChainDetectionServiceById.setIsRevise("1");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
			 }
		 }
		 return Result.OK("编辑成功!");
	 }
	 /**
	  * 批量取消已经修改
	  * @param ids
	  * @return
	  */
	 @DeleteMapping(value = "/ModifyFalseBatchDelete")
	 public Result<String> ModifyFalseBatchDelete(@RequestParam(name="ids",required=true) String ids) {
		 Page<MainSite> pageMainSite = new Page<MainSite>(1, 1);
		 IPage<MainSite> pageListMainSite = iMainSiteService.page(pageMainSite);
		 MainSite mainSite = null;
		 if(pageListMainSite.getRecords().size() >= 1){
			 mainSite = pageListMainSite.getRecords().get(0);
		 }
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(id);
				 outerChainDetectionServiceById.setIsRevise("2");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
			 }
		 }
		 return Result.OK("编辑成功!");
	 }



	 @ApiOperation(value="错链检测-通过id查询", notes="错链检测-通过id查询")
	 @GetMapping(value = "/addWhiteLink")
	 public Result<LinkOuterChainDetection> addWhiteLink(@RequestParam(name="id",required=true) String id) {
		 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(id);
		 String outerUrl = outerChainDetectionServiceById.getOuterUrl();
		 if(!ObjectUtils.isEmpty(outerUrl)){
			 LinkWhiteList whiteList = new LinkWhiteList();
			 whiteList.setId(DigestUtils.md5DigestAsHex(outerUrl.getBytes()));
			 whiteList.setCreateTime(new Date());
			 whiteList.setUpdateTime(new Date());
			 whiteList.setMainSiteId(outerChainDetectionServiceById.getId());
			 whiteList.setMainSiteName(outerChainDetectionServiceById.getMainSiteName());
			 whiteList.setMainSiteUrl(outerChainDetectionServiceById.getMainSiteUrl());
			 whiteList.setWhiteLink(outerUrl);
			 boolean save = linkWhiteListService.saveOrUpdate(whiteList);
		 }
		 if(outerChainDetectionServiceById==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(outerChainDetectionServiceById);
	 }

	 @ApiOperation(value="错链检测-通过id查询", notes="错链检测-通过id查询")
	 @GetMapping(value = "/removeWhiteLink")
	 public Result<LinkOuterChainDetection> removeWhiteLink(@RequestParam(name="id",required=true) String id) {
		 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(id);
		 String outerUrl = outerChainDetectionServiceById.getOuterUrl();
		 if(!ObjectUtils.isEmpty(outerUrl)){
			 QueryWrapper<LinkWhiteList> linkWhiteListQueryWrapper = new QueryWrapper<LinkWhiteList>();
			 linkWhiteListQueryWrapper.eq("white_link",outerUrl);
			 linkWhiteListService.remove(linkWhiteListQueryWrapper);
		 }
		 if(outerChainDetectionServiceById==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(outerChainDetectionServiceById);
	 }
	/**
	 * 添加
	 * @param linkOuterChainDetection
	 * @return
	 */
	@AutoLog(value = "外链检测-添加")
	@ApiOperation(value="外链检测-添加", notes="外链检测-添加")
	@RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LinkOuterChainDetection linkOuterChainDetection) {
		try {
			String mainSiteId = linkOuterChainDetection.getMainSiteId();
			MainSite mainSite = iMainSiteService.getById(mainSiteId);
			linkOuterChainDetection.setMainSiteName(mainSite.getMainSiteName());
			linkOuterChainDetection.setMainSiteUrl(mainSite.getMainSiteUrl());
		}catch (Exception e){
			e.printStackTrace();
		}
		linkOuterChainDetectionService.save(linkOuterChainDetection);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param linkOuterChainDetection
	 * @return
	 */
	@AutoLog(value = "外链检测-编辑")
	@ApiOperation(value="外链检测-编辑", notes="外链检测-编辑")
	@RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LinkOuterChainDetection linkOuterChainDetection) {
		try {
			String mainSiteId = linkOuterChainDetection.getMainSiteId();
			MainSite mainSite = iMainSiteService.getById(mainSiteId);
			linkOuterChainDetection.setMainSiteName(mainSite.getMainSiteName());
			linkOuterChainDetection.setMainSiteUrl(mainSite.getMainSiteUrl());
		}catch (Exception e){
			e.printStackTrace();
		}
		linkOuterChainDetectionService.updateById(linkOuterChainDetection);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "外链检测-通过id删除")
	@ApiOperation(value="外链检测-通过id删除", notes="外链检测-通过id删除")
	@RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		linkOuterChainDetectionService.removeById(id);
		return Result.OK("删除成功!");
	}
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "外链检测-批量删除")
	@ApiOperation(value="外链检测-批量删除", notes="外链检测-批量删除")
	@RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.linkOuterChainDetectionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	 @DeleteMapping(value = "/batchHIgnoreHandle")
	 public Result<String> IgnoreBatchTrue(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(sId);
				 outerChainDetectionServiceById.setIsIgnore("1");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }

	 @DeleteMapping(value = "/batchHIgnoreHandleDelete")
	 public Result<String> IgnoreBatchFalse(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(sId);
				 outerChainDetectionServiceById.setIsIgnore("2");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }

	 @DeleteMapping(value = "/batchHModifyTrue")
	 public Result<String> batchHModifyTrue(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkOuterChainDetection outerChainDetectionServiceById = linkOuterChainDetectionService.getById(sId);
				 outerChainDetectionServiceById.setIsRevise("1");
				 linkOuterChainDetectionService.updateById(outerChainDetectionServiceById);
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }

	 @DeleteMapping(value = "/batchHModifyFalse")
	 public Result<String> batchHModifyFalse(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(sId);
				 linkOuterChainDetection.setIsRevise("2");
				 linkOuterChainDetectionService.updateById(linkOuterChainDetection);
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }

	 @DeleteMapping(value = "/batchHAddWhiteLink")
	 public Result<String> batchHAddWhiteLink(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(sId);
				 String outerUrl = linkOuterChainDetection.getOuterUrl();
				 QueryWrapper<LinkWhiteList> linkWhiteListQueryWrapper = new QueryWrapper<LinkWhiteList>();
				 linkWhiteListQueryWrapper.eq("white_link",outerUrl);
				 linkWhiteListService.remove(linkWhiteListQueryWrapper);
				 if(!ObjectUtils.isEmpty(outerUrl)){
					 LinkWhiteList whiteList = new LinkWhiteList();
					 whiteList.setId(DigestUtils.md5DigestAsHex(outerUrl.getBytes()));
					 whiteList.setCreateTime(new Date());
					 whiteList.setUpdateTime(new Date());
					 whiteList.setMainSiteId(linkOuterChainDetection.getId());
					 whiteList.setMainSiteName(linkOuterChainDetection.getMainSiteName());
					 whiteList.setMainSiteUrl(linkOuterChainDetection.getMainSiteUrl());
					 whiteList.setWhiteLink(outerUrl);
					 boolean save = linkWhiteListService.saveOrUpdate(whiteList);
				 }
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }

	 @DeleteMapping(value = "/batchHRemoveWhiteLink")
	 public Result<String> batchHRemoveWhiteLink(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(sId);
				 String outerUrl = linkOuterChainDetection.getOuterUrl();
				 QueryWrapper<LinkWhiteList> linkWhiteListQueryWrapper = new QueryWrapper<LinkWhiteList>();
				 linkWhiteListQueryWrapper.eq("white_link",outerUrl);
				 linkWhiteListService.remove(linkWhiteListQueryWrapper);
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 */
	@ApiOperation(value="外链检测-通过id查询", notes="外链检测-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LinkOuterChainDetection> queryById(@RequestParam(name="id",required=true) String id) {
		LinkOuterChainDetection linkOuterChainDetection = linkOuterChainDetectionService.getById(id);
		if(linkOuterChainDetection==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(linkOuterChainDetection);
	}
    /**
    * 导出excel
    *
    * @param request
    * @param linkOuterChainDetection
    */

	@RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:exportXls")
	@RequestMapping(value = "/exportXlslinkOuter")
	public ModelAndView exportXlslinkOuter(HttpServletRequest request, LinkOuterChainDetection linkOuterChainDetection) {
		return super.exportXls(request, linkOuterChainDetection, LinkOuterChainDetection.class, "外链检测");
	}

    @RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LinkOuterChainDetection linkOuterChainDetection) {
        return super.exportXls(request, linkOuterChainDetection, LinkOuterChainDetection.class, "外链检测");
    }
    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("linkouterchaindetection:link_outer_chain_detection:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LinkOuterChainDetection.class);
    }

}
