package com.yihu.hos.resource.service;

import com.yihu.hos.resource.dao.RsResourceRestDaoImpl;
import com.yihu.hos.resource.dao.RsResourceRestDetailDaoImpl;
import com.yihu.hos.resource.model.RsResourceRest;
import com.yihu.hos.resource.model.RsResourceRestDetail;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/30.
 */
@Service("resourceRestService")
public class RsResourceRestServiceImpl {

    public static final String BEAN_ID = "resourceRestService";

    @Resource(name = RsResourceRestDaoImpl.BEAN_ID)
    private RsResourceRestDaoImpl resourceRestDao;
    @Resource(name = RsResourceRestDetailDaoImpl.BEAN_ID)
    private RsResourceRestDetailDaoImpl resourceRestDetailDao;

   
    public DataGridResult getResourceRestDetilList(Map<String, Object> conditionMap, Integer page, Integer rows) throws Exception {
        return resourceRestDetailDao.getResourceRestDetilList(conditionMap, page, rows);
    }

   
    public DataGridResult getResourceRestList(Map<String, Object> conditionMap, Integer page, Integer rows) throws Exception {
        return resourceRestDao.getResourceRestList(conditionMap, page, rows);
    }

   
    public RsResourceRest getResourceRestById(String id) throws Exception {
        return resourceRestDao.getEntity(RsResourceRest.class, id);
    }

   
    public RsResourceRestDetail getResourceRestDetail(String id) throws Exception {
        return resourceRestDetailDao.getEntity(RsResourceRestDetail.class, id);
    }

    @Transactional
    public Result deleteResourceRestById(String id) throws Exception {
        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("resourceRestId",id);
        if(resourceRestDetailDao.getResourceRestDetilList(conditionMap, 1, 10).getTotalCount()!=0)
        {
            return Result.error("该资源存在业务项");
        }
        RsResourceRest r = resourceRestDao.getEntity(RsResourceRest.class, id);
        //resourceRestDetailDao.get
        resourceRestDao.deleteEntity(r);
        return Result.success("删除成功");
    }

    @Transactional
    public Result deleteResourceRestDetail(String id) throws Exception {

        RsResourceRestDetail r = resourceRestDetailDao.getEntity(RsResourceRestDetail.class, id);
        resourceRestDetailDao.deleteEntity(r);
        return Result.success("删除成功");
    }

    @Transactional
    public Result addResourceRest(RsResourceRest rr) throws Exception {
        resourceRestDao.saveEntity(rr);
        return Result.success("新增成功");
    }

    @Transactional
    public Result updateResourceRest(RsResourceRest rr) throws Exception {
        RsResourceRest rrr = resourceRestDao.getEntity(RsResourceRest.class, rr.getId());
        rrr.setName(rr.getName());
        rrr.setRemark(rr.getRemark());
        rrr.setPassword(rr.getPassword());
        rrr.setUrl(rr.getUrl());
        rrr.setUsername(rr.getUsername());
        return Result.success("更新成功");
    }

    @Transactional
    public Result addResourceRestDetail(RsResourceRestDetail rrd) throws Exception {
        RsResourceRestDetail rrdTemp = resourceRestDetailDao.getResourceRestDetailByCodeAndResourceRestId(rrd.getCode(), rrd.getResourceRestId());
        if (rrdTemp != null) {
            return Result.error("业务资源项代码已经存在");
        }
        resourceRestDetailDao.saveEntity(rrd);
        return Result.success("新增成功");
    }

    @Transactional
    public Result updateResourceRestDetail(RsResourceRestDetail rrd) throws Exception {
        RsResourceRestDetail rrdTemp = resourceRestDetailDao.getResourceRestDetailByCodeAndResourceRestIdWithOutId(rrd.getCode(), rrd.getResourceRestId(),rrd.getId());
        if (rrdTemp != null) {
            return Result.error("业务资源项代码已经存在");
        }
        RsResourceRestDetail rrrd = resourceRestDetailDao.getEntity(RsResourceRestDetail.class, rrd.getId());
        rrrd.setName(rrd.getName());
        rrrd.setType(rrd.getType());
        rrrd.setCode(rrd.getCode());
        rrrd.setNamespace(rrd.getNamespace());
        rrrd.setParam(rrd.getParam());
        rrrd.setParamType(rrd.getParamType());
        rrrd.setResourceRestId(rrd.getResourceRestId());
        rrrd.setSignature(rrd.getSignature());
        rrrd.setReturnType(rrd.getReturnType());
        rrrd.setReturnValue(rrd.getReturnValue());
        return Result.success("更新成功");
    }

   
    public RsResourceRestDetail getResourceRestDetailByCode(String code) throws Exception {
        return resourceRestDetailDao.getResourceRestDetailByCode(code);
    }
}
