package com.mx.cneeds.server.user.web;

import com.mx.cneeds.common.annotation.DataFilter;
import com.mx.cneeds.common.dto.DataFilterDto;
import com.mx.cneeds.common.dto.UserDto;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.validator.ValidatorUtils;
import com.mx.cneeds.common.vo.UserVo;
import com.mx.cneeds.server.entity.SysUserEntity;
import com.mx.cneeds.server.user.service.SysUserDeptService;
import com.mx.cneeds.server.user.service.SysUserFuncService;
import com.mx.cneeds.server.user.service.SysUserRoleService;
import com.mx.cneeds.server.user.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 用户信息表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-01-09 15:09:01
 */
@RestController
@RequestMapping("sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserDeptService sysUserDeptService;

    @Autowired
    private SysUserFuncService sysUserFuncService;




    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = sysUserService.queryPage(params);
        return page;
    }


    /**
     * 信息
     */
//    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Long userId){
        SysUserEntity sysUser = sysUserService.getById(userId);

        return R.ok().put("sysUser", sysUser);
    }

    @PostMapping("/userinfo")
    public UserDto infoByUsername(@RequestParam("username") String username){
        SysUserEntity sysUser = sysUserService.queryUserByUsername(username);
        Long roleId = sysUserRoleService.queryRoleId(sysUser.getUserId());
        UserDto userDto = new UserDto();
        BeanUtils.copyProperties(sysUser,userDto);
        userDto.setRoleId(roleId);
        userDto.setUserDeptIdList(sysUserDeptService.queryDeptIdList(sysUser.getUserId()));
        userDto.setUserFuncIdList(sysUserFuncService.queryFuncIdList(sysUser.getUserId()));
        userDto.setFuncPerms(sysUserService.getUserPerms(userDto.getUsername()));
        return userDto;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserDto dto){

        SysUserEntity sysUser = new SysUserEntity();

        BeanUtils.copyProperties(dto,sysUser);

        sysUserService.saveUser(sysUser, dto.getUserFuncIdList(), dto.getUserDeptIdList(), dto.getRoleId());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserDto dto){
        SysUserEntity sysUser = new SysUserEntity();

        BeanUtils.copyProperties(dto,sysUser);
        ValidatorUtils.validateEntity(sysUser);
        sysUserService.update(sysUser, dto.getUserFuncIdList(), dto.getUserDeptIdList());

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
        sysUserService.deleteBatch(userIds);

        return R.ok();
    }


    /**
     * TODO 暂时通过这里去实现
     * @param dataFilterDto
     * @return
     */
    @RequestMapping("/genDataFilter")
    public String genDataFilter(@RequestBody DataFilterDto dataFilterDto){
        return sysUserService.genDataFilter(dataFilterDto);
    }

}
