package com.fs.users.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.ResponseResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.store.domain.FsUser;
import com.fs.store.param.h5.FsUserPageListParam;
import com.fs.store.service.IFsUserService;
import com.fs.store.vo.FsCompanyUserListQueryVO;
import com.fs.store.vo.h5.FsUserPageListVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/users/user")
public class FsUserController extends BaseController
{
    @Autowired
    private IFsUserService fsUserService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('users:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUser fsUser)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUser.setCompanyId(loginUser.getCompany().getCompanyId());
        startPage();
        List<FsCompanyUserListQueryVO> list = fsUserService.selectFsCompanyUserListQuery(fsUser);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('users:user:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsUser fsUser)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUser.setCompanyId(loginUser.getCompany().getCompanyId());
        fsUser.setCompanyUserId(loginUser.getUser().getUserId());
        startPage();
        List<FsCompanyUserListQueryVO> list = fsUserService.selectFsCompanyUserListQuery(fsUser);
        return getDataTable(list);
    }

    /**
     * 获取用户详细信息
     */

    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(fsUserService.selectFsUserById(userId));
    }

    @GetMapping("/getUserList")
    public R getUserList( FsUser fsUser)
    {
        fsUser.setIsDel(0);
        List<FsUser> list=fsUserService.selectFsUserList(fsUser);
        return R.ok().put("data", list);
    }

    /**
     * 查询企业内的用户，每次只返回5条
     * @param fsUser 查询参数
     * @return R
     */
    @GetMapping("/getUserListLimit")
    public R getUserListLimit( FsUser fsUser)
    {
        fsUser.setIsDel(0);
        List<FsUser> list=fsUserService.getUserListLimit(fsUser);
        return R.ok().put("data", list);
    }

    /**
     * 新增用户
     */
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FsUser fsUser)
    {
        FsUser check=fsUserService.selectFsUserByPhone(fsUser.getPhone());
        if(check!=null){
            return R.error("手机号已存在");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fsUser.setCompanyId(loginUser.getCompany().getCompanyId());
        fsUser.setCompanyUserId(loginUser.getUser().getUserId());
        fsUser.setUsername(fsUser.getPhone());
        fsUserService.insertFsUser(fsUser);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('users:user:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUser fsUser)
    {
        return toAjax(fsUserService.updateFsUser(fsUser));
    }

    @PreAuthorize("@ss.hasPermi('users:user:darkRoomList')")
    @GetMapping("/darkRoomList")
    @ApiOperation("小黑屋")
    public TableDataInfo darkRoomList(FsUserPageListParam param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setUserId(loginUser.getUser().getUserId());
        startPage();
        List<FsUserPageListVO> list = fsUserService.selectFsUserPageList(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('users:user:enabledUsers')")
    @PostMapping("/enabledUsers")
    @ApiOperation("批量启用会员")
    public ResponseResult<Boolean> enabledUsers(@RequestBody String[] ids) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Integer count = fsUserService.selectFsUserByUserIds(ids, loginUser.getUser().getUserId());
        if(count > 0){
            return ResponseResult.fail(400, "重粉会员不能移除小黑屋");
        }

        Boolean r = fsUserService.disabledUser(ids, true);
        return ResponseResult.ok(r);
    }
}
