package com.yihu.hos.resource.service;

import com.yihu.hos.resource.dao.RsCategoryDaoImpl;
import com.yihu.hos.resource.dao.RsResourceDaoImpl;
import com.yihu.hos.resource.model.RsResource;
import com.yihu.hos.resource.model.RsResourceCategory;
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;

@Service("categoryService")
public class RsCategoryServiceImpl  {

    public static final String BEAN_ID = "categoryService";


    @Resource(name = RsCategoryDaoImpl.BEAN_ID)
    private RsCategoryDaoImpl rsCategoryDao;

    @Resource(name = RsResourceDaoImpl.BEAN_ID)
    private RsResourceDaoImpl rsResourceDao;


    public Result getList(Map<String, Object> conditionMap) throws Exception {
        return rsCategoryDao.getList(conditionMap);
    }


    public RsResourceCategory getCategoryById(String id) throws Exception {
        return rsCategoryDao.getEntity(RsResourceCategory.class, id);
    }

    @Transactional
    public Result createCategory(RsResourceCategory rrc) throws Exception {
        RsResourceCategory rrcTemp = rsCategoryDao.getCategoryByNameAndPid(rrc.getName(), rrc.getPid());
       // rrc.setRemark(rrc.getRemark().replace("\n","</br>"));
        if (rrcTemp == null)
            rsCategoryDao.saveEntity(rrc);
        else
            return Result.error("分类名称重复");
        return Result.success("保存成功");
    }

    @Transactional
    public Result updateCategory(RsResourceCategory rrc) throws Exception {
        RsResourceCategory rrcTemp = rsCategoryDao.getCategoryByNameAndPidWithOutThis(rrc.getName(), rrc.getPid(), rrc.getId());
    //    rrc.setRemark(rrc.getRemark().replace("\n","</br>"));
        if (rrcTemp != null)
            return Result.error("分类名称重复");
        RsResourceCategory rc = rsCategoryDao.getEntity(RsResourceCategory.class, rrc.getId());
        rc.setName(rrc.getName());
        rc.setStatus(rrc.getStatus());
        rc.setPid(rrc.getPid());
        rc.setRemark(rrc.getRemark());
        return Result.success("更新成功");
    }

    /**
     * 删除资源分类，存在子节点或所属资源不允许删除
     */
    @Transactional
    public Result deleteCategory(String id) throws Exception {
        //判断是否存在子节点
        List<RsResourceCategory> categoryList = rsCategoryDao.getCategoryByPid(id);
        if(categoryList==null || categoryList.size()>0)
        {
            return Result.error("该分类包含子分类，不可直接删除！");
        }
        //判断是否存在资源
        List<RsResource> resourceList =  rsResourceDao.getAllRsResourceByCategory(id);
        if(resourceList==null || resourceList.size()>0)
        {
            return Result.error("该分类包含注册资源，不可直接删除！");
        }

        RsResourceCategory rc = rsCategoryDao.getEntity(RsResourceCategory.class, id);
        rsCategoryDao.deleteEntity(rc);
        return Result.success("删除成功");
    }


    public Result getCategoryWithOutId(String id) throws Exception {
        DataGridResult dataGridResult=new DataGridResult();
        List<RsResourceCategory> rs= rsCategoryDao.getCategoryWithOutId(id);
        dataGridResult.setDetailModelList(rs);
        return dataGridResult;
    }

    private List<RsResourceCategory> findChindren(String id, List<RsResourceCategory> allRsResourceCategorys) throws Exception {
        //得到子节点
        List<RsResourceCategory> rsResourceCategoryTemp = rsCategoryDao.getCategoryByPid(id);
        allRsResourceCategorys.addAll(rsResourceCategoryTemp);
        //判断子节点是否有孩子
        if (rsResourceCategoryTemp.size() != 0) {
            //有的话继续遍历
            for (int i = 0; i < rsResourceCategoryTemp.size(); i++) {
                findChindren(rsResourceCategoryTemp.get(i).getId(), allRsResourceCategorys);
            }
        } else {
            return rsResourceCategoryTemp;
        }
        return null;
    }
}
