package org.diagbot.nlp.controller;

import com.github.pagehelper.PageInfo;
import org.diagbot.common.dao.model.StandardInfo;
import org.diagbot.common.dao.model.wrapper.StandardInfoWrapper;
import org.diagbot.common.service.StandardInfoService;
import org.diagbot.nlp.participle.cfg.Configuration;
import org.diagbot.nlp.participle.cfg.DefaultConfig;
import org.diagbot.nlp.participle.word.Segment;
import org.diagbot.pub.api.Response;
import org.diagbot.pub.api.ResponsePage;
import org.diagbot.pub.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.*;

/**
 * Created by fyeman on 2018/1/17.
 */
@Controller
@RequestMapping("/kl/standard_info")
public class StandardInfoController extends BaseController<StandardInfo, StandardInfoWrapper, Long> {
    {
        listView = "pages/kl/standard_info/list";
    }

    @Autowired
    StandardInfoService standardInfoService;

    @ModelAttribute
    public void initBinder() {
        super.setBaseService(standardInfoService);
    }

    @RequestMapping({"/generate"})
    @ResponseBody
    public Response generate(HttpServletRequest request) {
        Response response = new Response();
        response.start();

        List<StandardInfoWrapper> nullCategoryIdList = standardInfoService.selectNullCategoryIdWrapper();
        if (nullCategoryIdList.size() > 0) {
            return response.paramFailure("有新的特征未标识category_id");
        }

        try {
            List<StandardInfo> list = standardInfoService.selectList(new HashMap<String, Object>());
            Map<String, String> libraryMap = new HashMap<>(10);
            String path = this.getClass().getClassLoader().getResource("").getPath();
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path + "/tc.dict"), "UTF-8");
            for (StandardInfo sl : list) {
                if (libraryMap.get(sl.getName()) == null) {
                    libraryMap.put(sl.getName(), String.valueOf(sl.getCategoryId()));
                } else {
                    libraryMap.put(sl.getName(), libraryMap.get(sl.getName()) + "," + sl.getCategoryId());
                }
            }
            List<Map.Entry<String, String>> libraryList = new ArrayList<Map.Entry<String, String>>(libraryMap.entrySet());
            Collections.sort(libraryList, new Comparator<Map.Entry<String, String>>() {
                public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                    return o1.getKey().compareTo(o2.getKey());
                }
            });

            List<String> dicts = new ArrayList<>();
            for (Map.Entry<String, String> entry : libraryList) {
                out.write(entry.getKey() + "|9|" + entry.getValue() + "\n");
                dicts.add(entry.getKey() + "|9|" + entry.getValue());
            }
            out.close();
            Configuration configuration = new DefaultConfig();
            Segment segment = configuration.loadMainDict(dicts);
            request.getServletContext().setAttribute("tc_segment", segment);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return response.paramFailure(ioe.getMessage());
        }
        return response.success();
    }

    @RequestMapping({"/page_data"})
    @ResponseBody
    public ResponsePage<StandardInfoWrapper> pageData(HttpServletRequest request, Integer start, Integer length, String search_name) {
        ResponsePage<StandardInfoWrapper> response = new ResponsePage();
        Map<String, Object> condition = new HashMap<>();
        condition.put("name", search_name);
        PageInfo<StandardInfoWrapper> pageInfo = standardInfoService.selectListWrapperPage(condition, Integer.valueOf(start.intValue() == 0 ? start.intValue() : start.intValue() / length), length);
        response.setRecordsTotal(pageInfo.getTotal());
        response.setRecordsFiltered(pageInfo.getTotal());
        response.setData(pageInfo.getList());
        return response;
    }

}
