package com.bonus.system.att.controller;

import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.common.log.annotation.Log;
import com.bonus.common.log.enums.BusinessType;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.system.att.entity.AttendanceMachineBean;
import com.bonus.system.att.service.AttendanceMachineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 考勤机
 *
 * @author zys
 */
@RestController
@RequestMapping("/attendanceMachine")
@Slf4j
public class AttendanceMachineController extends BaseController {

    @Resource(name = "attendanceMachineService")
    private AttendanceMachineService attendanceMachineService;

    /**
     * 获取考勤机列表
     */
    @RequiresPermissions("att:machine:list")
    @GetMapping("/list")
    @Log(title = "考勤设置->考勤机管理->列表查询", businessType = BusinessType.QUERY)
    public TableDataInfo list(AttendanceMachineBean bean) {
        try{
            startPage();
            return getDataTable(attendanceMachineService.selectAttendanceMachineList(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return getDataTableError(new ArrayList<>());
    }

    /**
     * 新增考勤机
     */
    @RequiresPermissions("att:machine:add")
    @PostMapping
    @Log(title = "考勤设置->考勤机管理->考勤机新增", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody AttendanceMachineBean bean) {
        try{
            return toAjax(attendanceMachineService.insertAttendanceMachine(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 根据考勤机编号获取详细信息
     */
    @RequiresPermissions("att:machine:query")
    @GetMapping(value = "/{machineId}")
    public AjaxResult getInfo(@PathVariable Long machineId) {
        try{
            return success(attendanceMachineService.selectAttendanceMachineById(machineId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 修改考勤机
     */
    @RequiresPermissions("att:machine:edit")
    @PutMapping
    @Log(title = "考勤设置->考勤机管理->考勤机修改", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody AttendanceMachineBean bean) {
        try{
            return toAjax(attendanceMachineService.updateAttendanceMachine(bean));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

    /**
     * 删除考勤机
     */
    @RequiresPermissions("att:machine:remove")
    @DeleteMapping("/{machineId}")
    @Log(title = "考勤设置->考勤机管理->考勤机删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable("machineId") Long machineId) {
        try{
            return toAjax(attendanceMachineService.deleteAttendanceMachine(machineId));
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return error("系统异常");
    }

}
