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.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.service.IFsUserService;
import com.fs.store.vo.FsCompanyUserListQueryVO;
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);
    }





    /**
     * 新增用户
     */
    @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));
    }

}
