package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.LcdInfo;
import com.ruoyi.system.domain.LcdInfoUserRelation;
import com.ruoyi.system.domain.vo.LcdInfoVo;
import com.ruoyi.system.mapper.LcdInfoMapper;
import com.ruoyi.system.mapper.LcdInfoUserRelationMapper;
import com.ruoyi.system.service.ILcdInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-14
 */
@Transactional//涉及两张表要加事务
@Service
public class LcdInfoServiceImpl implements ILcdInfoService {
    @Autowired
    private LcdInfoMapper lcdInfoMapper;
    @Autowired
    private LcdInfoUserRelationMapper lcdInfoUserRelationMapper;

    /**
     * 查询消息
     *
     * @param id 消息主键
     * @return 消息
     */
    @Override
    public List<LcdInfoVo> selectLcdInfoById(Long id) {

        List<LcdInfo> records = lcdInfoMapper.selectLcdInfoById(id);
        List<LcdInfoVo> lcdInfoVos = records.stream().map((item) -> {
            LcdInfoVo lcdInfoVo = new LcdInfoVo();
            BeanUtils.copyProperties(item , lcdInfoVo);
            List<String> list1 = lcdInfoMapper.selectMobile(id);
            lcdInfoVo.setMobile(list1);
            return lcdInfoVo;
        }).collect(Collectors.toList());
        return lcdInfoVos;
    }




    /**
     * 查询消息列表
     *
     * @param lcdInfo 消息
     * @return 消息
     */
    @Override
    public List<LcdInfo> selectLcdInfoList(LcdInfo lcdInfo) {
        return lcdInfoMapper.selectLcdInfoList(lcdInfo);
    }

    /**
     * 新增消息
     *
     * @param lcdInfoVo 消息
     * @return 结果
     */
    @Override
    public AjaxResult insertLcdInfo(LcdInfoVo lcdInfoVo) {
        lcdInfoVo.setCreateTime(DateUtils.getNowDate());//插入前端传入的数据
        if (lcdInfoVo.getTitle()!=null){
            if (lcdInfoMapper.selectTitleById(lcdInfoVo.getTitle())==null){
                int row = lcdInfoMapper.insertLcdInfo(lcdInfoVo);
                long info_id =lcdInfoVo.getId();//根据前端传入数据查询消息id
                for (int i = 0; i < lcdInfoVo.getMobile().size(); i++) {
                    int userId = lcdInfoMapper.selectUserId(lcdInfoVo.getMobile().get(i));//根据电话号获取userId
                    LcdInfoUserRelation lcdInfoUserRelation = new LcdInfoUserRelation();//插入中间表数据
                    lcdInfoUserRelation.setLcdInfoId(info_id);
                    lcdInfoUserRelation.setLcdReceiveUserId(userId);
                    lcdInfoUserRelation.setCreateAt(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
                    lcdInfoUserRelation.setUpdateAt(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
                    lcdInfoUserRelationMapper.insertLcdInfoUserRelation(lcdInfoUserRelation);
                }
                return AjaxResult.success("新增成功");
            }else {
                return AjaxResult.error("存在相同标题，请删除重试！");
            }
        }else {
            return AjaxResult.error("标题不能为空");
        }
    }

    @Override
    public int selectUserId(LcdInfoVo lcdInfoVo) {
        return 0;
    }
    /**
     * 修改消息
     *
     * @param lcdInfo 消息
     * @return 结果
     */
    @Override
    public int updateLcdInfo(LcdInfo lcdInfo) {
        lcdInfo.setUpdateTime(DateUtils.getNowDate());
        return lcdInfoMapper.updateLcdInfo(lcdInfo);
    }

    /**
     * 批量删除消息
     *
     * @param ids 需要删除的消息主键
     * @return 结果
     */
    @Override
    public int deleteLcdInfoByIds(Long[] ids) {
        return lcdInfoMapper.deleteLcdInfoByIds(ids);
    }

    /**
     * 删除消息信息
     *
     * @param id 消息主键
     * @return 结果
     */
    @Override
    public int deleteLcdInfoById(Long id) {
        lcdInfoUserRelationMapper.deleteLcdInfoUserRelationById(id);
        return lcdInfoMapper.deleteLcdInfoById(id);
    }

    /**
     * 搜索消息列表.
     *
     * @param title
     * @param lcdTimeLeft
     * @param lcdTimeRight
     * @return 结果
     */
    @Override
    public List<LcdInfo> selectList(String title, Date lcdTimeLeft, Date lcdTimeRight) {
        if (title != null) {
            //标题不为空则判断时间
            if (lcdTimeLeft != null && lcdTimeRight != null) {
                //时间不为空 ，且左时间在右时间之前
                //确保查询到的公告为已发布的公告
                if (DateUtils.getNowDate().before(lcdTimeRight)){
                    lcdTimeRight = DateUtils.getNowDate();
                }
                //时间不为空 ，且左时间在右时间之前
                if (lcdTimeLeft.before(lcdTimeRight)) {
                    return lcdInfoMapper.selectLcdListByTitleOrlcdTime(title, lcdTimeLeft, lcdTimeRight);
                } else {
                    List list = new ArrayList<>();
                    list.add("时间顺序错误");
                    return list;
                }
            } else {
                //时间为空
                return lcdInfoMapper.selectLcdListByTitleOrlcdTime(title, null, null);
            }
        } else {
            //标题为空
            if (lcdTimeLeft != null && lcdTimeRight != null && lcdTimeLeft.before(lcdTimeRight)) {
                //时间不为空
                return lcdInfoMapper.selectLcdListByTitleOrlcdTime(null, lcdTimeLeft, lcdTimeRight);
            }
        }
        LcdInfo lcdInfo = new LcdInfo();
        return lcdInfoMapper.selectLcdInfoList(lcdInfo);
    }
}
