package com.yanzhu.manage.controller;

import com.yanzhu.common.core.utils.poi.ExcelUtil;
import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.common.core.web.page.TableDataInfo;
import com.yanzhu.common.log.annotation.Log;
import com.yanzhu.common.log.enums.BusinessType;
import com.yanzhu.common.security.annotation.RequiresPermissions;
import com.yanzhu.manage.domain.AttendanceUbiDevice;
import com.yanzhu.manage.domain.BusExamInfo;
import com.yanzhu.manage.service.IAttendanceUbiDeviceService;
import com.yanzhu.manage.service.IBusExamInfoService;
import com.yanzhu.manage.service.IUniService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 宇泛的设备信息Controller
 * 
 * @author yanzhu
 * @date 2024-10-08
 */
@RestController
@RequestMapping("/attendance_ubi_device")
public class AttendanceUbiDeviceController extends BaseController
{
    @Autowired
    private IAttendanceUbiDeviceService attendanceUbiDeviceService;

    @Autowired
    private IUniService uniService;
    /**
     * 查询宇泛的设备信息列表
     */
    @RequiresPermissions("manage:attendance_ubi_device:list")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceUbiDevice attendanceUbiDevice)
    {
        startPage();
        List<AttendanceUbiDevice> list = attendanceUbiDeviceService.selectAttendanceUbiDeviceList(attendanceUbiDevice);
        return getDataTable(list);
    }

    /**
     * 导出宇泛的设备信息列表
     */
    @RequiresPermissions("manage:attendance_ubi_device:export")
    @Log(title = "宇泛的设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceUbiDevice attendanceUbiDevice)
    {
        List<AttendanceUbiDevice> list = attendanceUbiDeviceService.selectAttendanceUbiDeviceList(attendanceUbiDevice);
        ExcelUtil<AttendanceUbiDevice> util = new ExcelUtil<AttendanceUbiDevice>(AttendanceUbiDevice.class);
        util.exportExcel(response, list, "宇泛的设备信息数据");
    }

    /**
     * 获取宇泛的设备信息详细信息
     */
    @RequiresPermissions("manage:attendance_ubi_device:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(attendanceUbiDeviceService.selectAttendanceUbiDeviceById(id));
    }

    /**
     * 新增宇泛的设备信息
     */
    @RequiresPermissions("manage:attendance_ubi_device:add")
    @Log(title = "宇泛的设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceUbiDevice attendanceUbiDevice)
    {
        AttendanceUbiDevice where=new AttendanceUbiDevice();
        where.setDeviceNo(attendanceUbiDevice.getDeviceNo());
        List<AttendanceUbiDevice> list=attendanceUbiDeviceService.selectAttendanceUbiDeviceList(where);
        if(list.size()>0){
            return  new AjaxResult(500,"此设备编号已存在!");
        }
        return toAjax(attendanceUbiDeviceService.insertAttendanceUbiDevice(attendanceUbiDevice));
    }

    /**
     * 修改宇泛的设备信息
     */
    @RequiresPermissions("manage:attendance_ubi_device:edit")
    @Log(title = "宇泛的设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceUbiDevice attendanceUbiDevice)
    {
        AttendanceUbiDevice where=new AttendanceUbiDevice();
        where.setDeviceNo(attendanceUbiDevice.getDeviceNo());
        List<AttendanceUbiDevice> list=attendanceUbiDeviceService.selectAttendanceUbiDeviceList(where);
        if(list.size()>1 || (list.size()==1 && !list.get(0).getId().equals(attendanceUbiDevice.getId()))){
            return  new AjaxResult(500,"此设备编号已存在!");
        }
        return toAjax(attendanceUbiDeviceService.updateAttendanceUbiDevice(attendanceUbiDevice));
    }

    /**
     * 删除宇泛的设备信息
     */
    @RequiresPermissions("manage:attendance_ubi_device:remove")
    @Log(title = "宇泛的设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(attendanceUbiDeviceService.deleteAttendanceUbiDeviceByIds(ids));
    }

    @PostMapping("/auth")
    @RequiresPermissions("manage:attendance_ubi_device:add")
    @Log(title = "宇泛的设备授权", businessType = BusinessType.UPDATE)
    public AjaxResult auth(@RequestBody AttendanceUbiDevice attendanceUbiDevice){
      long ret=  uniService.syncUniDeviceUser(attendanceUbiDevice.getId(),attendanceUbiDevice.getProjectId());
      if(ret==1){
          return AjaxResult.success();
      }else{
          return AjaxResult.error();
      }
    }
}
