package com.pannk.demo.controller;

import com.pannk.demo.entity.SysUserEntity;
import com.pannk.demo.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * Created by wolf on 20-11-9.
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(sysUserService.list());
    }

    @GetMapping("/listByName")
    public Result listByName(String userName) {
        return Result.success(sysUserService.listByName(userName));
    }

    @GetMapping("/listByCond")
    public Result listByName(@RequestBody Map<String,Object> cond) {
        return Result.success(sysUserService.listByCond(cond));
    }

    /**
     * @param sysUserEntity
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody SysUserEntity sysUserEntity) {
        sysUserEntity = sysUserService.save(sysUserEntity);
        return Result.success(sysUserEntity);
    }



    /**
     * 更新
     *
     * @param sysUserEntity
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody SysUserEntity sysUserEntity) {
        sysUserService.update(sysUserEntity);
        return Result.success(sysUserEntity);
    }


    /**
     * 删除成功
     *
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Long id) {
        sysUserService.delete(id);
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param pageSize
     * @param currPage
     * @return
     */
    @GetMapping("/page")
    public Result page(int pageSize, int currPage) {
        Page page = new Page(pageSize, currPage);
        page = sysUserService.page(page);
        return Result.success(page);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Long> ids) {
        sysUserService.batchDelete(ids);
        return Result.success();
    }
}
