package com.ruoyi.ibs.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.grid.domain.vo.AddressDetail;
import com.ruoyi.ibs.grid.mapper.AnchorMapper;
import com.ruoyi.ibs.task.domain.dto.VisitLogDTO;
import com.ruoyi.ibs.task.domain.entity.VisitLog;
import com.ruoyi.ibs.task.domain.vo.VisitLogVO;
import com.ruoyi.ibs.task.mapper.VisitLogMapper;
import com.ruoyi.ibs.task.service.VisitLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 吴凯程
 * @Date 2025/3/3
 **/
@Service
public class VisitLogServiceImpl implements VisitLogService {


    @Resource
    private VisitLogMapper visitLogMapper;

    @Resource
    private AnchorMapper anchorMapper;


    /**
     * @param visitLogDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addVisitLog(VisitLogDTO visitLogDTO) {
        VisitLog visitLog = new VisitLog();
        BeanUtils.copyBeanProp(visitLog, visitLogDTO);
        visitLog.setNickName(SecurityUtils.getLoginUser().getUser().getNickName());
        visitLogMapper.insert(visitLog);
        return "走访日志插入成功";
    }

    /**
     * @param custId
     * @return
     */
    @Override
    public VisitLogVO getVisitLogList(String custId) {
        LambdaQueryWrapper<VisitLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VisitLog::getCustId, custId)
                .orderByDesc(VisitLog::getCreateTime);
        List<VisitLog> visitLogs = visitLogMapper.selectList(queryWrapper);
        List<AddressDetail> addressDetails = anchorMapper.selectAnchorAddressListByCustId(custId);
        VisitLogVO visitLogVO = new VisitLogVO();
        visitLogVO.setVisitLogList(visitLogs);
        visitLogVO.setAddressDetails(addressDetails);
        return visitLogVO;
    }
}
