package com.yihu.hos.crawler.controller;

import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.crawler.service.CrawlerService;
import com.yihu.hos.standard.service.adapter.AdapterSchemeService;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.DetailModelResult;
import com.yihu.hos.web.framework.model.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 目前版本只需要采集，上传和注册病人档案就可了。
 * <p>
 * Created by Airhead on 2015/12/16.
 */
@RequestMapping("/crawler")
@Controller("crawlerController")
@Api(protocols = "http", value = "CrawlerController", description = "档案采集接口", tags = {"采集"})
public class CrawlerController {

    @Resource
    CrawlerService crawlerService;

    @Resource(name = AdapterSchemeService.BEAN_ID)
    private AdapterSchemeService adapterSchemeService;

    /**
     * 任务编排
     */
    @RequestMapping("jobLayout")
    public String jobLayout(Model model) {
        model.addAttribute("contentPage", "/crawler/jobLayout");
        return "partView";
    }

    /*
   任务编排数据映射
    */
    @RequestMapping("jobDataMapping")
    public String jobDataMapping(Model model, Integer versionId, String datasetIdStr) {
        try {
            model.addAttribute("versionId", versionId);
            model.addAttribute("datasetIdStr", datasetIdStr);
            model.addAttribute("contentPage", "/crawler/dataMapping");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "/crawler/dataMapping");
            return "pageView";
        }
    }

    @RequestMapping("datasetDetail")
    public String datasetDetail(Model model, Integer schemeId, String schemeNm, String cacheDatasetId, String versionId) {
        model.addAttribute("contentPage", "/crawler/datasetDetail");
        model.addAttribute("schemeId", schemeId);
        model.addAttribute("versionId", versionId);
        model.addAttribute("schemeNm", schemeNm);
        model.addAttribute("cacheDatasetId", cacheDatasetId);
        return "pageView";
    }

    /**
     * 保存任务编排数据
     */
    @RequestMapping(value = "saveDateSet", method = RequestMethod.POST)
    @ApiOperation(value = "保存任务编排", produces = "application/json", notes = "保存任务编排")
    @ResponseBody
    public Result saveJobData(
            @ApiParam(name = "job", value = "任务编排信息", required = true)
            @RequestParam(value = "job") String jobInfo,
            @ApiParam(name = "rows", value = "当前行数", required = true)
            @RequestParam(value = "rows") Integer rows,
            @ApiParam(name = "page", value = "当前页数", required = true)
            @RequestParam(value = "page") Integer page) {
        try {
            crawlerService.saveJobData(jobInfo, rows, page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存失败");
        }
        return Result.success("保存成功");
    }


    /**
     * 获取适配方案 数据集关系
     *
     * @param versionId 适配方案版本ID
     * @return
     */
    @RequestMapping("getSchemeDataSets")
    @ResponseBody
    public DetailModelResult getSchemeDataSets(
            @RequestParam(value = "versionId", required = true) Integer versionId,
            @RequestParam(value = "name", required = false) String name
    ) {
        try {
            return crawlerService.getSchemeDataset(versionId, name);
        } catch (Exception e) {
            e.printStackTrace();
            return new DetailModelResult();
        }
    }

    /**
     * 保存适配方案 数据映射关系
     *
     * @param version
     * @param data    映射关系
     * @return
     */
    @RequestMapping("saveSchemeRelation")
    @ResponseBody
    public ActionResult addSchemeRelation(
            @RequestParam(value = "version", required = true) String version,
            @RequestParam(value = "data", required = true) String data
    ) {
        try {
            return crawlerService.saveDataSetRelation(version, data);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new ActionResult();
        }
    }

    @RequestMapping(value = "getRelations", produces = "text/html; charset=utf-8")
    @ResponseBody
    public String getRelations(
            @RequestParam(value = "versionId", required = true) Integer versionId,
            @RequestParam(value = "datasetIdStr", required = false) String datasetIdStr,
            @RequestParam(value = "lines", required = false) String lines,
            HttpServletResponse response) {
        try {
            return crawlerService.getRelations(versionId, datasetIdStr, lines);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("setRelationsCache")
    @ResponseBody
    public Result setRelationsCache(
            @RequestParam(value = "versionId", required = true) Integer versionId,
            @RequestParam(value = "lines", required = false) String lines
    ) {
        try {
            crawlerService.setLinesCache(versionId, lines);
            return Result.success("缓存映射关系成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("缓存映射关系失败");
    }


    /**
     * 删除任务编排数据
     */
    @RequestMapping(value = "deleteDateSet", method = RequestMethod.POST)
    @ApiOperation(value = "删除任务编排", produces = "application/json", notes = "删除任务编排")
    @ResponseBody
    public Result deleteJobData(
            @ApiParam(name = "version", value = "版本号", required = true)
            @RequestParam(value = "version") String version) {
        if (version != null) {
            String message = crawlerService.deleteJobData(version);
            if (StringUtil.isEmpty(message)) {
                return Result.success("删除成功");
            } else {
                return Result.error(message);
            }
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 分页显示任务编排数据
     */
    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ApiOperation(value = "分页显示任务编排", produces = "application/json", notes = "分页显示任务编排")
    @ResponseBody
    public DetailModelResult listJobData(
            @ApiParam(name = "rows", value = "Limit the size of result set. Must be an integer")
            @RequestParam(value = "rows", required = false) Integer rows,
            @ApiParam(name = "page", value = "Start position of result set. Must be an integer")
            @RequestParam(value = "page", required = false) Integer page) throws Exception {

        return crawlerService.getDataSetResult(rows, page);
    }


    @RequestMapping(value = "getSchemeList", method = RequestMethod.POST)
    @ApiOperation(value = "获取适配方案-方案版本下拉框", produces = "application/json", notes = "获取适配方案-方案版本下拉框")
    @ResponseBody
    public DetailModelResult getSchemeList() {
        DetailModelResult result = adapterSchemeService.getAdapterSchemeResultModelList();
        return result;
    }


    /**
     * 获取任务编排保存数据集
     */
    @RequestMapping(value = "savedJobData", method = RequestMethod.POST)
    @ApiOperation(value = "获取保存的数据集", produces = "application/json", notes = "保存的数据集")
    @ResponseBody
    public DetailModelResult ListSavedJobData(
            @ApiParam(name = "version", value = "版本号", required = true)
            @RequestParam(value = "version") Integer version
    ) {
        return crawlerService.getDataSetSavedResult(version);
    }


    /**
     * 获取任务编排保存适配方案-方案版本
     */
    @RequestMapping(value = "savedSchemeList", method = RequestMethod.POST)
    @ApiOperation(value = "获取保存的适配方案", produces = "application/json", notes = "保存的适配方案")
    @ResponseBody
    public DetailModelResult SavedSchemeList() {
        return crawlerService.getSchemeSavedResult();
    }


}
