package cn.iocoder.yudao.module.system.service.reservation;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.VisitRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.VisitRecordRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitAppointmentDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRecordDetailDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRegistrationDO;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.VisitAppointmentMapper;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.VisitRecordDetailMapper;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.VisitRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.VisitRegistrationMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.List;


/*
 * 客户回访记录 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class VisitRecordServiceImpl implements VisitRecordService {

    @Resource
    private VisitRecordMapper visitRecordMapper;
    @Resource
    private VisitRecordDetailMapper visitRecordDetailMapper;
    @Resource
    private VisitAppointmentMapper visitAppointmentMapper;
    @Resource
    private VisitRegistrationMapper visitRegistrationMapper;

    @Override
    public void createReturnVisitRecord(VisitRecordDO visitRecordDO) {
        VisitRecordDO dbVisitRecord = visitRecordMapper.selectOne(new LambdaQueryWrapperX<VisitRecordDO>()
                .eq(VisitRecordDO::getSourceId, visitRecordDO.getSourceId()));
        if (dbVisitRecord == null) {
            //只生成一次回访记录
            visitRecordMapper.insert(visitRecordDO);
            //生成详情
            VisitRecordDetailDO detailDO = new VisitRecordDetailDO();
            detailDO.setReturnVisitRecordId(visitRecordDO.getId());
            BeanUtils.copyProperties(visitRecordDO, detailDO);
            detailDO.setId(null);
            detailDO.setTenantId(visitRecordDO.getTenantId());
            visitRecordDetailMapper.insert(detailDO);
        } else {
            //生成详情
            VisitRecordDetailDO detailDO = new VisitRecordDetailDO();
            detailDO.setReturnVisitRecordId(dbVisitRecord.getId());
            BeanUtils.copyProperties(visitRecordDO, detailDO);
            detailDO.setId(null);
            detailDO.setTenantId(dbVisitRecord.getTenantId());
            visitRecordDetailMapper.insert(detailDO);
        }
    }

    @Override
    public void deleteVisitRecord(Long[] ids) {
        for (Long id : ids) {
            visitRecordMapper.deleteById(id);
        }
    }

    @Override
    public VisitRecordRespVO getVisitRecord(Long id) {
        VisitRecordRespVO respVO = new VisitRecordRespVO();
        VisitRecordDO visitRecordDO = visitRecordMapper.selectById(id);
        BeanUtils.copyProperties(visitRecordDO, respVO);
        if (visitRecordDO.getSourceType() == 1) {
            VisitAppointmentDO visitAppointmentDO = visitAppointmentMapper.selectById(visitRecordDO.getSourceId());
            respVO.setVisitAppointment(visitAppointmentDO);
        } else {
            VisitRegistrationDO registrationDO = visitRegistrationMapper.selectById(visitRecordDO.getSourceId());
            respVO.setVisitRegistration(registrationDO);
        }
        List<VisitRecordDetailDO> visitRecordDetails = visitRecordDetailMapper.selectList(new LambdaQueryWrapperX<VisitRecordDetailDO>()
                .eq(VisitRecordDetailDO::getReturnVisitRecordId, visitRecordDO.getId()));
        respVO.setDetails(visitRecordDetails);
        return respVO;
    }

    @Override
    public PageResult<VisitRecordDO> getVisitRecordPage(VisitRecordPageReqVO pageReqVO) {
        return visitRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateVisitRecordDetail(VisitRecordDetailDO updateReqVO) {
        visitRecordDetailMapper.updateById(updateReqVO);
    }
}