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.metadata.MetadataStrategyExecute;
import com.yihu.hos.standard.service.adapter.AdapterDatasetService;
import com.yihu.hos.standard.service.adapter.AdapterMetadataService;
import com.yihu.hos.standard.service.bo.AdapterVersion;
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("AdapterMetadataController")
@RequestMapping("/adapterCenter")
//@Api(protocols = "https", value = "AdapterMetadataController", description = "适配字典管理", tags = {"适配字典"})
public class AdapterMetadataController extends BaseController {
    @Resource(name = AdapterMetadataService.BEAN_ID)
    private AdapterMetadataService metadataService;
    @Resource(name = AdapterDatasetService.BEAN_ID)
    private AdapterDatasetService adapterDatasetService;
    /**
     * 查询数据元列表信息（get）
     * @param adapterVersion
     * @param condition
     * @param order
     * @param rows
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/searchMetadataList")
    @ApiOperation(value = "获取数据元列表", response = DetailModelResult.class, produces = "application/json", notes = "获取数据元列表")
    public DetailModelResult getMetadataList(
            @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)  throws Exception{
        return  metadataService.getDetailResult(adapterVersion, condition, order, rows, page);
    }

    @RequestMapping("/getForMetadataId")
     @ApiOperation(value = "获取数据元", response = Result.class, produces = "application/json", notes = "获取数据元")
     public Result get(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "metadataId", value = "数据元ID")
            @RequestParam(value = "metadataId") Integer metadataId) {
        AdapterVersion version = new AdapterVersion(adapterVersion);
        return metadataService.get(version.getMetaDataTableName(), metadataId);
    }

    @RequestMapping("/updateMetadata")
    @ApiOperation(value = "修改数据元", response = Result.class, produces = "application/json", notes = "修改数据元")
    public Result updateMetadata(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "metadata", value = "数据元")
            @RequestParam(value = "metadata") String metadata) throws Exception {
        return metadataService.modify(adapterVersion, metadata);
    }

    @RequestMapping("/addMetadata")
    @ApiOperation(value = "保存数据元", response = Result.class, produces = "application/json", notes = "保存数据元")
    public Result addMetadata(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "metadata", value = "数据元")
            @RequestParam(value = "metadata") String metadata) throws Exception {
        return metadataService.add(adapterVersion, metadata);
    }

    @RequestMapping("/updateMetadatas")
    @ApiOperation(value = "修改数据元", response = DetailModelResult.class, produces = "application/json", notes = "保存数据元")
    public DetailModelResult updateMetadatas(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "list", value = "数据元列表")
            @RequestParam(value = "list") String list) {
        return metadataService.updateMetadatas(adapterVersion, list);
    }

    @RequestMapping("/delMetadata")
    @ApiOperation(value = "删除数据元", response = Result.class, produces = "application/json", notes = "删除数据元")
    public Result delete(
            @ApiParam(name = "adapterVersion", value = "标准版本号")
            @RequestParam(value = "adapterVersion") String adapterVersion,
            @ApiParam(name = "metadataId", value = "数据元ID")
            @RequestParam(value = "metadataId") Integer metadataId,
            @ApiParam(name = "stdDatasetId", value = "标准数据集ID")
            @RequestParam(value = "stdDatasetId") Integer stdDatasetId,
            @ApiParam(name = "adapterDatasetId", value = "适配数据集ID")
            @RequestParam(value = "adapterDatasetId") Integer adapterDatasetId) {
        return metadataService.deleteMultiple(adapterVersion, metadataId, stdDatasetId, adapterDatasetId);
    }

    @RequestMapping(value = "/isStrategy", 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\":\"" + MetadataStrategyExecute.isAdapt(version) + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

    /**
     * 智能匹配
     *
     * @param version
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/strategy", method = RequestMethod.POST)
    @ApiOperation(value = "智能匹配", response = Object.class, produces = "application/json", notes = "智能匹配")
    public Result strategy(
            @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 (MetadataStrategyExecute.class) {
                if (!MetadataStrategyExecute.isAdapt(version)) {
                    //判断之前是否已经有匹配有的话不进行匹配
                    MetadataStrategyExecute.setVersionYes(version);
                } else {
                    return actionResult;
                }
            }

            String schemaName = LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA);
            adapterDatasetService.strategy(schemaName,version, std_version, adapter_std_version);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
        return actionResult;
    }

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

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

