package com.mvc.service.dic;

import com.mvc.constant.BaseConstant;
import com.mvc.dao.FacUserRoleMapper;
import com.mvc.entity.FacUserRole;
import com.mvc.entity.FacUserRoleExample;
import com.mvc.model.FacUserRoleModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class FacUserRoleService {

    @Resource
    private FacUserRoleMapper facUserRoleMapper;

    public List<FacUserRoleModel> findAllFacUserRole(FacUserRoleModel queryModel) {
        FacUserRoleExample example = new FacUserRoleExample();
        FacUserRoleExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(queryModel.getRoleName())) {
            criteria.andRoleNameLike("%" + queryModel.getRoleName() + "%");
        }
        List<FacUserRole> FacUserRoles = facUserRoleMapper.selectByExample(example);
        List<FacUserRoleModel> resList = new ArrayList<>();
        for (FacUserRole FacUserRole : FacUserRoles) {
            FacUserRoleModel model = new FacUserRoleModel();
            BeanUtils.copyProperties(FacUserRole, model);
            resList.add(model);
        }
        return resList;
    }

    public FacUserRoleModel saveFacUserRole(FacUserRoleModel model) {
        FacUserRole FacUserRole = new FacUserRole();
        model.setRoleId(StringUtil.getUuid());
        model.setCreateTime(new Date());
        BeanUtils.copyProperties(model, FacUserRole);
        int insert = facUserRoleMapper.insert(FacUserRole);
        model.setId(FacUserRole.getId());
        return model;
    }

    public FacUserRoleModel updateFacUserRole(FacUserRoleModel model) {
        FacUserRole FacUserRole = facUserRoleMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, FacUserRole);
        int i = facUserRoleMapper.updateByPrimaryKey(FacUserRole);
        return model;
    }

    public int deleteFacUserRole(int id) {
        int i = facUserRoleMapper.deleteByPrimaryKey(id);
        return i;
    }

    public FacUserRoleModel findFacUserRoleModelByRoleId(String roleId) {
        FacUserRoleExample example = new FacUserRoleExample();
        FacUserRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List<FacUserRole> facUserRoles = facUserRoleMapper.selectByExample(example);
        if (facUserRoles.size() > 0) {
            FacUserRoleModel resModel = new FacUserRoleModel();
            BeanUtils.copyProperties(facUserRoles.get(0), resModel);
            return resModel;
        } else {
            return null;
        }
    }

    /**
     * 根据角色ID获取对应的动态路由表的ID
     * 暂时是固定值,还未建立对应关系
     *
     * @param roleId
     * @return
     */
    public List<Integer> findRouterByRoleId(String roleId) {
        FacUserRoleModel roleModel = findFacUserRoleModelByRoleId(roleId);
        List<Integer> routerIds = new ArrayList<>();
        routerIds.add(1);//首页,必须有
        routerIds.add(BaseConstant.CUSTOMIZ_ROUTER_ID);//我的常用菜单
        if (roleModel == null) return routerIds;

        if (roleModel.getAccAuth().equals("1")) {
            routerIds.add(2);
        }
        if (roleModel.getDataAuth().equals("1")) {
            routerIds.add(3);
        }
        if (roleModel.getStationAuth().equals("1")) {
            routerIds.add(4);
        }
        if (roleModel.getCheckAtuth().equals("1")) {
            routerIds.add(5);
            //此处一个联动权限,有拷机的,就有库房、文件、字典权限(算是临时处理,库中暂无如下三个权限字段) 2023-12-14 guozhao
            routerIds.add(11);
            routerIds.add(12);
            routerIds.add(13);
            routerIds.add(56);
            routerIds.add(57);
        }
        if (roleModel.getReviewAuth().equals("1")) {
            routerIds.add(6);
        }
        if (roleModel.getBuildAuth().equals("1")) {
            routerIds.add(7);
        }
        if (roleModel.getTaskAuth().equals("1")) {
            routerIds.add(8);
        }
        if (roleModel.getUserAuth().equals("1")) {
            routerIds.add(9);
        }
        if (roleModel.getCustomAuth().equals("1")) {
            routerIds.add(10);
        }

        routerIds.add(50);//远程升级
        routerIds.add(54);//日志管理
        routerIds.add(60);//生产管理
        routerIds.add(64);//生产管理

        return routerIds;
    }
}
