package com.zd.laboratory.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.constant.Constants;
import com.zd.common.core.utils.Assert;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.redis.service.RedisService;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.laboratory.domain.LabSubAccessCheckInLog;
import com.zd.laboratory.domain.LabSubjectAccessRecord;
import com.zd.laboratory.service.ILabSubAccessCheckLogService;
import com.zd.laboratory.service.ILabSubjectAccessRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhoupan
 * @Date: 2021/11/13/17:17
 * @Description:
 */
@Api(tags = {"签到检查记录管理"})
@ApiSupport(author = Knife4jConfiguration.Author.ZP)
@RestController
@RequestMapping("/checklog")
public class LabSubAccessCheckLogController {

    @Autowired
    ILabSubAccessCheckLogService labSubAccessCheckLogService;

    @Autowired
    private ILabSubjectAccessRecordService subjectAccessRecordService;

    @Autowired
    RedisService redisService;

    /**
     * 提交检查项结果
     * @param labSubAccessCheckInLog
     * @return
     */
    @PostMapping()
    @ApiOperation("提交检查项结果")
    public ResultData add(@RequestBody @Validated LabSubAccessCheckInLog labSubAccessCheckInLog)
    {
        //新增日志
        return ResultData.result(labSubAccessCheckLogService.insertLog(labSubAccessCheckInLog));
    }


    /**
     * 跳过检查项
     * @param id
     *
     * @return
     */
    @PostMapping("/{id}/jump/{code}")
    @ApiOperation("跳过检查")
    public ResultData jump(@PathVariable("id") @ApiParam("进出记录ID") Long id, @PathVariable("id") @ApiParam("检查项") String code) {
        Long cacheObject = redisService.getCacheObject(Constants.SINGIN_id_KEY + id);
        if (cacheObject == null) {
            return ResultData.fail(600, "签到&签出已超时，请重新刷卡重试！");
        }
        Assert.isNotNull(cacheObject, "签到超时！");
        LabSubAccessCheckInLog labSubAccessCheckInLog = new LabSubAccessCheckInLog();
        labSubAccessCheckInLog.setCode(code);
        labSubAccessCheckInLog.setResult(true);
        labSubAccessCheckInLog.setSignId(id);
        labSubAccessCheckInLog.setHasJump(true);

        LabSubjectAccessRecord accessRecord=new LabSubjectAccessRecord();
        accessRecord.setInitFlag(false);
        accessRecord.setId(id);
        int i= subjectAccessRecordService.updateLabSubjectAccessRecordById(accessRecord);

        //新增日志
        return ResultData.result(labSubAccessCheckLogService.insertLog(labSubAccessCheckInLog));
    }


}
