package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.PoNews;
import com.ruoyi.system.mapper.PoNewsMapper;
import com.ruoyi.system.mapper.PoUserMapper;
import com.ruoyi.system.service.IPoNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息Service业务层处理
 * @date 2023-01-15
 */
@Service
public class PoNewsServiceImpl implements IPoNewsService {
    @Autowired
    private PoNewsMapper poNewsMapper;
    @Autowired
    private PoUserMapper poUserMapper;
    @Autowired
    protected Validator validator;

    @Autowired
    private PoUserServiceImpl poUserService;

    /**
     * 根据Id查询
     *
     * @param newsId
     * @return
     */
    @Override
    public List<PoNews> selectPoNewsByNewsId(String newsId) {

        List list = new ArrayList();
        List<PoNews> poNewsList = new ArrayList<>();
        poNewsList.add(poNewsMapper.selectPoNewsByNewsId(newsId));
        List<PoNews> collect = poNewsList.stream().map((item) -> {
            PoNews poNews = new PoNews();
            //将所有消息数据拷贝到消息用户链接对象实例
            BeanUtils.copyProperties(item, poNews);
            List<PoNews> newsAndUserVos = poNewsMapper.selectUserByNewsId(newsId);
            list.add(newsAndUserVos);
            poNews.setPhonenumber(list.toString());
            return poNews;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 分页查询
     *
     * @param poNews
     * @return
     */
    @Override
    public List<PoNews> selectPoNewsList(PoNews poNews) {
        return poNewsMapper.selectPoNewsList(poNews);
    }

    /**
     * 新增
     *
     * @param poNews
     * @return
     */
    @Override
    public int insertPoNews(PoNews poNews) {
        poNews.setCreateTime(DateUtils.getNowDate());
        return poNewsMapper.insertPoNews(poNews);
    }

    /**
     * 修改
     *
     * @param poNews
     * @return
     */
    @Override
    public int updatePoNews(PoNews poNews) {
        poNews.setUpdateTime(DateUtils.getNowDate());
        return poNewsMapper.updatePoNews(poNews);
    }

    /**
     * 批量删除
     *
     * @param newsIds
     * @return
     */
    @Override
    public int deletePoNewsByNewsIds(String[] newsIds) {
        return poNewsMapper.deletePoNewsByNewsIds(newsIds);
    }

    /**
     * 删除
     *
     * @param newsId
     * @return
     */
    @Override
    public int deletePoNewsByNewsId(String newsId) {
        return poNewsMapper.deletePoNewsByNewsId(newsId);
    }

    /**
     * 校验标题是否重复
     *
     * @param poNews
     * @return
     */
    @Override
    public String checkPostNewsTitleUnique(PoNews poNews) {
        String poNewsId = StringUtils.isNull(poNews.getNewsId()) ? "消息不存在" : poNews.getNewsId();
        PoNews info = poNewsMapper.checkPostNewsTitleUnique(poNews.getNewsTitle());
        if (StringUtils.isNotNull(info) && info.getNewsId() != poNewsId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 搜索消息
     *
     * @param title
     * @param newsTimeStart
     * @param newsTimeEnd
     * @return
     */

    @Override
    public List<PoNews> selectListByTitleAndTime(String title, Date newsTimeStart, Date newsTimeEnd) {
        if (title != null) {
            //标题不为空且开始时间和结束时间都不为空
            if (newsTimeStart != null && newsTimeEnd != null) {

                if (DateUtils.getNowDate().before(newsTimeEnd)){
                    newsTimeEnd = DateUtils.getNowDate();
                }
                if (newsTimeStart.before(newsTimeEnd)) {
                    return poNewsMapper.selectPoNewsListByTitleAndNewsTimeStartAndNewsTimeEnd(title, newsTimeStart, newsTimeEnd);
                } else if ((newsTimeStart != null && newsTimeEnd == null) || (newsTimeStart == null && newsTimeEnd != null)) {
                    //有一个时间不为空返回提示信息
                    List list = new ArrayList();
                    list.add("请选择正确的时间段");
                    return list;
                }
            } else {//时间都为空
                return poNewsMapper.selectPoNewsListByTitle(title);
            }
        }
        //标题为空
        else {
            //时间不为空
            if (newsTimeStart != null && newsTimeEnd != null) {
                return poNewsMapper.selectPoNewsByTime(newsTimeStart, newsTimeEnd);
            }
        }
        //都为空执行分页查询
        PoNews poNews = new PoNews();
        return poNewsMapper.selectPoNewsList(poNews);
    }

    /**
     * 获取详细消息内容
     *
     * @param newsId
     * @return
     */
    @Override
    public PoNews selectContentByNewsId(String newsId) {
        return poNewsMapper.selectContentByNewsId(newsId);
    }

    /**
     * 校验图片是否重复
     *
     * @param poNews
     * @return
     */
    @Override
    public String checkPostNewsImageUnique(PoNews poNews) {

        String poNewsId = StringUtils.isNull(poNews.getNewsId()) ? "图片不存在" : poNews.getNewsId();

        PoNews info = poNewsMapper.checkPostNewsImageUnique(poNews.getImage());
        if (StringUtils.isNotNull(info) && info.getNewsId() != poNewsId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验时间是否相等
     *
     * @param poNews
     * @return
     */
    @Override
    public String checkPostNewsTimeUnique(PoNews poNews) {
        String poNewsId = StringUtils.isNull(poNews.getNewsId()) ? "图片不存在" : poNews.getNewsId();
        PoNews info = poNewsMapper.checkPostNewsTimeUnique(poNews.getCreateTime());
        if (StringUtils.isNotNull(info) && info.getNewsId() != poNewsId) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    /**
     * 导入用户手机号
     *
     * @param phoneList     手机号数据
     * @param updateSupport 是否支持更新 存在则更新
     * @param userName      操作用户
     * @param poNews        返回结果
     * @return
     */
    @Override
    public String importPhone(List<PoNews> phoneList, boolean updateSupport, String userName, PoNews poNews) {
        if (StringUtils.isNull(phoneList) || phoneList.size() == 0) {
            throw new ServiceException("导入用户电话数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (PoNews phone : phoneList) {
            try {
                // 验证是否存在这个用户
                SysUser u = poUserMapper.selectUserByUserPhoneNumber(phone.getPhonenumber());
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, phone);
                    phone.setPhonenumber(phone.getPhonenumber());
                    phone.setCreateBy(userName);
                    this.insertPoNews(phone);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、电话号 " + phone.getPhonenumber() + " 导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、dian " + phone.getPhonenumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
                return successMsg.toString();
    }
}

