package com.zd.laboratory.service.impl;

import java.util.List;

import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.Assert;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.laboratory.domain.LabSubjectAccessRecord;
import com.zd.laboratory.domain.vo.LabSubjectAccessRecordVo;
import com.zd.laboratory.service.ILabSubjectAccessRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabPhotoInspectionMapper;
import com.zd.laboratory.domain.LabPhotoInspection;
import com.zd.laboratory.service.ILabPhotoInspectionService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 拍照检查附件Service业务层处理
 *
 * @author zd
 * @date 2021-08-31
 */
@Service
public class LabPhotoInspectionServiceImpl implements ILabPhotoInspectionService {
    @Autowired
    private LabPhotoInspectionMapper labPhotoInspectionMapper;

    @Autowired
    private ILabSubjectAccessRecordService labSubjectAccessRecordService;

    /**
     * 查询拍照检查附件
     *
     * @param id 拍照检查附件主键
     * @return 拍照检查附件
     */
    @Override
    public LabPhotoInspection selectLabPhotoInspectionById(Long id) {
        return labPhotoInspectionMapper.selectLabPhotoInspectionById(id);
    }

    /**
     * 查询拍照检查附件列表
     *
     * @param labPhotoInspection 拍照检查附件
     * @return 拍照检查附件
     */
    @Override
    @DataScope(deptAlias = "pi")
    public List<LabPhotoInspection> selectLabPhotoInspectionList(LabPhotoInspection labPhotoInspection) {
        return labPhotoInspectionMapper.selectLabPhotoInspectionList(labPhotoInspection);
    }

    /**
     * 新增拍照检查附件
     *
     * @param labPhotoInspection 拍照检查附件
     * @return 结果
     */
    @Override
    public int insertLabPhotoInspection(LabPhotoInspection labPhotoInspection) {
        labPhotoInspection.setCreateTime(DateUtils.getNowDate());
        return labPhotoInspectionMapper.insertLabPhotoInspection(labPhotoInspection);
    }

    /**
     * 修改拍照检查附件
     *
     * @param labPhotoInspection 拍照检查附件
     * @return 结果
     */
    @Override
    public int updateLabPhotoInspection(LabPhotoInspection labPhotoInspection) {
        labPhotoInspection.setUpdateTime(DateUtils.getNowDate());
        return labPhotoInspectionMapper.updateLabPhotoInspection(labPhotoInspection);
    }

    /**
     * 批量删除拍照检查附件
     *
     * @param ids 需要删除的拍照检查附件主键
     * @return 结果
     */
    @Override
    public int deleteLabPhotoInspectionByIds(Long[] ids) {
        return labPhotoInspectionMapper.deleteLabPhotoInspectionByIds(ids);
    }

    /**
     * 删除拍照检查附件信息
     *
     * @param id 拍照检查附件主键
     * @return 结果
     */
    @Override
    public int deleteLabPhotoInspectionById(Long id) {
        return labPhotoInspectionMapper.deleteLabPhotoInspectionById(id);
    }



    /**
     * 新增拍照检查附件
     *
     * @param photo 拍照检查附件
     * @return 结果
     */
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public int savaPhotoFj(LabPhotoInspection photo) {
        int flag = 0;
        //todo  这些验证查询在高并发环境下都有问题, 在当前用调度任务检查24小时违规打卡的时间差也会导致这里出现脏数据
        if(photo.getId()==null)
        {
            //保存，保存时获取记录ID
            LabSubjectAccessRecordVo sigInAndGet = labSubjectAccessRecordService.isSigInAndGet(SecurityUtils.getUserIdEx(),photo.getSubjectId());
            Assert.isNotNull(sigInAndGet,()->"您当前不存在正常的打卡记录！");
            photo.setId(sigInAndGet.getId());
            try {
               return insertLabPhotoInspection(photo);
            } catch (Exception e) {
                Assert.isFalse(e instanceof DuplicateKeyException,"您已提交拍照检查，不能重复提交！");
                throw e;
            }
        }
        else
        {
            //修改 时需要判断数据否是自己的
            LabSubjectAccessRecord subjectAccessRecord = labSubjectAccessRecordService.selectLabSubjectAccessRecordById(photo.getId());
            Assert.isNotNull(subjectAccessRecord,"进出记录不存在！");
            Assert.isTrue( subjectAccessRecord.getJoinUserId().equals(SecurityUtils.getUserId()),"进出记录已关闭，无法修改拍照检查！");
            Assert.isTrue( 1l==subjectAccessRecord.getAccessStatus(),"您无法修改他人的拍照检查记录！");
            return updateLabPhotoInspection(photo);

        }

    }
}
