package com.yihu.hos.system.service;

import com.yihu.hos.common.Services;
import com.yihu.hos.system.dao.DatasourceDao;
import com.yihu.hos.system.dao.OrganizationDao;
import com.yihu.hos.system.model.SystemOrganization;
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.List;
import java.util.Map;

/**
 * Created by hzp on 2016/1/11.
 */
@Service(Services.Organization)
public class OrganizationManager {

    public static final String BEAN_ID = Services.Organization;


    @Resource(name = OrganizationDao.BEAN_ID)
    private OrganizationDao organizationDao;

    @Resource(name = DatasourceDao.BEAN_ID)
    private DatasourceDao datasourceDao;

    /**
     * 根据ID获取组织详细
     * @return
     */
  
    public SystemOrganization getOrgById(String id) throws Exception
    {
        return organizationDao.getEntity(SystemOrganization.class,id);
    }

    /**
     * 获取组织列表
     */
  
    public DataGridResult getOrgList(Map<String, Object> conditionMap, Integer page, Integer rows) throws Exception
    {
        return organizationDao.getOrgList(conditionMap,page,rows);
    }

    /**
     * 新增组织
     */
  
    @Transactional
    public Result addOrg(SystemOrganization obj) throws Exception {
        String code= obj.getCode() ;
        String name=obj.getFullName();
        List org =organizationDao.getOrgByCodeOrName(code, name);
        if (org != null && org.size()>0) {
            return Result.error("机构已经存在了！");
        }else {

            organizationDao.saveEntity(obj);
            return Result.success("新增成功！");
        }
    }
    /**
     * 修改组织
     */
  
    @Transactional
    public Result updateOrg(SystemOrganization obj) {

        try{
            String code= obj.getCode() ;
            String name=obj.getFullName();
            List<SystemOrganization> org =organizationDao.getOrgByCodeOrName(code, name);
            if(org!=null) {
                for (int i = 0; i < org.size(); i++) {
                    if (!org.get(i).getId().equals(obj.getId())) {
                        return Result.error("机构已存在！");
                    }
                }
            }
            organizationDao.saveOrUpdateEntity(obj);
            return Result.success("修改成功！");

        }
        catch (Exception ex)
        {
            return Result.error("修改失败！");
        }
    }

    /**
     * 删除组织
     */
  
    @Transactional
    public Result deleteOrg(String id) {
        try{
            //判断是否有存在机构数据源
            DataGridResult list = datasourceDao.getDatasourceByOrg(id);
            if(list.getTotalCount()>0)
            {
                return Result.error("该机构关联数据库，无法删除！");
            }
            else{
                organizationDao.deleteEntity(SystemOrganization.class,id);
                return Result.success("删除成功！");
            }

        }
        catch (Exception ex)
        {
            return Result.error("删除失败！");
        }
    }

  
    public SystemOrganization getOrgByIde(String orgId) throws Exception {
        return organizationDao.getOrgByIde(orgId);
    }

    /**
     * 修改组织状态
     * @param orgId
     * @param activityFlag
     * @return
     * @throws Exception
     *//*
    public ActionResult activityOrg(String orgId,String activityFlag)
    {
        try {
            organizationDao.activityOrg(orgId, activityFlag);
            return new ActionResult(true,"状态修改成功！");
        }
        catch (Exception ex)
        {
            return new ActionResult(false,"状态修改失败！");
        }
    }*/
}
