package com.younike.system.service.impl;

import com.younike.common.core.domain.AjaxResult;
import com.younike.common.utils.bean.BeanUtils;
import com.younike.common.utils.id.IDUtils;
import com.younike.system.domain.AccessPasscodeVerifyRecord;
import com.younike.system.domain.vo.AccessPasscodeVO;
import com.younike.system.mapper.AccessPasscodeVerifyRecordMapper;
import com.younike.system.param.AccessPasscodeVerifyRecordParam;
import com.younike.system.param.AccessPasscodeVerifyRecordQueryParam;
import com.younike.system.service.IAccessPasscodeVerifyRecordService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 门岗通行证核查记录Service业务层处理
 *
 * @author dail
 * @date 2022-04-16
 */
@Service
@Log4j2
public class AccessPasscodeVerifyRecordServiceImpl implements IAccessPasscodeVerifyRecordService {

    @Autowired
    private AccessPasscodeVerifyRecordMapper accessPasscodeVerifyRecordMapper;


    /**
     * 保存门岗通行证核查记录
     *
     * @param accessPasscodeVerifyRecordParam
     * @return
     */
    @Override
    public AjaxResult saveAccessPasscodeVerifyRecord(AccessPasscodeVerifyRecordParam accessPasscodeVerifyRecordParam) {
        // 根据门岗通行证id查询是否核查过门岗通行证
        AccessPasscodeVerifyRecord accessPasscodeVerifyRecord = accessPasscodeVerifyRecordMapper.selectByAccessPasscodeId(accessPasscodeVerifyRecordParam.getAccessPasscodeId());
        if (accessPasscodeVerifyRecord != null) {
            return AjaxResult.error("此通行证已被使用过,不可重复使用！");
        }
        // 生成门岗通行证核查记录id
        Long accessPasscodeVerifyRecordId = IDUtils.nextLongId();
        // 保存门岗通行证核查记录
        AccessPasscodeVerifyRecord newAccessPasscodeVerifyRecord = new AccessPasscodeVerifyRecord();
        BeanUtils.copyProperties(accessPasscodeVerifyRecordParam, newAccessPasscodeVerifyRecord);
        newAccessPasscodeVerifyRecord.setAccessPasscodeVerifyRecordId(accessPasscodeVerifyRecordId);
        newAccessPasscodeVerifyRecord.setCreateBy("admin");
        newAccessPasscodeVerifyRecord.setUpdateBy("admin");
        int result = accessPasscodeVerifyRecordMapper.insertSelective(newAccessPasscodeVerifyRecord);
        if (result <= 0) {
            return AjaxResult.success("门岗通行证核查记录保存失败!");
        }

        return AjaxResult.success(newAccessPasscodeVerifyRecord);
    }

    /**
     * 根据ID查询门岗二维码信息
     *
     * @param accessPasscodeVerifyRecordId
     * @return
     */
    @Override
    public AccessPasscodeVerifyRecord getByAccessPasscodeVerifyRecordId(Long accessPasscodeVerifyRecordId) {
        return null;
    }

    /**
     * 根据门岗通行证id查询门岗通行证核查记录
     *
     * @param queryParam
     * @return
     */
    @Override
    public AccessPasscodeVerifyRecord getByAccessPasscodeId(AccessPasscodeVerifyRecordQueryParam queryParam) {
        return null;
    }

    /**
     * 查询门岗通行记录列表
     * @param accessPasscode
     * @return
     */
    @Override
    public List<AccessPasscodeVO> selectAccessPasscodeVoList(AccessPasscodeVO accessPasscode) {
        List<AccessPasscodeVO> accessPasscodeVOS = accessPasscodeVerifyRecordMapper.selectAccessPasscodeVoList(accessPasscode);
        for (AccessPasscodeVO vo:accessPasscodeVOS) {
            if("003".equals(vo.getOrderType())){
                vo.setAppointmentStartTime(vo.getAppointmentTime());
            }
        }

        return accessPasscodeVOS;
    }
}
