package com.dgtis.dorm.manager.wx.web;

import net.sf.json.JSONObject;
import com.dgtis.dorm.manager.db.domain.AdministrativeItems;
import com.dgtis.dorm.manager.db.param.ItemsQueryParam;
import com.dgtis.dorm.manager.db.service.AdministrativeItemsService;
import com.dgtis.dorm.manager.db.util.ArtificialUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 查询政务事项
 */
@RestController
@RequestMapping("/wx/querymatter")
public class QueryMatterController {

    @Resource
    private AdministrativeItemsService administrativeItemsService;

    /**
     * 接口
     *   根据一级事项名称与二级事项名称，
     *   查询返回二级事项的小程序二维码
     *
     */
    @RequestMapping("loadStrativeItems")
    @ResponseBody
    public String loadStrativeItems(HttpServletRequest request){
        String showType = ArtificialUtil.WORDS_TYPE;
        String resultStr = "sorry,没有找到您要查询的相关内容!";
        // 读取请求内容
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = reader.readLine()) != null){
                sb.append(line);
            }
            reader.close();
            JSONObject jsonObject = JSONObject.fromObject(sb.toString());
            System.out.println("jsonObject = "+jsonObject.toString());
            if (null != jsonObject) {
                if(jsonObject.containsKey("VoiceStr") && jsonObject.containsKey("VoiceDisp")){
                    String VoiceStr = jsonObject.getString("VoiceStr");
                    String VoiceDisp = jsonObject.getString("VoiceDisp");
                    String oneTitle = ArtificialUtil.getValueByKey(VoiceDisp,"oneTitle");
                    String twoTitle = ArtificialUtil.getValueByKey(VoiceDisp,"twoTitle");
                    if(StringUtils.isNotBlank(oneTitle) && StringUtils.isNotBlank(twoTitle)){

                        //判断最后一个字母是否为g,是则认为是后鼻音，去掉最后一个字母
                        String oneTitlePY = ArtificialUtil.getPingYin(oneTitle);
                        if(oneTitlePY.endsWith("g")) {
                            oneTitlePY = oneTitlePY.substring(0,oneTitlePY.length() - 1);
                        }

                        String twoTitlePY = ArtificialUtil.getPingYin(twoTitle);
                        if(twoTitlePY.endsWith("g")) {
                            twoTitlePY = twoTitlePY.substring(0,twoTitlePY.length() - 1);
                        }

                        List<String> strList = administrativeItemsService.findQrcodeStorageUrlByNames(oneTitlePY.toUpperCase(), twoTitlePY.toUpperCase());
                        if (strList != null && strList.size() > 0){
                            showType = ArtificialUtil.IMAGE_TYPE;
                            resultStr = strList.get(0);
                        } else {
                            resultStr = "对不起，我使用了洪荒之力，也没有找到"+oneTitle+"下的"+twoTitle+",换一个查询试一试";
                        }
                    } else {
                        resultStr = "刚刚没有听清楚,您说的声音再大一点";
                    }
                } else {
                    resultStr = "查询失败了，VoiceStr与VoiceDisp参数有误";
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject object = new JSONObject();
        object.put("showType", showType);
        object.put("showValue", resultStr);
        System.out.println(object.toString());
        return object.toString();
    }

}
