package com.yinet.system.service.impl;

import com.yinet.common.core.text.Convert;
import com.yinet.system.domain.TouchScreenHelpInfo;
import com.yinet.system.mapper.TouchScreenHelpInfoMapper;
import com.yinet.system.service.TouchScreenHelpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 参数配置 服务层实现
 *
 * @author ruoyi
 */
@Service
public class TouchScreenHelpInfoServiceImpl implements TouchScreenHelpInfoService {
    @Autowired
    private TouchScreenHelpInfoMapper touchScreenHelpInfoMapper;

    @Override
    public List<TouchScreenHelpInfo> selectHelpInfoList() {
        List<TouchScreenHelpInfo> touchScreenHelpInfos = touchScreenHelpInfoMapper.selectHelpInfoList();
        for (int i = 0; i < touchScreenHelpInfos.size(); i++) {
            touchScreenHelpInfos.get(i).setContent(delHtmlTag(touchScreenHelpInfos.get(i).getContent()));
        }
        return touchScreenHelpInfos;
    }

    @Override
    public int insertHelpInfo(TouchScreenHelpInfo touchScreenHelpInfo) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        touchScreenHelpInfo.setTsHelpinfoId(uuid);
        touchScreenHelpInfo.setSubmitTime(new Date());
        String content = touchScreenHelpInfo.getContent();
        content = content.replaceAll("\r|\n", "");
        touchScreenHelpInfo.setContent(content);
        return touchScreenHelpInfoMapper.insertHelpInfo(touchScreenHelpInfo);
    }

    @Override
    public TouchScreenHelpInfo selectHelpInfoById(String id) {
        TouchScreenHelpInfo touchScreenHelpInfo = touchScreenHelpInfoMapper.selectHelpInfoById(id);
        return touchScreenHelpInfo;
    }

    @Override
    public int updateHelpInfo(TouchScreenHelpInfo touchScreenHelpInfo) {
        touchScreenHelpInfo.setSubmitTime(new Date());
        String content = touchScreenHelpInfo.getContent();
        content = content.replaceAll("\r|\n", "");
        touchScreenHelpInfo.setContent(content);
        return touchScreenHelpInfoMapper.updateHelpInfo(touchScreenHelpInfo);
    }

    @Override
    public int deletHelpInfo(String ids) {
        String[] helpInfoIds = Convert.toStrArray(ids);
        return touchScreenHelpInfoMapper.deletHelpInfo(helpInfoIds);
    }

    @Override
    public TouchScreenHelpInfo selectHelpInfoLimit(Integer count) {
        return touchScreenHelpInfoMapper.selectHelpInfoLimit(count);
    }

    /**
     * 删除Html标签显示在列表
     *
     * @param htmlStr
     * @return 纯文本
     */
    public static String delHtmlTag(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
        String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
        String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
        return htmlStr.trim(); // 返回文本字符串
    }
}
