package com.yihu.hos.standard.controller;

import com.yihu.hos.common.constants.ContextAttributes;
import com.yihu.hos.interceptor.LocalContext;
import com.yihu.hos.standard.match.dictionary.DictItemStrategyExecute;
import com.yihu.hos.standard.service.adapter.AdapterDictService;
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 com.yihu.hos.web.framework.util.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * Created by LLH on 2016/1/6.
 */
@RestController("AdapterDictController")
@RequestMapping("/adapterCenter")
//@Api(protocols = "https", value = "AdapterDictController", description = "适配字典管理", tags = {"适配字典"})
public class AdapterDictController extends BaseController {

    @Resource(name = AdapterDictService.BEAN_ID)
    private AdapterDictService dictService;
    /**
     * 查询字典列表信息（get）
     * @param adapterVersion
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/searchDictList")
    @ApiOperation(value = "获取字典列表", response = DetailModelResult.class, produces = "application/json", notes = "获取字典列表")
    public DetailModelResult searchDictList(
            @ApiParam(name = "adapterVersion", value = "适配标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "condition", value = "查询条件：编码或名称")
            @RequestParam(value = "condition", required = false) String condition,
            @ApiParam(name = "order", value = "Order param,Must be json.ascending:asc,descending:desc")
            @RequestParam(value = "order", required = false) String order,
            @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) {
        return dictService.getDetailResult(adapterVersion, condition, order, rows, page);
    }

    @RequestMapping(value = "/getForDictId")
    @ApiOperation(value = "获取字典", response = Result.class, produces = "application/json", notes = "获取字典")
    public Result get(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "dictId", value = "字典ID")
            @RequestParam(value = "dictId") Integer dictId) {
        return dictService.get(adapterVersion, dictId);
    }

    @RequestMapping(value = "/delDict")
    @ApiOperation(value = "删除字典", produces = "application/json", notes = "删除字典")
    public void delDict(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "dictId", value = "字典ID")
            @RequestParam(value = "dictId") Integer dictId) {
        dictService.delete(adapterVersion, dictId);
    }
    
    @RequestMapping(value = "/updateDict")
    @ApiOperation(value = "修改字典", response = Result.class, produces = "application/json", notes = "修改字典")
    public Result updateDict(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "dict", value = "字典")
            @RequestParam(value = "dict") String dict) {
        return dictService.modify(adapterVersion, dict);
    }

    @RequestMapping(value = "/addDict")
    @ApiOperation(value = "保存字典", response = Result.class, produces = "application/json", notes = "保存字典")
    public Result addDict(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "dict", value = "字典")
            @RequestParam(value = "dict") String dict) {
        return dictService.add(adapterVersion, dict);
    }

    @RequestMapping(value = "/isStrategyDic", method = RequestMethod.POST , produces = "application/json")
    @ResponseBody
    @ApiOperation(value = "是否智能匹配", response = Object.class, produces = "application/json", notes = "是否智能匹配")
    public Result isStrategy(
            @ApiParam(value = "匹配version", required = true) @RequestParam("version") String version
    ) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setData("{\"isAdapt\":\"" + DictItemStrategyExecute.isAdapt(version) + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

    /**
     * 智能匹配
     *
     * @param version
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/strategyDic", method = RequestMethod.POST)
    @ApiOperation(value = "智能匹配", response = Object.class, produces = "application/json", notes = "智能匹配")
    public Result strategyDic(
            @ApiParam(value = "匹配version", required = true) @RequestParam("version") String version,
            @ApiParam(value = "平台标准version", required = true) @RequestParam("std_version") String std_version,
            @ApiParam(value = "适配方案版本号", required = true) @RequestParam("adapter_std_version") String adapter_std_version
    ) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            synchronized (DictItemStrategyExecute.class){
                if (!DictItemStrategyExecute.isAdapt(version)) {
                    //判断之前是否已经有匹配有的话不进行匹配
                    DictItemStrategyExecute.setVersionYes(version);
                }else{
                    return actionResult;
                }
            }

            String schemaName = LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA);

            dictService.strategy(schemaName,version,std_version,adapter_std_version);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

    @RequestMapping(value = "/getStrategyDicSize", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "智能匹配进行", response = Object.class, produces = "application/json", notes = "智能匹配进行")
    public Result getStrategyDicSize(
            @ApiParam(value = "匹配version", required = true) @RequestParam("version") String version
    ) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setData(DictItemStrategyExecute.getAllNumAndAdaptNum(version));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

    @RequestMapping(value = "/endStrategyDic", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "智能匹配結束", response = Object.class, produces = "application/json", notes = "智能匹配結束")
    public Result endStrategyDic(
            @ApiParam(value = "匹配version", required = true) @RequestParam("version") String version
    ) throws Exception {
        ActionResult actionResult = new ActionResult();
        try {
            DictItemStrategyExecute.removeAdapt(version);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

}
