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

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.Data;
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.linkwechatphonedetection.entity.LinkWechatPhoneDetection;
import org.jeecg.modules.demo.linkwechatphonedetection.entity.LinkWechatPhoneDetectionVo;
import org.jeecg.modules.demo.linkwechatphonedetection.service.ILinkWechatPhoneDetectionService;

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.linkwechatphonedetection.service.ILinkWechatPhoneDetectionVoService;
import org.jeecg.modules.demo.linkwechatphonedetectionstatistics.entity.LinkWechatPhoneDetectionStatistics;
import org.jeecg.modules.demo.linkwechatphonewhitelist.entity.LinkWechatPhoneWhiteList;
import org.jeecg.modules.demo.linkwechatphonewhitelist.service.ILinkWechatPhoneWhiteListService;
import org.jeecg.modules.demo.linkweibophonedetection.controller.LinkWeiboPhoneDetectionController;
import org.jeecg.modules.demo.linkweibophonedetection.entity.LinkWeiboPhoneDetection;
import org.jeecg.modules.demo.linkweibophonedetectionstatistics.entity.LinkWeiboPhoneDetectionStatistics;
import org.jeecg.modules.demo.linkweibophonewhitelist.entity.LinkWeiboPhoneWhiteList;
import org.jeecg.modules.demo.pagecontentcapture.controller.Params;
import org.jeecg.modules.demo.wechatpostsword.entity.WechatPostsWord;
import org.jeecg.modules.demo.weibo.entity.Weibo;
import org.jeecg.modules.demo.weibouser.entity.WeiboUser;
import org.jeecg.modules.spyderUtils.spider.JsoupReturn;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
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-07-03
 * @Version: V1.0
 */
@Api(tags="微信手机号检测")
@RestController
@RequestMapping("/linkwechatphonedetection/linkWechatPhoneDetection")
@Slf4j
public class LinkWechatPhoneDetectionController extends JeecgController<LinkWechatPhoneDetection, ILinkWechatPhoneDetectionService> {
	@Autowired
	private ILinkWechatPhoneDetectionService linkWechatPhoneDetectionService;

	@Autowired
	private ILinkWechatPhoneDetectionVoService iLinkWechatPhoneDetectionVoService;


