package com.dcsoft.system.visitor.controller;

import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.core.web.controller.BaseController;
import com.dcsoft.common.core.web.domain.AjaxResult;
import com.dcsoft.common.core.web.page.TableDataInfo;
import com.dcsoft.common.log.annotation.Log;
import com.dcsoft.common.log.enums.BusinessType;
import com.dcsoft.system.visitor.domain.VisExitOutRecordVo;
import com.dcsoft.system.visitor.service.IVisExitOutRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/exitRecord")
public class VisExitOutRecordController extends BaseController {

    @Resource
    private IVisExitOutRecordService visExitOutRecordService;

    /**
     * 查询出门记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VisExitOutRecordVo vo) {
        startPage();
        List<VisExitOutRecordVo> list = visExitOutRecordService.selectList(vo);
        return getDataTable(list);
    }

    /**
     * 获取出门记录详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        VisExitOutRecordVo stuffVo = visExitOutRecordService.selectExitOutRecordById(id);
        return success(stuffVo);
    }

    /**
     * 新增出门证记录信息
     */
    @Log(title = "新增出门记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody VisExitOutRecordVo vo) {
        log.info("新增出门记录信息：{}", vo);
        try {
            //新增出门申请
            visExitOutRecordService.insertExitOutRecord(vo);

        } catch (Exception e) {
            log.error("新增出门记录异常：", e);
            throw new ServiceException("系统异常");
        }
        return AjaxResult.success();
    }

    /**
     * 扫码确认放行
     */
    @Log(title = "扫码确认放行", businessType = BusinessType.INSERT)
    @PostMapping("/confirmPass")
    @Transactional
    public AjaxResult confirmPass(@RequestBody VisExitOutRecordVo vo) {
        log.info("扫码确认放行入参：{}", vo);
        try {
            //新增出门申请
            visExitOutRecordService.confirmPass(vo);

        } catch (Exception e) {
            log.error("扫码确认放行异常：", e);
            throw new ServiceException("系统异常");
        }
        return AjaxResult.success();
    }

    /**
     * 删除出门证记录信息
     */
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids){
        log.info("删除出门记录信息：{}", ids);
        try {
            //新增出门申请
            visExitOutRecordService.delete(ids);

        } catch (Exception e) {
            log.error("新增出门记录异常：", e);
            throw new ServiceException("系统异常");
        }
        return AjaxResult.success();
    }

}
