package com.org.hktshop.service.enquiry.impl;

import com.org.hktshop.Interface.system.HktSensitiveWordsService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.entity.enquiry.Enquiry;
import com.org.hktshop.entity.system.HktSensitiveWords;
import com.org.hktshop.service.enquiry.EnquiryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class EnquiryServiceImpl extends BaseService<Enquiry> implements EnquiryService {
    @Autowired
    private HktSensitiveWordsService hktSensitiveWordsService;

    private static Set<String> sensitiveWordsSet = new HashSet<>();

    @Override
    public BaseResult insertEnquiry(Enquiry enquiry) {
        List<HktSensitiveWords> hktSensitiveWords = hktSensitiveWordsService.selectHktSensitiveWords(new HktSensitiveWords());
        for(HktSensitiveWords hsw : hktSensitiveWords){
            sensitiveWordsSet.add(hsw.getName());
        }

        int i = 0;
        for (String str : sensitiveWordsSet) {
//            if(hktUser.getNickname().contains(str) || hktUser.getName().contains(str)){
//                return BaseResult.fail("昵称或姓名不能包含敏感字符");
//            }
            if (!StringUtils.isEmpty(enquiry.getPhoneNumber()) && enquiry.getPhoneNumber().contains(str)) {
                enquiry.setPhoneNumber("***");
                i++;
            }

            if (!StringUtils.isEmpty(enquiry.getName()) && enquiry.getName().contains(str)) {
                enquiry.setName("***");
                i++;
            }

            if (!StringUtils.isEmpty(enquiry.getContent()) && enquiry.getContent().contains(str)) {
                enquiry.setContent("***");
                i++;
            }

            if (!StringUtils.isEmpty(enquiry.getCommodityName()) && enquiry.getCommodityName().contains(str)) {
                enquiry.setCommodityName("***");
                i++;
            }

        }
        if (i > 0) {
            return BaseResult.fail("提交数据不能包含敏感字符",enquiry);
        }
        this.updateJdbc(enquiry);//更新数据库
        return BaseResult.success();

    }
    @Override
    public List<Enquiry> queryByEnquiry(Enquiry enquiry) {
        String  sql = "select * from enquiry  ORDER BY create_time DESC";;
        List<Enquiry> enquiries = this.baseJdbcDao.queryList(sql, enquiry);
        return enquiries;
    }

}
