package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ruoyi.demo.entity.BusinessOpportunitiesClue;
import com.ruoyi.demo.entity.FollowUpRecord;
import com.ruoyi.demo.entity.bo.BusinessOpportunitiesClueBo;
import com.ruoyi.demo.entity.bo.FollowUpRecordBo;
import com.ruoyi.demo.entity.vo.AddFollowRecordVo;
import com.ruoyi.demo.entity.vo.WorkTableSearchVo;
import com.ruoyi.demo.mapper.BusinessOpportunitiesDao;
import com.ruoyi.demo.mapper.FollowUpDao;
import com.ruoyi.demo.service.BusinessOpportunitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class BusinessOpportunitiesServiceImpl implements BusinessOpportunitiesService {
    @Autowired
    BusinessOpportunitiesDao businessOpportunitiesDao;
    @Autowired
    FollowUpDao followUpDao;

    @Override
    public PageInfo<List<BusinessOpportunitiesClueBo>> searchList(WorkTableSearchVo workVo, String userId) {
        PageHelper.startPage(workVo.getPageNum(), workVo.getPageSize(), true);

        QueryWrapper<BusinessOpportunitiesClue> clueQueryWrapper = new QueryWrapper<>();
        clueQueryWrapper.eq("user_id", userId);
        if (workVo.getText() != null)
            clueQueryWrapper.and(businessOpportunitiesClueQueryWrapper -> {
                businessOpportunitiesClueQueryWrapper.like("contact", workVo.getText())
                    .or()
                    .like("telephone", workVo.getText());
            });
        List<BusinessOpportunitiesClue> clues = businessOpportunitiesDao.selectList(clueQueryWrapper);

        List<BusinessOpportunitiesClueBo> clueBos = new ArrayList<>();
        clues.forEach(clue -> clueBos.add(new BusinessOpportunitiesClueBo(clue)));
        return new PageInfo(clueBos);
    }

    @Override
    public PageInfo<List<FollowUpRecordBo>> searchFollowList(WorkTableSearchVo workVo, String userId) {
        PageHelper.startPage(workVo.getPageNum(), workVo.getPageSize(), true);
        if (workVo.getStartTime() != null || workVo.getStartTime().equals(""))
                workVo.setStartTime(null);
        if (workVo.getEndTime() != null || workVo.getEndTime().equals(""))
                workVo.setEndTime(null);

        List<FollowUpRecord> records = followUpDao.searchAllByMulti(workVo.getClueId(), workVo.getText(), workVo.getStartTime(), workVo.getEndTime(), userId);
        List<FollowUpRecordBo> recordBos = new ArrayList<>();
        records.forEach(record -> recordBos.add(new FollowUpRecordBo(record)));
        return new PageInfo(recordBos);
    }

    @Override
    public int addFollow(AddFollowRecordVo addVo, String userId) {
        if (addVo.getClueId() == null || addVo.getClueId().equals(""))
            return 0;
        BusinessOpportunitiesClue clue = businessOpportunitiesDao.searchByClueIdOrAttentionIdAndUserId(addVo.getClueId(), null, userId);
        if (clue == null)
            return 0;
        FollowUpRecord record = new FollowUpRecord();
        record.setRecordId(UUID.randomUUID().toString().replace("-", ""));
        record.setClueId(addVo.getClueId());
        record.setFollowContent(addVo.getContent());
        record.setFollowPerson(addVo.getPerson());
        record.setFollowWay(addVo.getWay());
        record.setUserId(userId);
        record.setFollowTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        // 插入并更新跟进数
        if (followUpDao.insert(record) == 1) {
            return businessOpportunitiesDao.updateFollowUpCountByClueIdAndUserId(clue.getClueId(), clue.getFollowUpCount() + 1, userId);
        }
        return 0;
    }
}
