package com.zd.smartlock.controller;

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

import com.zd.common.core.web.domain.ResultData;
import com.zd.smartlock.domain.SlUserRelation;
import com.zd.smartlock.domain.api.SlUser;
import com.zd.smartlock.utils.SmartlockUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.smartlock.service.ISlUserRelationService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 人员关联Controller
 *
 * @author xxf
 * @date 2022-09-02
 */
@RestController
@Api(tags = "【人员关联】")
@RequestMapping("/userRelation")
public class SlUserRelationController extends BaseController
{
    @Autowired
    private ISlUserRelationService slUserRelationService;

    /**
     * 查询人员关联列表
     */
    @PreAuthorize(hasPermi = "laboratory:relation:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询人员关联列表")
    public TableDataInfo<SlUserRelation> list(SlUserRelation slUserRelation)
    {
        startPage();
        List<SlUserRelation> list = slUserRelationService.selectSlUserRelationList(slUserRelation);
        return getDataTable(list);
    }

    /**
     * 导出人员关联列表
     */
    @ApiOperation(value = "导出人员关联列表")
    @PreAuthorize(hasPermi = "laboratory:relation:export")
    @Log(title = "人员关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlUserRelation slUserRelation) throws IOException
    {
        List<SlUserRelation> list = slUserRelationService.selectSlUserRelationList(slUserRelation);
        ExcelUtil<SlUserRelation> util = new ExcelUtil<SlUserRelation>(SlUserRelation.class);
        util.exportExcel(response, list, "人员关联数据");
    }

    /**
     * 获取人员关联详细信息
     */
    @ApiOperation(value = "获取人员关联详细信息")
    @PreAuthorize(hasPermi = "laboratory:relation:query")
    @GetMapping(value = "/{id}")
    public ResultData<SlUserRelation> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(slUserRelationService.selectSlUserRelationById(id));
    }

    /**
     * 新增人员关联
     */
    @ApiOperation(value = "新增人员关联")
    @PreAuthorize(hasPermi = "laboratory:relation:add")
    @Log(title = "人员关联", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody SlUserRelation slUserRelation)
    {
        return ResultData.result(slUserRelationService.insertSlUserRelation(slUserRelation));
    }

    /**
     * 修改人员关联
     */
    @ApiOperation(value = "修改人员关联")
    @PreAuthorize(hasPermi = "laboratory:relation:edit")
    @Log(title = "人员关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody SlUserRelation slUserRelation)
    {
        return  ResultData.result(slUserRelationService.updateSlUserRelation(slUserRelation));
    }

    /**
     * 删除人员关联
     */
    @ApiOperation(value = "删除人员关联")
    @PreAuthorize(hasPermi = "laboratory:relation:remove")
    @Log(title = "人员关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(slUserRelationService.deleteSlUserRelationByIds(ids));
    }

    /**
     * 查询智能锁人员列表
     */
    @GetMapping("/listLockUser")
    @ApiOperation(value = "查询智能锁人员列表")
    public TableDataInfo<SlUser> listLockUser(SlUser sluser)
    {
        List<SlUser> list = SmartlockUtil.getUser(sluser);
        return getDataTable(list);
    }
}
