package org.jeecg.modules.demo.homepageerrorword.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 org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.homepageerrorword.entity.HomePageErrorWord;
import org.jeecg.modules.demo.homepageerrorword.service.IHomePageErrorWordService;

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.pagecontentcapture.entity.PageContentCapture;
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.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
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-11-21
 * @Version: V1.0
 */
@Api(tags="首页错敏词")
@RestController
@RequestMapping("/homepageerrorword/homePageErrorWord")
@Slf4j
public class HomePageErrorWordController extends JeecgController<HomePageErrorWord, IHomePageErrorWordService> {
	@Autowired
	private IHomePageErrorWordService homePageErrorWordService;
	
	/**
	 * 分页列表查询
	 *
	 * @param homePageErrorWord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "首页错敏词-分页列表查询")
	@ApiOperation(value="首页错敏词-分页列表查询", notes="首页错敏词-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<HomePageErrorWord>> queryPageList(HomePageErrorWord homePageErrorWord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HomePageErrorWord> queryWrapper = QueryGenerator.initQueryWrapper(homePageErrorWord, req.getParameterMap());
		Page<HomePageErrorWord> page = new Page<HomePageErrorWord>(pageNo, pageSize);
		IPage<HomePageErrorWord> pageList = homePageErrorWordService.page(page, queryWrapper);
		for (HomePageErrorWord record : pageList.getRecords()) {
			Document parse1 = Jsoup.parse(record.getSensitiveList());
			Elements elements1 = parse1.select("span.proofreadSignClass.errorWordMark");
			String sensitiveErrorList = "";
			String errorMessage = "";
			for(Element element : elements1){
				String title = element.attr("title")+"<br>";
				errorMessage += Jsoup.parse(title).text() +"<br>";
				String stringError = element.toString();
				int i = record.getRetMsg().indexOf(stringError);
				String t1 = "";
				String t2 = "";
				try {
					t1 = record.getRetMsg().substring(i-20,i);
					t2 = record.getRetMsg().substring(i+(stringError.length()),i+(stringError.length())+20);
				}catch (Exception e){
					e.printStackTrace();
				}
				sensitiveErrorList += t1 + stringError +t2+"<br>";
			}
			record.setErrorMessage(errorMessage);
			record.setSensitiveErrorList(sensitiveErrorList);
		}
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param homePageErrorWord
	 * @return
	 */
	@AutoLog(value = "首页错敏词-添加")
	@ApiOperation(value="首页错敏词-添加", notes="首页错敏词-添加")
	@RequiresPermissions("homepageerrorword:home_page_error_word:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody HomePageErrorWord homePageErrorWord) {
		homePageErrorWordService.save(homePageErrorWord);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param homePageErrorWord
	 * @return
	 */
	@AutoLog(value = "首页错敏词-编辑")
	@ApiOperation(value="首页错敏词-编辑", notes="首页错敏词-编辑")
	@RequiresPermissions("homepageerrorword:home_page_error_word:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody HomePageErrorWord homePageErrorWord) {
		homePageErrorWordService.updateById(homePageErrorWord);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "首页错敏词-通过id删除")
	@ApiOperation(value="首页错敏词-通过id删除", notes="首页错敏词-通过id删除")
	@RequiresPermissions("homepageerrorword:home_page_error_word:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		homePageErrorWordService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "首页错敏词-批量删除")
	@ApiOperation(value="首页错敏词-批量删除", notes="首页错敏词-批量删除")
	@RequiresPermissions("homepageerrorword:home_page_error_word:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.homePageErrorWordService.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<HomePageErrorWord> queryById(@RequestParam(name="id",required=true) String id) {
		HomePageErrorWord homePageErrorWord = homePageErrorWordService.getById(id);



		if(homePageErrorWord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(homePageErrorWord);
	}

	 //@AutoLog(value = "首页错敏词-通过id查询")
	 @ApiOperation(value="首页错敏词-通过id查询", notes="首页错敏词-通过id查询")
	 @GetMapping(value = "/queryByIdErr")
	 public Result<HomePageErrorWord> queryByIdErr(@RequestParam(name="id",required=true) String id) {
		 HomePageErrorWord homePageErrorWord = homePageErrorWordService.getById(id);

		 if(!ObjectUtils.isEmpty(homePageErrorWord)){
			 String retMsg = homePageErrorWord.getRetMsg();
			 Document parse1 = Jsoup.parse(homePageErrorWord.getRetMsg());
			 Elements elements1 = parse1.select("span.proofreadSignClass.errorWordMark");
			 String sensitiveErrorList = "";
			 String errorMessage = "";
			 List<String> errorMsg = new ArrayList<String>();
			 int index = 0;
			 for(Element element : elements1){
				 try {
					 String reHtml = "<span id='a"+index+"'>"+element.toString()+"</span>";
					 retMsg = retMsg.replace(element.toString(),reHtml);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
				 String title = element.attr("title")+"<br>";
				 errorMessage += Jsoup.parse(title).text() +"<br>";
				 String stringError = element.toString();
				 int i = homePageErrorWord.getRetMsg().indexOf(stringError);
				 String t1 = "";
				 String t2 = "";
				 try {
					 t1 = homePageErrorWord.getRetMsg().substring(i-20,i);
					 t2 = homePageErrorWord.getRetMsg().substring(i+(stringError.length()),i+(stringError.length())+20);
				 }catch (Exception e){
					 e.printStackTrace();
				 }
				 sensitiveErrorList += t1 + stringError +t2+"<br>";
				 errorMsg.add("<a href='#a"+index+"'>"+title+"</a>");
				 index++;
			 }
			 homePageErrorWord.setRetMsg(retMsg);
			 homePageErrorWord.setErrorMsg(errorMsg);
			 homePageErrorWord.setErrorMessage(errorMessage);
			 homePageErrorWord.setSensitiveErrorList(sensitiveErrorList);
		 }

		 if(homePageErrorWord==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(homePageErrorWord);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param homePageErrorWord
    */
    @RequiresPermissions("homepageerrorword:home_page_error_word:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HomePageErrorWord homePageErrorWord) {
        return super.exportXls(request, homePageErrorWord, HomePageErrorWord.class, "首页错敏词");
    }

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

}
