package com.fs.store.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.store.vo.FsUserListVO;
import com.fs.store.vo.FsUserVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.store.domain.FsUser;
import com.fs.store.service.IFsUserService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 用户Controller
 *
 * @author fs
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/store/user")
public class FsUserController extends BaseController
{
    @Autowired
    private IFsUserService fsUserService;

    /**
     * 查询用户列表
     */
    @PreAuthorize("@ss.hasPermi('store:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUser fsUser)
    {
        startPage();
        List<FsUserListVO> list = fsUserService.selectFsUserVOList(fsUser);
        for (FsUserListVO vo : list){
            vo.setPhone(ParseUtils.parsePhone(vo.getPhone()));
            vo.setSpreadPhone(ParseUtils.parsePhone(vo.getSpreadPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('store:user:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUser fsUser)
    {
        List<FsUser> list = fsUserService.selectFsUserList(fsUser);
        for (FsUser vo : list){
            vo.setPhone(ParseUtils.parsePhone(vo.getPhone()));
        }
        ExcelUtil<FsUser> util = new ExcelUtil<FsUser>(FsUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(fsUserService.selectFsUserById(userId));
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('store:user:query')")
    @GetMapping(value = "/queryvo/{userId}")
    public AjaxResult getUserInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(fsUserService.selectFsUserByUserId(userId));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('store:user:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody FsUser fsUser)
    {
        FsUser check=fsUserService.selectFsUserByPhone(fsUser.getPhone());
        if(check!=null){
            return R.error("手机号已存在");
        }
        fsUserService.insertFsUser(fsUser);
        return R.ok();
    }

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

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('store:user:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(fsUserService.deleteFsUserByIds(userIds));
    }


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