	@Autowired
	private ILinkWechatPhoneWhiteListService iLinkWechatPhoneWhiteListService;
	/**
	 * 分页列表查询
	 *
	 * @param linkWechatPhoneDetection
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "微信手机号检测-分页列表查询")
	@ApiOperation(value="微信手机号检测-分页列表查询", notes="微信手机号检测-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LinkWechatPhoneDetection>> queryPageList(LinkWechatPhoneDetection linkWechatPhoneDetection,
																 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
																 HttpServletRequest req) {
		QueryWrapper<LinkWechatPhoneDetection> queryWrapper = QueryGenerator.initQueryWrapper(linkWechatPhoneDetection, req.getParameterMap());
		Page<LinkWechatPhoneDetection> page = new Page<LinkWechatPhoneDetection>(pageNo, pageSize);
		IPage<LinkWechatPhoneDetection> pageList = linkWechatPhoneDetectionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	/**
	 *   添加
	 *
	 * @param linkWechatPhoneDetection
	 * @return
	 */
	@AutoLog(value = "微信手机号检测-添加")
	@ApiOperation(value="微信手机号检测-添加", notes="微信手机号检测-添加")
	@RequiresPermissions("linkwechatphonedetection:link_wechat_phone_detection:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LinkWechatPhoneDetection linkWechatPhoneDetection) {
		linkWechatPhoneDetectionService.save(linkWechatPhoneDetection);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param linkWechatPhoneDetection
	 * @return
	 */
	@AutoLog(value = "微信手机号检测-编辑")
	@ApiOperation(value="微信手机号检测-编辑", notes="微信手机号检测-编辑")
	@RequiresPermissions("linkwechatphonedetection:link_wechat_phone_detection:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LinkWechatPhoneDetection linkWechatPhoneDetection) {
		linkWechatPhoneDetectionService.updateById(linkWechatPhoneDetection);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "微信手机号检测-通过id删除")
	@ApiOperation(value="微信手机号检测-通过id删除", notes="微信手机号检测-通过id删除")
	@RequiresPermissions("linkwechatphonedetection:link_wechat_phone_detection:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		linkWechatPhoneDetectionService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "微信手机号检测-批量删除")
	@ApiOperation(value="微信手机号检测-批量删除", notes="微信手机号检测-批量删除")
	@RequiresPermissions("linkwechatphonedetection:link_wechat_phone_detection:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.linkWechatPhoneDetectionService.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<LinkWechatPhoneDetection> queryById(@RequestParam(name="id",required=true) String id) {
		LinkWechatPhoneDetection linkWechatPhoneDetection = linkWechatPhoneDetectionService.getById(id);
		if(linkWechatPhoneDetection==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(linkWechatPhoneDetection);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param linkWechatPhoneDetection
    */
    @RequiresPermissions("linkwechatphonedetection:link_wechat_phone_detection:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LinkWechatPhoneDetection linkWechatPhoneDetection) {
        return super.exportXls(request, linkWechatPhoneDetection, LinkWechatPhoneDetection.class, "微信手机号检测");
    }

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




	 @DeleteMapping(value = "/batchHModifyTrue")
	 public Result<String> batchHModifyTrue(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 for (String sId : split) {
			 try {
				 LinkWechatPhoneDetection weiboContentCapture = linkWechatPhoneDetectionService.getById(sId);
				 weiboContentCapture.setIsRevise("2");
				 linkWechatPhoneDetectionService.updateById(weiboContentCapture);
			 }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 {
				 LinkWechatPhoneDetection weiboContentCapture = linkWechatPhoneDetectionService.getById(sId);
				 weiboContentCapture.setIsRevise("1");
				 linkWechatPhoneDetectionService.updateById(weiboContentCapture);
			 }catch (Exception e){
				 e.printStackTrace();
			 }
		 }
		 return Result.OK("批量设置成功!");
	 }

	 @ApiOperation(value="个人信息手机号检测-通过id查询", notes="个人信息手机号检测-通过id查询")
	 @GetMapping(value = "/queryByIdModifyTrue")
	 public Result<LinkWechatPhoneDetection> queryByIdModifyTrue(@RequestParam(name="id",required=true) String id) {
		 LinkWechatPhoneDetection linkWeiboPhoneDetection = linkWechatPhoneDetectionService.getById(id);
		 linkWeiboPhoneDetection.setIsRevise("2");
		 if(linkWeiboPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkWechatPhoneDetectionService.updateById(linkWeiboPhoneDetection);
		 return Result.OK(linkWeiboPhoneDetection);
	 }

	 @ApiOperation(value="个人信息手机号检测-通过id查询", notes="个人信息手机号检测-通过id查询")
	 @GetMapping(value = "/queryByIdModifyFalse")
	 public Result<LinkWechatPhoneDetection> queryByIdModifyFalse(@RequestParam(name="id",required=true) String id) {
		 LinkWechatPhoneDetection linkPersonalPhoneDetection = linkWechatPhoneDetectionService.getById(id);
		 linkPersonalPhoneDetection.setIsRevise("1");
		 if(linkPersonalPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkWechatPhoneDetectionService.updateById(linkPersonalPhoneDetection);
		 return Result.OK(linkPersonalPhoneDetection);
	 }
	 @Data
	 class PageContentCaptureTrueOrFalse{
		 private String wordName;
		 private String wordValue;
		 private boolean flag;
	 }

	 @GetMapping(value = "/queryByIdErrorList")
	 public Result<List<PageContentCaptureTrueOrFalse>> queryByIdErrorList(@RequestParam(name="id",required=true) String id) {
		 List<PageContentCaptureTrueOrFalse> list = new ArrayList<PageContentCaptureTrueOrFalse>();
		 LinkWechatPhoneDetection linkPersonalPhoneDetection = linkWechatPhoneDetectionService.getById(id);
		 if(linkPersonalPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 String phoneDetectionStrList = linkPersonalPhoneDetection.getStrList();
		 String[] split = phoneDetectionStrList.split(",");
		 for (String strPhone : split) {
			 PageContentCaptureTrueOrFalse captureTrueOrFalse = new PageContentCaptureTrueOrFalse();
			 captureTrueOrFalse.setWordName(strPhone);
			 captureTrueOrFalse.setWordValue(strPhone);
			 captureTrueOrFalse.setFlag(false);
			 QueryWrapper<LinkWechatPhoneWhiteList> queryWrapper = new QueryWrapper<LinkWechatPhoneWhiteList>();
			 queryWrapper.eq("white_content",strPhone);
			 Page<LinkWechatPhoneWhiteList> page = new Page<LinkWechatPhoneWhiteList>(1, 1);
			 IPage<LinkWechatPhoneWhiteList> pageList = iLinkWechatPhoneWhiteListService.page(page, queryWrapper);
			 List<LinkWechatPhoneWhiteList> records = pageList.getRecords();
			 if(records.size() > 0){
				 captureTrueOrFalse.setFlag(true);
			 }else{
				 captureTrueOrFalse.setFlag(false);
			 }
			 list.add(captureTrueOrFalse);
		 }
		 return Result.OK(list);
	 }

	 @RequestMapping(value = "/ignore", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> ignore(@RequestBody Params params) {
		 String weiboId = params.getWeiboId();
		 LinkWechatPhoneDetection byId = linkWechatPhoneDetectionService.getById(weiboId);
		 System.out.println(byId);
		 if(!ObjectUtils.isEmpty(params) && weiboId != ""){
			 if(params.getPlainOptions().length > 0){
				 for (String plainOption : params.getPlainOptions()) {
					 QueryWrapper<LinkWechatPhoneWhiteList> phoneWhiteListQueryWrapper = new QueryWrapper<LinkWechatPhoneWhiteList>();
					 phoneWhiteListQueryWrapper.eq("white_content",plainOption);
					 try {
						 iLinkWechatPhoneWhiteListService.remove(phoneWhiteListQueryWrapper);
					 }catch (Exception e){
						 e.printStackTrace();
					 }
				 }
			 }
			 if(params.getCheckedList().length > 0){
				 for (String plainOption : params.getCheckedList()) {
					 LinkWechatPhoneWhiteList phoneWhiteList = new LinkWechatPhoneWhiteList();
					 phoneWhiteList.setId(IdWorker.getIdStr());
					 String id = byId.getMsgBiz();
					 if(!ObjectUtils.isEmpty(id)){
						 phoneWhiteList.setMainSiteId(id);
					 }
					 String wechatId = byId.getWechatTitle();
					 if(!ObjectUtils.isEmpty(wechatId)){
						 phoneWhiteList.setMainSiteName(wechatId);
					 }
					 String msgBiz = byId.getMsgBiz();
					 if(!ObjectUtils.isEmpty(msgBiz)){
						 phoneWhiteList.setMainSiteUrl(msgBiz);
					 }
					 phoneWhiteList.setTenantId(byId.getTenantId()+"");
					 phoneWhiteList.setWhiteContent(plainOption);
					 if(!ObjectUtils.isEmpty(plainOption) && plainOption != ""){
						 try {
							 iLinkWechatPhoneWhiteListService.save(phoneWhiteList);
						 }catch (Exception e){
							 e.printStackTrace();
						 }
					 }
				 }
			 }
		 }
		 return Result.OK("编辑成功!");
	 }


	 @DeleteMapping(value = "/batchHAddWhitePhone")
	 public Result<String> ignoreAllBatch(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 LinkWechatPhoneDetection linkWeiboPhoneDetection = linkWechatPhoneDetectionService.getById(id);
				 String phoneDetectionStrList = linkWeiboPhoneDetection.getStrList();
				 String[] split1 = phoneDetectionStrList.split(",");
				 for (String phone : split1) {
					 QueryWrapper<LinkWechatPhoneWhiteList> phoneWhiteListQueryWrapper = new QueryWrapper<LinkWechatPhoneWhiteList>();
					 phoneWhiteListQueryWrapper.eq("white_content",phone);
					 try {
						 iLinkWechatPhoneWhiteListService.remove(phoneWhiteListQueryWrapper);
					 }catch (Exception e){
						 e.printStackTrace();
					 }
					 LinkWechatPhoneWhiteList phoneWhiteList = new LinkWechatPhoneWhiteList();
					 phoneWhiteList.setId(IdWorker.getIdStr());
					 String idId = linkWeiboPhoneDetection.getMsgBiz();
					 if(!ObjectUtils.isEmpty(linkWeiboPhoneDetection)){
						 phoneWhiteList.setMainSiteId(id);
					 }
					 String wechatId = linkWeiboPhoneDetection.getWechatTitle();
					 if(!ObjectUtils.isEmpty(wechatId)){
						 phoneWhiteList.setMainSiteName(wechatId);
					 }
					 String msgBiz = linkWeiboPhoneDetection.getMsgBiz();
					 if(!ObjectUtils.isEmpty(msgBiz)){
						 phoneWhiteList.setMainSiteUrl(msgBiz);
					 }
					 phoneWhiteList.setTenantId(linkWeiboPhoneDetection.getTenantId()+"");
					 phoneWhiteList.setWhiteContent(phone);
					 if(!ObjectUtils.isEmpty(phone) && phone != ""){
						 try {
							 iLinkWechatPhoneWhiteListService.save(phoneWhiteList);
						 }catch (Exception e){
							 e.printStackTrace();
						 }
					 }
				 }
			 }
		 }else{
			 Result.error("没有选择任何内容!");
		 }
		 return Result.OK("编辑成功!");
	 }

	 @DeleteMapping(value = "/batchHRemoveWhiteLink")
	 public Result<String> ignoreAllBatchCancell(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 LinkWechatPhoneDetection linkWeiboPhoneDetection = linkWechatPhoneDetectionService.getById(id);
				 String phoneDetectionStrList = linkWeiboPhoneDetection.getStrList();
				 String[] split1 = phoneDetectionStrList.split(",");
				 for (String phone : split1) {
					 QueryWrapper<LinkWechatPhoneWhiteList> phoneWhiteListQueryWrapper = new QueryWrapper<LinkWechatPhoneWhiteList>();
					 phoneWhiteListQueryWrapper.eq("white_content",phone);
					 try {
						 iLinkWechatPhoneWhiteListService.remove(phoneWhiteListQueryWrapper);
					 }catch (Exception e){
						 e.printStackTrace();
					 }
				 }
			 }
		 }else{
			 Result.error("没有选择任何内容!");
		 }
		 return Result.OK("编辑成功!");
	 }


	 @ApiOperation(value="个人信息手机号检测-通过id查询", notes="个人信息手机号检测-通过id查询")
	 @GetMapping(value = "/queryByIdIgnoreTrue")
	 public Result<LinkWechatPhoneDetection> queryByIdIgnoreTrue(@RequestParam(name="id",required=true) String id) {
		 LinkWechatPhoneDetection linkWeiboPhoneDetection= linkWechatPhoneDetectionService.getById(id);
		 linkWeiboPhoneDetection.setIsIgnore("1");
		 if(linkWeiboPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkWechatPhoneDetectionService.updateById(linkWeiboPhoneDetection);
		 return Result.OK(linkWeiboPhoneDetection);
	 }

	 @ApiOperation(value="个人信息手机号检测-通过id查询", notes="个人信息手机号检测-通过id查询")
	 @GetMapping(value = "/queryByIdIgnoreFalse")
	 public Result<LinkWechatPhoneDetection> queryByIdIgnoreFalse(@RequestParam(name="id",required=true) String id) {
		 LinkWechatPhoneDetection linkWeiboPhoneDetection = linkWechatPhoneDetectionService.getById(id);
		 linkWeiboPhoneDetection.setIsIgnore("2");
		 if(linkWeiboPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkWechatPhoneDetectionService.updateById(linkWeiboPhoneDetection);
		 return Result.OK(linkWeiboPhoneDetection);
	 }


	 @DeleteMapping(value = "/batchHIgnoreHandle")
	 public Result<String> batchHIgnoreHandle(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 try {
					 LinkWechatPhoneDetection byId = linkWechatPhoneDetectionService.getById(id);
					 byId.setIsIgnore("1");
					 linkWechatPhoneDetectionService.updateById(byId);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.OK("编辑成功!");
	 }

	 @DeleteMapping(value = "/batchHIgnoreHandleDelete")
	 public Result<String> batchHIgnoreHandleDelete(@RequestParam(name="ids",required=true) String ids) {
		 String[] split = ids.split(",");
		 if(split.length > 0){
			 for (String id : split) {
				 try {
					 LinkWechatPhoneDetection byId = linkWechatPhoneDetectionService.getById(id);
					 byId.setIsIgnore("2");
					 linkWechatPhoneDetectionService.updateById(byId);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.OK("编辑成功!");
	 }

	 @GetMapping(value = "/onHandleCheckWeiboContentCapture")
	 public Result<String> onHandleCheckWeiboContentCapture(HttpServletRequest request, LinkWechatPhoneDetection linkWeiboPhoneDetection) {
		 String selections = request.getParameter("selections");
		 QueryWrapper<LinkWechatPhoneDetection> queryWrapper = QueryGenerator.initQueryWrapper(linkWeiboPhoneDetection, request.getParameterMap());
		 if (oConvertUtils.isNotEmpty(selections)) {
			 List<String> selectionList = Arrays.asList(selections.split(","));
			 queryWrapper.in("id",selectionList);
			 List<LinkWechatPhoneDetection> exportList = linkWechatPhoneDetectionService.list(queryWrapper);
			 checkWeiboContentCapture(exportList);
		 }else{
			 queryWrapper.isNotNull("sensitive_list");
			 queryWrapper.ne("sensitive_list","");
			 queryWrapper.gt("sensitive_count",0);
			 Long count =  linkWechatPhoneDetectionService.count(queryWrapper);
			 Integer pageSize = 100;
			 Integer pageNum = (int)Math.ceil((double)count/pageSize);
			 for(int i = 1; i <= pageNum;i+=1){
				 Page<LinkWechatPhoneDetection> page = new Page<LinkWechatPhoneDetection>(i, pageSize);
				 IPage<LinkWechatPhoneDetection> pageList = linkWechatPhoneDetectionService.page(page, queryWrapper);
				 checkWeiboContentCapture(pageList.getRecords());
			 }
		 }
		 return Result.OK("成功");
	 }

	 private void checkWeiboContentCapture(List<LinkWechatPhoneDetection> records) {




	 }

 }
