package com.usoftchina.bi.server.service.dashboard;

import com.usoftchina.bi.core.base.BaseContextHolder;
import com.usoftchina.bi.core.base.RepCode;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.core.base.RoleConstant;
import com.usoftchina.bi.core.exception.MyException;
import com.usoftchina.bi.core.utils.GetTokenDataUtil;
import com.usoftchina.bi.server.dao.dashboard.DashBoardMenuMapper;
import com.usoftchina.bi.server.model.vo.configVo.DashBoardMenuInfo;
import com.usoftchina.bi.server.model.vo.configVo.DashBoardUpdateMenuInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardMenuRespDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author chenwei
 * @Date 2019-04-19
 */
@Service
public class DashBoardMenuService {

    @Autowired
    private DashBoardMenuMapper dashBoardMenuMapper;

    public List<DashboardMenuRespDTO> list(String token){
        String role = BaseContextHolder.getRole();
        return getData(BaseContextHolder.getUserId(), 0, role);
    }

    private List<DashboardMenuRespDTO> getData(int userId, int id, String role){
        List<DashboardMenuRespDTO> dashboardMenuRespDTOList = new ArrayList<>();
        if (!RoleConstant.SUPER_ADMIN.getRole().equals(role)) {
            dashboardMenuRespDTOList = dashBoardMenuMapper.getMenuById(userId, id);
        }else {
            dashboardMenuRespDTOList = dashBoardMenuMapper.getMenuWithNoLimit(id);
        }
        if (!CollectionUtils.isEmpty(dashboardMenuRespDTOList)) {
            for (DashboardMenuRespDTO dashboardMenuRespDTO : dashboardMenuRespDTOList) {
                if ("menu".equals(dashboardMenuRespDTO.getType())) {
                    dashboardMenuRespDTO.setChildren(getData(userId, dashboardMenuRespDTO.getId(), role));
                }
            }
        }
        return dashboardMenuRespDTOList;
    }

    public int save(DashBoardMenuInfo dashBoardMenuInfo){
        dashBoardMenuMapper.insert(dashBoardMenuInfo);
        return dashBoardMenuInfo.getId();
    }

    public void update(DashBoardMenuInfo dashBoardMenuInfo){
        dashBoardMenuMapper.updateSelectiveByPrimaryKey(dashBoardMenuInfo);
    }

    public void deleteById(Long id){
        Assert.state(id > 0, "参数错误");
        // 判断是否存在报表,存在不允许删除目录
        int count = dashBoardMenuMapper.countDashboardByMenuId(id);
        if (count > 0) {
            throw new MyException(RepCode.MENU_USED_DASHBOARD);
        }
        dashBoardMenuMapper.deleteByPrimaryKey(id);
    }

    public void updateDashBoardMenu(DashBoardUpdateMenuInfo dashBoardUpdateMenuInfo){
        dashBoardMenuMapper.updateDashBoardMenu(dashBoardUpdateMenuInfo);
    }

    public RepEntity getDashBoardByMenuId(String token, int menuId){
        int userId = BaseContextHolder.getUserId();
        String role = BaseContextHolder.getRole();
        if (!RoleConstant.SUPER_ADMIN.getRole().equals(role)) {
            return new RepEntity(RepCode.success, dashBoardMenuMapper.getDashBoardByMenuId(userId, menuId));
        }else {
            return new RepEntity(RepCode.success, dashBoardMenuMapper.getDashBoardByMenuIdWithoutLimit(menuId));
        }
    }

}
