package com.securitycontrol.system.service;

import com.securitycontrol.system.api.domain.SysUser;
import com.securitycontrol.system.api.domain.decision.SysMenu;
import com.securitycontrol.system.api.model.BidProVo;
import com.securitycontrol.system.mapper.LoginMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 系统 用户
 *
 * @author HeiZi
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {


    @Autowired
    private LoginMapper mapper;

    /**
     * 查询用户信息
     *
     * @param username 用户名
     * @return
     */
    @Override
    public SysUser selectUserByUserName(String username) {
        return mapper.selectUserByUserName(username);
    }

    /**
     * 查询用户登录权限
     *
     * @param sysUser
     * @return
     */
    @Override
    public Set<String> getMenuPermission(SysUser sysUser) {
        return mapper.getMenuPermission(sysUser);
    }

    /**
     * 获取菜单列表的集合
     *
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> getAllMenuList(String userId) {
        List<SysMenu> parent = mapper.getAllMenuList(userId, "0");
        getChilder(parent, userId);
        return parent;
    }

    @Override
    public void updateUserLogin(String userId, String ip) {
        try {
            mapper.updateUserLogin(userId, ip);
        } catch (Exception e) {
            log.error(e.toString(), e);
        }
    }

    /**
     * 子菜单查询
     *
     * @param menuList
     */
    private void getChilder(List<SysMenu> menuList, String userId) {
        for (SysMenu menu : menuList) {
            List<SysMenu> list = mapper.getAllMenuList(userId, menu.getMenuId());
            if (list != null && list.size() > 0) {
                menu.setChilder(list);
                menu.setLast(false);
            } else {
                menu.setChilder(new ArrayList<SysMenu>());
                menu.setLast(true);
            }
        }
    }

    @Override
    public List<BidProVo> getUserBandingBidPro(String param,int type) {
        List<BidProVo> list = new ArrayList<>();
        try {
            list = mapper.getUserBandingBidPro(param,type);
        } catch (Exception e) {
            log.error("获取用户绑定标段工程",e);
        }
        return list;
    }
}
