package com.iformall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iformall.common.IdWorker;
import com.iformall.domain.po.MallRolePermission;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.MallUserRole;
import com.iformall.enums.EnumMenuType;
import com.iformall.enums.EnumUserAdmin;
import com.iformall.mapper.MallRolePermissionMapper;
import com.iformall.mapper.MallUserRoleMapper;
import com.iformall.service.MallUserRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MallUserRoleServiceImpl implements MallUserRoleService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
    MallUserRoleMapper mallUserRoleMapper;

    @Autowired
    MallRolePermissionMapper mallRolePermissionMapper;


    @Override
    public PageInfo<MallUserRole> listAsPage(MallUserRole record, Integer pageIndex, Integer pageSize) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> mallUserRoleMapper.findList(record));
    }

    @Override
    public List<MallUserRole> getList(MallUserRole record) {
        return mallUserRoleMapper.findList(record);
    }

    @Override
    public MallUserRole getById(Long id) {
        return mallUserRoleMapper.selectById(id);
    }

    @Override
    public MallUserRole getByUid(Long uid) {
        try {
            MallUserRole q = new MallUserRole();
            q.setUid(uid);
            return mallUserRoleMapper.selectOne(new QueryWrapper(q));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    @Override
    public void saveOrUpdate(MallUserRole record) {
        if (record.getId() == null) {
            //record.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            final IdWorker idWorker = IdWorker.get();
        	record.setId(idWorker.nextId());
            mallUserRoleMapper.insert(record);
        } else {
            mallUserRoleMapper.updateById(record);
        }
    }

    @Override
    public void deleteById(Long id) {
        mallUserRoleMapper.deleteById(id);
    }

    @Override
    public int cntUserList(MallUserRole record) {
        return mallUserRoleMapper.selectCount(new QueryWrapper(record));
    }

	@Override
	public void deleteByUserId(Long uid) {
		mallUserRoleMapper.deleteByUserId(uid);
		
	}

	@Override
    public boolean checkIsMobileAdmin(MallUserInfo userInfo) {
        boolean isInMobile  = false;
        if(EnumUserAdmin.ADMIN.getCode().equals(userInfo.getIsAdmin())) { // 超管
            isInMobile = true;
            return isInMobile;
        }
        MallUserRole ur = new MallUserRole();
        ur.setUid(userInfo.getId());
        List<MallUserRole> userRoles = mallUserRoleMapper.findList(ur);
        if (userRoles.size() > 0) {
            MallUserRole userRole = userRoles.get(0);
            Long roleId = userRole.getRoleId();
            MallRolePermission p = new MallRolePermission();
            p.setRoleId(roleId);
            p.setTenantId(userInfo.getTenantId());
            List<MallRolePermission> rolePermissions = mallRolePermissionMapper.findList(p);
            for (MallRolePermission rp : rolePermissions) {
                logger.info("user permission id " + rp.getPermissionId());
                if (EnumMenuType.MOBILE_ADMIN.getCode().equals(rp.getPermissionId().intValue())) { /// 移动端数据塔台
                    isInMobile = true;
                    return isInMobile;
                }
            }
        }
        return isInMobile;
    }

    @Override
    public String getPermissionsByUser(MallUserInfo userInfo) {
        MallUserRole ur = new MallUserRole();
        ur.setUid(userInfo.getId());
        List<MallUserRole> userRoleList = mallUserRoleMapper.findList(ur);
        if (userRoleList.size() > 0) {
            MallUserRole userRole = userRoleList.get(0);
            Long roleId = userRole.getRoleId();
            MallRolePermission p = new MallRolePermission();
            p.setRoleId(roleId);
            p.setTenantId(userInfo.getTenantId());
            List<MallRolePermission> rolePermissionList = mallRolePermissionMapper.findList(p);
            String menus = "";
            for (MallRolePermission rp : rolePermissionList) {
                menus += rp.getPermissionId() + ",";
            }
            if (menus.length() > 0) {
                menus = menus.substring(0, menus.length() - 1);
            }
            return menus;
        }
        return null;
    }
    
    
    
    
}
