package com.zd.laboratory.controller;

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

import com.zd.common.core.domain.R;
import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.web.domain.ResultData;
import com.zd.laboratory.domain.vo.LabSubjectAccessRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.laboratory.domain.LabSubjectAccessRecord;
import com.zd.laboratory.service.ILabSubjectAccessRecordService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.page.TableDataInfo;

/**
 * 实验室进出记录Controller
 *
 * @author zd
 * @date 2021-08-30
 */
@RestController
@RequestMapping("/record")
public class LabSubjectAccessRecordController extends BaseController {
    @Autowired
    private ILabSubjectAccessRecordService labSubjectAccessRecordService;

    /**
     * 查询实验室进出记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_RECORD + PerFun.LIST)
    @GetMapping("/list")
    public TableDataInfo list(LabSubjectAccessRecordVo labSubjectAccessRecord) {
        startPage();
        List<LabSubjectAccessRecordVo> list = labSubjectAccessRecordService.selectLabSubjectAccessRecordList(labSubjectAccessRecord);
        return getDataTable(list);
    }

    /**
     * 查询my的实验室进出记录列表
     */
    @GetMapping("/list/my")
    public TableDataInfo myList(LabSubjectAccessRecordVo labSubjectAccessRecord) {
        startPage();
        labSubjectAccessRecord.setJoinUserId(SecurityUtils.getUserId());
        List<LabSubjectAccessRecordVo> list = labSubjectAccessRecordService.selectMyAccessRecordList(labSubjectAccessRecord);
        return getDataTable(list);
    }


    /**
     * 导出实验室进出记录列表
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_RECORD + PerFun.EXPORT)
    @Log(title = "实验室进出记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabSubjectAccessRecordVo labSubjectAccessRecord) throws IOException {
        List<LabSubjectAccessRecordVo> list = labSubjectAccessRecordService.selectLabSubjectAccessRecordList(labSubjectAccessRecord);
        ExcelUtil<LabSubjectAccessRecordVo> util = new ExcelUtil<LabSubjectAccessRecordVo>(LabSubjectAccessRecordVo.class);
        util.exportExcel(response, list, "实验室进出记录数据");
    }

    /**
     * 获取实验室进出记录详细信息
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_RECORD + PerFun.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(labSubjectAccessRecordService.selectLabSubjectAccessRecordById(id));
    }

    /**
     * 新增实验室进出记录
     */
    @Log(title = "实验室进出记录", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody LabSubjectAccessRecord labSubjectAccessRecord) {
        return ResultData.result(labSubjectAccessRecordService.insertLabSubjectAccessRecord(labSubjectAccessRecord));
    }

    @GetMapping("/addRecords")
    public R addRecord(@RequestParam Map map){
           return labSubjectAccessRecordService.addRecord(map);
    }

    /**
     * 修改实验室进出记录
     */
    @Log(title = "实验室进出记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabSubjectAccessRecord labSubjectAccessRecord) {
        return toAjax(labSubjectAccessRecordService.updateLabSubjectAccessRecord(labSubjectAccessRecord));
    }

    /**
     * 删除实验室进出记录
     */
    @Log(title = "实验室进出记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(labSubjectAccessRecordService.deleteLabSubjectAccessRecordByIds(ids));
    }


    /**
     * 统计人员出入实验室次数(以日为单位)。
     */
    @GetMapping("/labInoutTime")
    public TableDataInfo labInoutTime(LabSubjectAccessRecordVo labSubjectAccessRecordVo) {
        startPage();
        List<LabSubjectAccessRecordVo> list = labSubjectAccessRecordService.labInoutTime(labSubjectAccessRecordVo);
        return getDataTable(list);
    }


    /**
     * 查询详细记录
     */
    @GetMapping("/myAccessDetail/{id}")
    public AjaxResult myAccessDetail(@PathVariable("id") Long id) {
        return AjaxResult.success(labSubjectAccessRecordService.myAccessDetail(id));
    }


    /**
     * 定时器定时查询离开时间超过24小时，记违规
     */
    // @Log(title = "定时器定时查询离开时间超过24小时，记违规", businessType = BusinessType.INSERT)
    @PutMapping("/outTimeRecord")
    public AjaxResult outTimeRecord() {
        return toAjax(labSubjectAccessRecordService.outTimeRecord());
    }

    /**
     * 离开实验室未打卡，再次进入其他实验室打卡，记违规
     */
    @Log(title = "离开实验室未打卡，再次进入其他实验室打卡，记违规", businessType = BusinessType.INSERT)
    @PutMapping("/leaveAgainSign")
    public ResultData leaveAgainSign(LabSubjectAccessRecordVo labSubjectAccessRecord) {
        return ResultData.result(labSubjectAccessRecordService.leaveAgainSign(labSubjectAccessRecord));
    }

}
