package com.winsun.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.SysUser;
import com.winsun.interfaces.PermissionVerification;
import com.winsun.item.core.common.constant.factory.PageFactory;
import com.winsun.item.core.shiro.ShiroKit;
import com.winsun.item.modular.system.service.IUserService;
import com.winsun.item.modular.system.warpper.UserWarpper;
import com.winsun.mapper.SysUserMapper;
import com.winsun.service.ISysUserService;
import com.winsun.utils.DesensitizationUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/mgr")
public class MgrController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IUserService userService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @PermissionVerification(value = "/system/user")
    @Permission(menuname = "管理员配置查询", value = "hhrUserList", method = RequestMethod.POST)
    public Object list(@RequestParam Map<String, Object> map) {
        log.info("用户管理列表查询已启动");
        Page<SysUser> page = (new PageFactory()).defaultPage();
        List<Map<String, Object>> users = null;
        ShiroUser user = ShiroKit.getUser();
        Object subType = map.get("subType");
        Object phone = map.get("phone");
        Object name = map.get("name");
        if (subType != null) {
            String[] subTypeList = subType.toString().split(",");
            map.put("subType", Arrays.asList(subTypeList));
        }

        if (phone != null) {
            map.put("phone", phone);
        }

        if (name != null) {
            map.put("name", name);
        }

        List<Map<String, Object>> maps = null;
        if (ShiroKit.isAdmin()) {
            maps = sysUserMapper.selectUsers(page,map);
        } else {
            map.put("deptid", user.getDeptId());
            maps = sysUserMapper.selectUsers(page,map);
        }

        for (Map<String, Object> userMap : maps) {
            if (StringUtils.isNotBlank(userMap.get("phone").toString())) {
                userMap.put("phone", DesensitizationUtil.desensitizedPhoneNumber(userMap.get("phone").toString()));
            }
            if (StringUtils.isNotBlank(userMap.get("account").toString())) {
                userMap.put("account", DesensitizationUtil.desensitizedPhoneNumber(userMap.get("account").toString()));
            }
        }
        page.setRecords((List) (new UserWarpper(maps)).warp());
        return super.packForBT(page);
    }
}
