package com.ruoyi.web.work.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.work.domain.User;
import com.ruoyi.web.work.service.IContractService;
import com.ruoyi.web.work.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 用户管理
 * @author lsw
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/work/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    @Autowired
    private IContractService contractService;

    @PreAuthorize("@ss.hasPermi('work:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user){
        startPage();
        List<User> list = userService.selectList(user);
        return getDataTable(list);
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Long id){
        return AjaxResult.success(userService.getById(id));
    }

    @PreAuthorize("@ss.hasPermi('work:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody User user){
        return toAjax(userService.updateById(user));
    }

    @PreAuthorize("@ss.hasPermi('work:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(userService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping("/look")
    public void look(HttpServletResponse response,Long userId) throws IOException {
        contractService.look(response,userId);
    }
}
