package com.yihu.hos.system.service;

import com.yihu.hos.core.file.FileUtil;
import com.yihu.hos.system.dao.intf.IAppDao;
import com.yihu.hos.system.dao.intf.IAppServiceDao;
import com.yihu.hos.system.model.SystemApp;
import com.yihu.hos.system.model.SystemServiceEndpoint;
import com.yihu.hos.system.service.intf.IAppManager;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/21.
 */

@Service("appManager")
public class AppManager implements IAppManager {
    @Resource(name = "appDao")
    private IAppDao appDao;

    @Resource(name = "appServiceDao")
    private IAppServiceDao appServiceDao;

    @Override
    public Result getAppList(Map<String, Object> params) throws Exception {
        return appDao.getAppList(params);
    }

    @Override
    public SystemApp getAppById(String id) throws Exception {
        return appDao.getEntity(SystemApp.class,id);
    }

    @Transactional
    public Result addApp(SystemApp obj) throws Exception {
        appDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateApp(SystemApp obj) throws Exception {
        SystemApp systemApp = appDao.getEntity(SystemApp.class, obj.getId());
        systemApp.setName(obj.getName());
        systemApp.setCode(obj.getCode());
        systemApp.setAppKey(obj.getAppKey());
        systemApp.setAppSecret(obj.getAppSecret());
        systemApp.setAppUrl(obj.getAppUrl());
        systemApp.setDeveloper(obj.getDeveloper());
        systemApp.setIcon(obj.getIcon());
        systemApp.setStatus(obj.getStatus());
        return Result.success("更新成功");
    }

    @Transactional
    public Result deleteApp(String id) throws Exception {
        SystemApp systemApp = appDao.getEntity(SystemApp.class, id);
        appDao.deleteEntity(systemApp);
        return Result.success("删除成功");
    }




    /* ==============================服务模块=================================  */


    @Override
    public Result getAppServiceList(Map<String, Object> params) throws Exception {
        return appServiceDao.getAppServiceList(params);
    }

    @Override
    public SystemServiceEndpoint getAppServiceById(String id) throws Exception {
        SystemServiceEndpoint serviceEndpoint = appServiceDao.getEntity(SystemServiceEndpoint.class,id);
        serviceEndpoint.setRequesModule(serviceEndpoint.getRequesModule().replaceAll("\n", "<br>"));
        serviceEndpoint.setResponeResult(serviceEndpoint.getResponeResult().replaceAll("\n", "<br>"));
        serviceEndpoint.setResponeError(serviceEndpoint.getResponeError().replaceAll("\n", "<br>"));

        return serviceEndpoint;
    }

    @Transactional
    public Result addAppService(SystemServiceEndpoint obj) throws Exception {
        appServiceDao.saveEntity(obj);
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateAppService(SystemServiceEndpoint obj) throws Exception {
        SystemServiceEndpoint systemApp = appServiceDao.getEntity(SystemServiceEndpoint.class, obj.getId());
        systemApp.setName(obj.getName());
        systemApp.setCode(obj.getCode());
        systemApp.setValid(obj.getValid());
        systemApp.setVersion(obj.getVersion());
        systemApp.setDescription(obj.getDescription());
        systemApp.setEndpoint(obj.getEndpoint());
        systemApp.setRequesModule(obj.getRequesModule());
        systemApp.setRequestProtocol(obj.getRequestProtocol());
        systemApp.setResponeError(obj.getResponeError());
        systemApp.setResponeResult(obj.getResponeResult());
        systemApp.setRequestMethod(obj.getRequestMethod());
        systemApp.setRequestParameterList(obj.getRequestParameterList());
        systemApp.setHealthEndpoint(obj.getHealthEndpoint());
        systemApp.setHealthReportType(obj.getHealthReportType());
        systemApp.setMetricsEndpoint(obj.getMetricsEndpoint());
        systemApp.setMetricsReportType(obj.getMetricsReportType());
        systemApp.setAppId(obj.getAppId());
        systemApp.setRequestFormat(obj.getRequestFormat());
        return Result.success("更新成功");
    }

    @Transactional
    public Result deleteAppService(String id) throws Exception {
        SystemServiceEndpoint systemApp = appServiceDao.getEntity(SystemServiceEndpoint.class, id);
        appServiceDao.deleteEntity(systemApp);
        return Result.success("删除成功");
    }


    @Override
    public String uploadFile(MultipartFile file, String baseSavePath) {
        String fileName =  file.getOriginalFilename();
        boolean succ = false;
        try {
            succ = FileUtil.writeFile(baseSavePath +"/"+ fileName, file.getBytes(), "utf-8");
            if (succ){
                return  fileName;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
