package com.ruoyi.demo.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ruoyi.demo.entity.*;
import com.ruoyi.demo.entity.bo.AttentionPoolBo;
import com.ruoyi.demo.entity.vo.TurnBusinessOpportunitiesVo;
import com.ruoyi.demo.entity.vo.WorkTableSearchVo;
import com.ruoyi.demo.mapper.*;
import com.ruoyi.demo.service.AttentionPoolService;
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 AttentionPoolServiceImpl implements AttentionPoolService {
    @Autowired
    AttentionPoolDao attentionPoolDao;
    @Autowired
    BusinessOpportunitiesDao businessOpportunitiesDao;
    @Autowired
    FollowUpDao followUpDao;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    WdInfoDao wdInfoDao;
    @Autowired
    StoreWdDao storeWdDao;

    @Override
    public PageInfo<List<AttentionPoolBo>> searchList(WorkTableSearchVo workVo, String userId) {
        PageHelper.startPage(workVo.getPageNum(),workVo.getPageSize(),true);
        List<AttentionPool> pools = attentionPoolDao.searchByMulti(workVo.getText(), userId);
        List<AttentionPoolBo> bos = new ArrayList<>();
        for (AttentionPool pool : pools){
            AttentionPoolBo bo = new AttentionPoolBo();
            bo.setAttentionId(pool.getAttentionId());
            bo.setContact(pool.getContact());
            bo.setTelephone(pool.getTelephone());
            bo.setBrandName(pool.getBrandName());
            bo.setWdName(pool.getWdName());
            bo.setWdAddrInfo(pool.getWdAddrInfo());
            bo.setAttentionTime(pool.getAttentionTime());
            bos.add(bo);
        }
        return new PageInfo(bos);
    }

    @Override
    public int cancelAttention(String attentionId, String userId) {
        BusinessOpportunitiesClue clue = businessOpportunitiesDao.searchByClueIdOrAttentionIdAndUserId(null, attentionId, userId);
        if (clue != null){
            List<FollowUpRecord> followUpRecords = followUpDao.searchAllByClueIdAndUserId(clue.getClueId(), userId);
            if (!followUpRecords.isEmpty())
                followUpDao.deleteByClueIdAndUserId(clue.getClueId(), userId);
            businessOpportunitiesDao.deleteByAttentionIdAndUserId(attentionId, userId);
        }
        return attentionPoolDao.deleteByAttentionIdAndUserId(attentionId, userId);
    }

    @Override
    public int turnBusinessOpportunities(TurnBusinessOpportunitiesVo turnVo, String userId) {
        AttentionPool pool = attentionPoolDao.searchByAttentionIdAndUserId(turnVo.getAttentionId(), userId);
        if (pool == null)
            return 0;
        BusinessOpportunitiesClue businessOpportunitiesClue = businessOpportunitiesDao.searchByClueIdOrAttentionIdAndUserId(null, turnVo.getAttentionId(), userId);
        if (businessOpportunitiesClue != null){
            return 2;
        }
        BusinessOpportunitiesClue clue = new BusinessOpportunitiesClue();
        clue.setClueId(UUID.randomUUID().toString().replace("-",""));
        clue.setAttentionId(turnVo.getAttentionId());
        clue.setContact(turnVo.getContact());
        clue.setTelephone(turnVo.getTelephone());
        clue.setFollowUpCount(0);
        clue.setUserId(userId);
        clue.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        return businessOpportunitiesDao.insert(clue);
    }

    @Override
    public int attentionBrand(String brandId, String userId) {
        AttentionPool attentionPool = attentionPoolDao.searchByBrandIdOrWdIdAndUserId(brandId,null, userId);
        if (attentionPool != null) // 判断是否已经关注
            return 2;
        Brand brand = brandMapper.searchById(brandId);
        if (brand == null) // 判断是否有该品牌
            return 3;
        AttentionPool pool = new AttentionPool();
        pool.setAttentionId(UUID.randomUUID().toString().replace("-",""));
        pool.setContact(brand.getContact());
        if (brand.getContact() == null)
            pool.setContact("");
        pool.setTelephone(brand.getTelephone());
        if (brand.getTelephone() == null)
            pool.setTelephone("");
        pool.setBrandId(brand.getBrandId());
        pool.setBrandName(brand.getBrandName());
        pool.setWdId("");
        pool.setWdName("");
        pool.setWdAddrInfo("");
        pool.setUserId(userId);
        pool.setAttentionTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        return attentionPoolDao.insert(pool);
    }

    @Override
    public int cancelBrand(String brnadId, String userId) {
        AttentionPool attentionPool = attentionPoolDao.searchByBrandIdOrWdIdAndUserId(brnadId, null, userId);
        if (attentionPool == null)
            return 1;
        BusinessOpportunitiesClue clue = businessOpportunitiesDao.searchByClueIdOrAttentionIdAndUserId(null, attentionPool.getAttentionId(), userId);
        if (clue != null){
            if (followUpDao.deleteByClueIdAndUserId(clue.getClueId(), userId) == 0)
                return 0;
            if (businessOpportunitiesDao.deleteByAttentionIdAndUserId(attentionPool.getAttentionId(),userId) == 0)
                return 0;
        }
        return attentionPoolDao.deleteByBrandIdOrWdIdAndUserId(brnadId,null,userId);
    }

    @Override
    public int attentionWdInfo(String wdId, String userId) {
        AttentionPool attentionPool = attentionPoolDao.searchByBrandIdOrWdIdAndUserId(null, wdId, userId);
        if (attentionPool != null) // 判断是否已经关注
            return 2;
        WdInfo wdInfo = wdInfoDao.selectById(wdId);
        if (wdInfo == null) // 判断是否有该网点
            return 3;
        AttentionPool pool = new AttentionPool();
        pool.setAttentionId(UUID.randomUUID().toString().replace("-",""));
        pool.setWdId(wdInfo.getWdId());
        pool.setWdName(wdInfo.getWdName());
        pool.setWdAddrInfo(wdInfo.getAddrInfo());
        StoreWd storeWd = storeWdDao.selectById(wdId);
        if (storeWd != null){
            pool.setContact(storeWd.getContact());
            if (storeWd.getContact() == null)
                pool.setContact("");
            pool.setTelephone(storeWd.getTelephone());
            if (storeWd.getTelephone() == null)
                pool.setTelephone("");
            if (storeWd.getBrandId() != null || !storeWd.getBrandId().equals("")){
                Brand brand = brandMapper.searchById(storeWd.getBrandId());
                if (brand != null){
                    pool.setBrandId(brand.getBrandId());
                    pool.setBrandName(brand.getBrandName());
                }else {
                    pool.setBrandId("");
                    pool.setBrandName("");
                }
            }
        }else {
            pool.setContact("");
            pool.setTelephone("");
            pool.setBrandId("");
            pool.setBrandName("");
        }
        pool.setUserId(userId);
        pool.setAttentionTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        return attentionPoolDao.insert(pool);
    }

    @Override
    public int cancelWdInfo(String wdId, String userId) {
        AttentionPool attentionPool = attentionPoolDao.searchByBrandIdOrWdIdAndUserId(null, wdId, userId);
        if (attentionPool == null)
            return 1;
        BusinessOpportunitiesClue clue = businessOpportunitiesDao.searchByClueIdOrAttentionIdAndUserId(null, attentionPool.getAttentionId(), userId);
        if (clue != null){
            if (followUpDao.deleteByClueIdAndUserId(clue.getClueId(), userId) == 0)
                return 0;
            if (businessOpportunitiesDao.deleteByAttentionIdAndUserId(attentionPool.getAttentionId(),userId) == 0)
                return 0;
        }
        return attentionPoolDao.deleteByBrandIdOrWdIdAndUserId(null,wdId,userId);
    }
}
