package com.ruoyi.demo.service.impl;

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);
        List<BusinessOpportunitiesClue> clues = businessOpportunitiesDao.searchAllByMulti(workVo.getText(), userId);
        List<BusinessOpportunitiesClueBo> clueBos = new ArrayList<>();
        for (BusinessOpportunitiesClue clue : clues){
            BusinessOpportunitiesClueBo clueBo = new BusinessOpportunitiesClueBo();
            clueBo.setClueId(clue.getClueId());
            clueBo.setAttentionId(clue.getAttentionId());
            clueBo.setContact(clue.getContact());
            clueBo.setTelephone(clue.getTelephone());
            clueBo.setFollowUpCount(clue.getFollowUpCount());
            clueBo.setCreateTime(clue.getCreateTime());
            clueBos.add(clueBo);
        }
        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){
            if (workVo.getStartTime().equals(""))
                workVo.setStartTime(null);
        }
        if (workVo.getEndTime() != null){
            if (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<>();
        for (FollowUpRecord record : records){
            FollowUpRecordBo recordBo = new FollowUpRecordBo();
            recordBo.setRecordId(record.getRecordId());
            recordBo.setContent(record.getFollowContent());
            recordBo.setPerson(record.getFollowPerson());
            recordBo.setWay(record.getFollowWay());
            recordBo.setFollowTime(record.getFollowTime());
            recordBos.add(recordBo);
        }
        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;
    }
}
