package com.ruoyi.system.service.impl;


import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QRCodeUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ZxFj;
import com.ruoyi.system.domain.conference.ZxConference;
import com.ruoyi.system.domain.conference.ZxConferenceUser;
import com.ruoyi.system.domain.notice.ZxNotice;
import com.ruoyi.system.mapper.ZxFjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.ZxConferenceMapper;
import com.ruoyi.system.service.IZxConferenceService;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.constant.CommonConstants.FOR;
import static com.ruoyi.common.constant.CommonConstants.THR;

/**
 * 政协会议活动Service业务层处理
 *
 * @author boman
 * @date 2024-03-06
 */
@Service
public class ZxConferenceServiceImpl implements IZxConferenceService {
    @Autowired
    private ZxConferenceMapper zxConferenceMapper;
    @Autowired
    private ZxFjMapper zxFjMapper;

    /**
     * 查询政协会议活动
     *
     * @param conferenceId 政协会议活动主键
     * @return 政协会议活动
     */
    @Override
    public ZxConference selectZxConferenceByConferenceId(Long conferenceId) {
        return zxConferenceMapper.selectZxConferenceByConferenceId(conferenceId);
    }

    /**
     * 查询政协会议活动人员详情
     *
     * @param conferenceId 政协会议活动主键
     * @return
     */
    @Override
    public List<ZxConferenceUser> selectZxConferenceUserByConferenceId(Long conferenceId) {
        return zxConferenceMapper.selectZxConferenceUserByConferenceId(conferenceId);
    }

    /**
     *查询政协会议活动人员详情
     * @return
     */
    @Override
    public List<ZxConferenceUser> selectZxConferenceUser(ZxConferenceUser zxConferenceUser) {
        return zxConferenceMapper.selectZxConferenceUser(zxConferenceUser);
    }

    /**
     * 查询政协会议活动列表
     *
     * @param zxConference 政协会议活动
     * @return 政协会议活动
     */
    @Override
    public List<ZxConference> selectZxConferenceList(ZxConference zxConference) {
        //查询是否答复，不答复算未读
        List<ZxConference> zxConferences = zxConferenceMapper.selectZxConferenceList(zxConference);
        if (zxConferences != null && zxConferences.size() > 0) {
            long count;
            for (ZxConference conference : zxConferences) {
                List<ZxConferenceUser> zxConferenceUserList = conference.getZxConferenceUserList();
                conference.setRead("0");
                conference.setNoRead("0");
                if (zxConferenceUserList != null && zxConferenceUserList.size() > 0) {
                    //找出答复字段不是null的
                    count = zxConferenceUserList.stream().filter(e -> e.getIsJoin() != null).count();
                    conference.setRead(count + "");
                    conference.setNoRead(String.valueOf(zxConferenceUserList.size() - count));
                }
            }
        }
        return zxConferences;
    }

    /**
     * 新增政协会议活动
     *
     * @param zxConference 政协会议活动
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertZxConference(ZxConference zxConference) {
        //todo 给参会人员发送短信
        zxConference.setCreateTime(DateUtils.getNowDate());
        int rows = zxConferenceMapper.insertZxConference(zxConference);
        insertZxConferenceUser(zxConference);
        insertZxFj(zxConference);
        try {
            String imagePath= QRCodeUtils.encode(zxConference.getConferenceId().toString(), null, RuoYiConfig.getUploadQrPath(), true);
            zxConference.setConferenceQr(imagePath);
            zxConferenceMapper.updateZxConference(zxConference);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    /**
     * 修改政协会议活动
     *
     * @param zxConference 政协会议活动
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateZxConference(ZxConference zxConference) {
        //todo 给参会人员发送短信
        zxConference.setUpdateTime(DateUtils.getNowDate());
        zxConferenceMapper.deleteZxConferenceUserByConferenceId(zxConference.getConferenceId());
        zxFjMapper.deleteZxFjBySourceId(zxConference.getConferenceId());
        insertZxConferenceUser(zxConference);
        insertZxFj(zxConference);
        return zxConferenceMapper.updateZxConference(zxConference);
    }

    /**
     * 修改会议答复信息
     */
    @Override
    public int updateReply(ZxConferenceUser zxConferenceUser) {
        zxConferenceUser.setUserId(SecurityUtils.getUserId());
        return zxConferenceMapper.updateReply(zxConferenceUser);
    }

    /**
     * 批量删除政协会议活动
     *
     * @param conferenceIds 需要删除的政协会议活动主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteZxConferenceByConferenceIds(Long[] conferenceIds) {
        zxConferenceMapper.deleteZxConferenceUserByConferenceIds(conferenceIds);
        return zxConferenceMapper.deleteZxConferenceByConferenceIds(conferenceIds);
    }

    /**
     * 删除政协会议活动信息
     *
     * @param conferenceId 政协会议活动主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteZxConferenceByConferenceId(Long conferenceId) {
        zxConferenceMapper.deleteZxConferenceUserByConferenceId(conferenceId);
        return zxConferenceMapper.deleteZxConferenceByConferenceId(conferenceId);
    }

    /**
     * 新增政协会议活动-用户关联信息
     *
     * @param zxConference 政协会议活动对象
     */
    public void insertZxConferenceUser(ZxConference zxConference) {
        List<ZxConferenceUser> zxConferenceUserList = zxConference.getZxConferenceUserList();
        Long conferenceId = zxConference.getConferenceId();
        if (StringUtils.isNotNull(zxConferenceUserList)) {
            List<ZxConferenceUser> list = new ArrayList<ZxConferenceUser>();
            for (ZxConferenceUser zxConferenceUser : zxConferenceUserList) {
                zxConferenceUser.setConferenceId(conferenceId);
                list.add(zxConferenceUser);
            }
            if (list.size() > 0) {
                zxConferenceMapper.batchZxConferenceUser(list);
            }
        }
    }

    /**
     * 新增政协会议附件关联信息
     *
     */
    public void insertZxFj(ZxConference zxConference)
    {
        List<ZxFj> zxFjListFj = zxConference.getZxFjListFj();
        Long conferenceId = zxConference.getConferenceId();
        if (StringUtils.isNotNull(zxFjListFj))
        {
            List<ZxFj> list = new ArrayList<ZxFj>();
            for (ZxFj zxFj : zxFjListFj)
            {
                zxFj.setType(FOR);
                zxFj.setMainId(conferenceId);
                zxFj.setSourceId(conferenceId);
                list.add(zxFj);
            }
            if (list.size() > 0)
            {
                zxFjMapper.batchZxFj(list);
            }
        }
    }
}
