package com.securitycontrol.system.controller;

import com.securitycontrol.common.core.constant.Constant;
import com.securitycontrol.common.core.domain.Result;
import com.securitycontrol.common.core.utils.ServletUtils;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.ip.IpUtils;
import com.securitycontrol.common.security.annotation.InnerAuth;
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.api.model.LoginUser;
import com.securitycontrol.system.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 系统用户登录
 * 业务控制中心
 *
 * @author HeiZi
 */
@RestController
@RequestMapping(value = "/userLogin/")
public class LoginController {


    @Autowired
    private LoginService service;
    private static final String ZIOE = "0";
    private static final int ONE = 1;

    /**
     * 系统用户登录获取用户信息
     */
    @InnerAuth
    @GetMapping("login/{username}")
    public Result<LoginUser> login(@PathVariable("username") String username) {
        SysUser sysUser = service.selectUserByUserName(username);
        if (StringUtils.isNull(sysUser)) {
            return Result.fail("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = new HashSet<String>();
        roles.add(sysUser.getRoleId().toString());
        // 权限集合
        Set<String> permissions = service.getMenuPermission(sysUser);
        // 用户绑定标段工程
        String roleCode = sysUser.getRoleCode();
        List<BidProVo> bidProVoList = null;
        if(Objects.equals(roleCode, Constant.ADMINISTRATORS)){
            // 管理员
            bidProVoList = service.getUserBandingBidPro(null,1);
        }else if(Objects.equals(roleCode, Constant.MANAGER)){
            // 施工项目经理
            bidProVoList = service.getUserBandingBidPro(String.valueOf(sysUser.getUserId()),2);
        }else if(Objects.equals(roleCode, Constant.CITY)){
            // 地市值班员
            bidProVoList = service.getUserBandingBidPro(sysUser.getOrgId(),3);
        }
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSysUser(sysUser);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        sysUser.setBidProVoList(bidProVoList);
        return Result.ok(sysUserVo);
    }

    /**
     * 获取用户当前菜单数据
     *
     * @param userId
     * @return
     */
    @PostMapping("getAllMenuList")
    public Result<List<SysMenu>> getAllMenuList(@RequestBody String userId) {
        List<SysMenu> list = new ArrayList<SysMenu>();
        list = service.getAllMenuList(userId);
        return Result.ok(list);
    }

    /**
     * 获取用户当前菜单数据
     *
     * @param userId
     * @return
     */
    @PostMapping("updateUserLogin")
    public void updateUserLogin(@RequestBody String userId) {
        String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
        service.updateUserLogin(userId, ip);
    }


}
