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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
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.linkwechatbankcarddetection.entity.LinkWechatBankCardDetection;
import org.jeecg.modules.demo.linkwechatbankcarddetection.service.ILinkWechatBankCardDetectionService;

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.linkwechatbankcardwhitelist.entity.LinkWechatBankCardWhiteList;
import org.jeecg.modules.demo.linkwechatbankcardwhitelist.service.ILinkWechatBankCardWhiteListService;
import org.jeecg.modules.demo.linkwechatidnumberdetection.controller.LinkWechatIdNumberDetectionController;
import org.jeecg.modules.demo.linkwechatidnumberdetection.entity.LinkWechatIdNumberDetection;
import org.jeecg.modules.demo.linkwechatidnumberwhitelist.entity.LinkWechatIdNumberWhiteList;
import org.jeecg.modules.demo.pagecontentcapture.controller.Params;
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("/linkwechatbankcarddetection/linkWechatBankCardDetection")
@Slf4j
public class LinkWechatBankCardDetectionController extends JeecgController<LinkWechatBankCardDetection, ILinkWechatBankCardDetectionService> {
	@Autowired
	private ILinkWechatBankCardDetectionService linkWechatBankCardDetectionService;

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

    /**
    * 导出excel
    *
    * @param request
    * @param linkWechatBankCardDetection
    */
    @RequiresPermissions("linkwechatbankcarddetection:link_wechat_bank_card_detection:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LinkWechatBankCardDetection linkWechatBankCardDetection) {
        return super.exportXls(request, linkWechatBankCardDetection, LinkWechatBankCardDetection.class, "微信银行卡号检测");
    }

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


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

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

	 @ApiOperation(value="个人信息手机号检测-通过id查询", notes="个人信息手机号检测-通过id查询")
	 @GetMapping(value = "/queryByIdModifyFalse")
	 public Result<LinkWechatBankCardDetection> queryByIdModifyFalse(@RequestParam(name="id",required=true) String id) {
		 LinkWechatBankCardDetection linkPersonalPhoneDetection = linkWechatBankCardDetectionService.getById(id);
		 linkPersonalPhoneDetection.setIsRevise("1");
		 if(linkPersonalPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkWechatBankCardDetectionService.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>();
		 LinkWechatBankCardDetection linkPersonalPhoneDetection = linkWechatBankCardDetectionService.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<LinkWechatBankCardWhiteList> queryWrapper = new QueryWrapper<LinkWechatBankCardWhiteList>();
			 queryWrapper.eq("white_content",strPhone);
			 Page<LinkWechatBankCardWhiteList> page = new Page<LinkWechatBankCardWhiteList>(1, 1);
			 IPage<LinkWechatBankCardWhiteList> pageList = iLinkWechatBankCardWhiteListService.page(page, queryWrapper);
			 List<LinkWechatBankCardWhiteList> 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();
		 LinkWechatBankCardDetection byId = linkWechatBankCardDetectionService.getById(weiboId);
		 System.out.println(byId);
		 if(!ObjectUtils.isEmpty(params) && weiboId != ""){
			 if(params.getPlainOptions().length > 0){
				 for (String plainOption : params.getPlainOptions()) {
					 QueryWrapper<LinkWechatBankCardWhiteList> phoneWhiteListQueryWrapper = new QueryWrapper<LinkWechatBankCardWhiteList>();
					 phoneWhiteListQueryWrapper.eq("white_content",plainOption);
					 try {
						 iLinkWechatBankCardWhiteListService.remove(phoneWhiteListQueryWrapper);
					 }catch (Exception e){
						 e.printStackTrace();
					 }
				 }
			 }
			 if(params.getCheckedList().length > 0){
				 for (String plainOption : params.getCheckedList()) {
					 LinkWechatBankCardWhiteList phoneWhiteList = new LinkWechatBankCardWhiteList();
					 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 {
							 iLinkWechatBankCardWhiteListService.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) {
				 LinkWechatBankCardDetection linkWeiboPhoneDetection = linkWechatBankCardDetectionService.getById(id);
				 String phoneDetectionStrList = linkWeiboPhoneDetection.getStrList();
				 String[] split1 = phoneDetectionStrList.split(",");
				 for (String phone : split1) {
					 QueryWrapper<LinkWechatBankCardWhiteList> phoneWhiteListQueryWrapper = new QueryWrapper<LinkWechatBankCardWhiteList>();
					 phoneWhiteListQueryWrapper.eq("white_content",phone);
					 try {
						 iLinkWechatBankCardWhiteListService.remove(phoneWhiteListQueryWrapper);
					 }catch (Exception e){
						 e.printStackTrace();
					 }
					 LinkWechatBankCardWhiteList phoneWhiteList = new LinkWechatBankCardWhiteList();
					 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 {
							 iLinkWechatBankCardWhiteListService.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) {
				 LinkWechatBankCardDetection linkWeiboPhoneDetection = linkWechatBankCardDetectionService.getById(id);
				 String phoneDetectionStrList = linkWeiboPhoneDetection.getStrList();
				 String[] split1 = phoneDetectionStrList.split(",");
				 for (String phone : split1) {
					 QueryWrapper<LinkWechatBankCardWhiteList> phoneWhiteListQueryWrapper = new QueryWrapper<LinkWechatBankCardWhiteList>();
					 phoneWhiteListQueryWrapper.eq("white_content",phone);
					 try {
						 iLinkWechatBankCardWhiteListService.remove(phoneWhiteListQueryWrapper);
					 }catch (Exception e){
						 e.printStackTrace();
					 }
				 }
			 }
		 }else{
			 Result.error("没有选择任何内容!");
		 }
		 return Result.OK("编辑成功!");
	 }


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

	 @ApiOperation(value="个人信息手机号检测-通过id查询", notes="个人信息手机号检测-通过id查询")
	 @GetMapping(value = "/queryByIdIgnoreFalse")
	 public Result<LinkWechatBankCardDetection> queryByIdIgnoreFalse(@RequestParam(name="id",required=true) String id) {
		 LinkWechatBankCardDetection linkWeiboPhoneDetection = linkWechatBankCardDetectionService.getById(id);
		 linkWeiboPhoneDetection.setIsIgnore("2");
		 if(linkWeiboPhoneDetection==null) {
			 return Result.error("未找到对应数据");
		 }
		 linkWechatBankCardDetectionService.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 {
					 LinkWechatBankCardDetection byId = linkWechatBankCardDetectionService.getById(id);
					 byId.setIsIgnore("1");
					 linkWechatBankCardDetectionService.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 {
					 LinkWechatBankCardDetection byId = linkWechatBankCardDetectionService.getById(id);
					 byId.setIsIgnore("2");
					 linkWechatBankCardDetectionService.updateById(byId);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
			 }
		 }
		 return Result.OK("编辑成功!");
	 }

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

	 private void checkWeiboContentCapture(List<LinkWechatBankCardDetection> records) {




	 }



 }
