package com.dcsoft.system.visitor.service.impl;

import com.dcsoft.common.core.constant.Constants;
import com.dcsoft.common.core.enums.ExamineEnum;
import com.dcsoft.common.core.exception.ServiceException;
import com.dcsoft.common.security.utils.SecurityUtils;
import com.dcsoft.system.api.model.LoginUser;
import com.dcsoft.system.visitor.domain.*;
import com.dcsoft.system.visitor.mapper.VisCarryStuffMapper;
import com.dcsoft.system.visitor.mapper.VisitorMapper;
import com.dcsoft.system.visitor.service.IVisCarryStuffService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class VisCarryStuffImpl implements IVisCarryStuffService {

    @Resource
    private VisCarryStuffMapper visCarryStuffMapper;

    @Override
    public List<VisExitOutVo> selectList(VisExitOutVo vo) {
        return visCarryStuffMapper.selectList(vo);
    }

    @Override
    public void insertVisExitOut(VisExitOutVo vo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        vo.setCreateBy(loginUser.getUsername());
        vo.setUserId(loginUser.getUserid());
        vo.setExamineState(ExamineEnum.IN_REVIEW.getCode());
        visCarryStuffMapper.insertVisExitOut(vo);
    }

    @Override
    public void insertVisStuffInfo(List<VisStuffInfoVo> visStuffInfoList) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        visStuffInfoList.forEach(vo -> vo.setCreateBy(loginUser.getUsername()));
        visCarryStuffMapper.insertVisStuffInfo(visStuffInfoList);
    }

    @Override
    public List<VisStuffInfoVo> selectStuffList(VisStuffInfoVo visStuffInfoVo) {
        return visCarryStuffMapper.selectStuffList(visStuffInfoVo);
    }

    @Override
    public List<VisStuffInfoExportVo> selectVisitorList(VisExitOutVo vo) {
        return visCarryStuffMapper.selectVisitorList(vo);
    }

    @Override
    public void saveVisitorReviewProcessList(List<VisExitOutVo> list) {
        visCarryStuffMapper.saveVisitorReviewProcessList(list);
    }

    @Override
    public VisitorReviewProcessVo selectVisitorReviewProcessOne(VisitorReviewProcessVo reviewProcessVo) {
        return visCarryStuffMapper.selectVisitorReviewProcessOne(reviewProcessVo);
    }

    @Override
    public void updateVisitorReviewProcessById(VisitorReviewProcessVo reviewProcess) {
        visCarryStuffMapper.updateVisitorReviewProcessById(reviewProcess);
    }

    @Override
    public void cancel(VisExitOutVo vo) {
        VisExitOutVo visExitOutVo = visCarryStuffMapper.selectVisExitOutById(vo.getId());
        if (visExitOutVo != null && !StringUtils.equals(visExitOutVo.getExamineState(), ExamineEnum.IN_REVIEW.getCode())) {
            throw new ServiceException("审核中，无法取消");
        }

        //修改申请状态
        VisExitOutVo updateVo = new VisExitOutVo();
        updateVo.setId(vo.getId());
        updateVo.setExamineState("4");
        visCarryStuffMapper.updateStaff(updateVo);

        //删除流程环节信息
        visCarryStuffMapper.deleteVisitorReviewProcess(vo.getId());
    }

    @Override
    public List<Long> selectexpireVisCarryStuff(Date dateTime) {
        return visCarryStuffMapper.selectexpireVisCarryStuff(dateTime);
    }

    @Override
    public void updateStaffState(VisExitOutVo visExitOutVo) {
        visCarryStuffMapper.updateStaffState(visExitOutVo);
    }

    @Override
    public List<VisExitOutVo> queryReviewList(Long peopleId) {
        return visCarryStuffMapper.queryReviewList(peopleId);
    }

    @Override
    public VisitorReviewProcessVo queryReviewProcess(VisitorReviewProcessVo visitorReviewProcessVo) {
        return visCarryStuffMapper.queryReviewProcess(visitorReviewProcessVo);
    }

    @Override
    public VisExitOutVo selectVisExitOutById(Long id) {
        return visCarryStuffMapper.selectVisExitOutById(id);
    }

    @Override
    public CheckCodeVo selectStuffCheckCode(Long id) {
        return visCarryStuffMapper.selectStuffCheckCode(id);
    }

    @Override
    public void updateStaff(VisExitOutVo vo) {
        visCarryStuffMapper.updateStaff(vo);
    }

    @Override
    public List<VisitorReviewProcessVo> queryVisitorReviewProcessList(Long id) {
        return visCarryStuffMapper.queryVisitorReviewProcessList(id);
    }

    @Override
    public void saveVisitorReviewProcess(VisExitOutVo vo) {
        visCarryStuffMapper.saveVisitorReviewProcess(vo);
    }

    @Override
    public VisitorReviewProcessVo queryReviewProcessNew(VisitorReviewProcessVo stuffVo) {
        return visCarryStuffMapper.queryReviewProcessNew(stuffVo);
    }
}
