package com.ruoyi.system.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ZxFj;
import com.ruoyi.system.domain.activity.ZxActivity;
import com.ruoyi.system.domain.activity.ZxActivityUser;
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.ZxActivityMapper;
import com.ruoyi.system.service.IZxActivityService;

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

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

/**
 * 政协活动Service业务层处理
 *
 * @author boman
 * @date 2024-03-14
 */
@Service
public class ZxActivityServiceImpl implements IZxActivityService {
    @Autowired
    private ZxActivityMapper zxActivityMapper;

    @Autowired
    private ZxFjMapper zxFjMapper;

    /**
     * 查询政协活动
     *
     * @param activityId 政协活动主键
     * @return 政协活动
     */
    @Override
    public ZxActivity selectZxActivityByActivityId(Long activityId) {
        return zxActivityMapper.selectZxActivityByActivityId(activityId);
    }

    /**
     * 查询政协活动人员详情
     *
     * @param activityId 政协活动主键
     * @return 政协活动
     */
    @Override
    public List<ZxActivityUser> selectZxActivityUserByActivityId(Long activityId) {
        return zxActivityMapper.selectZxActivityUserByActivityId(activityId);
    }
    /**
     * 获取政协活动人员详细信息
     */
    @Override
    public List<ZxActivityUser> selectZxActivityUser(ZxActivityUser zxActivityUser) {
        return zxActivityMapper.selectZxActivityUser(zxActivityUser);
    }

    /**
     * 查询政协活动列表
     *
     * @param zxActivity 政协活动
     * @return 政协活动
     */
    @Override
    public List<ZxActivity> selectZxActivityList(ZxActivity zxActivity) {
        List<ZxActivity> zxActivities = zxActivityMapper.selectZxActivityList(zxActivity);
        if (zxActivities != null && zxActivities.size() > 0) {
            long count;
            for (ZxActivity activity : zxActivities) {
                List<ZxActivityUser> zxActivityUserList = activity.getZxActivityUserList();
                activity.setRead("0");
                activity.setNoRead("0");
                if (zxActivityUserList != null && zxActivityUserList.size() > 0) {
                    //找出答复字段不是null的
                    count = zxActivityUserList.stream().filter(e -> e.getIsJoin() != null).count();
                    activity.setRead(count + "");
                    activity.setNoRead(String.valueOf(zxActivityUserList.size() - count));
                }
            }
        }
        return zxActivities;
    }

    /**
     * 新增政协活动
     *
     * @param zxActivity 政协活动
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertZxActivity(ZxActivity zxActivity) {
        zxActivity.setCreateTime(DateUtils.getNowDate());
        int rows = zxActivityMapper.insertZxActivity(zxActivity);
        insertZxActivityUser(zxActivity);
        insertZxFj(zxActivity);
        return rows;
    }

    /**
     * 修改活动答复信息
     */
    @Override
    public int updateReply(ZxActivityUser zxActivityUser) {
        zxActivityUser.setUserId(SecurityUtils.getUserId());
        return zxActivityMapper.updateReply(zxActivityUser);
    }

    /**
     * 修改政协活动
     *
     * @param zxActivity 政协活动
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateZxActivity(ZxActivity zxActivity) {
        zxActivity.setUpdateTime(DateUtils.getNowDate());
        zxActivityMapper.deleteZxActivityUserByActivityId(zxActivity.getActivityId());
        zxFjMapper.deleteZxFjBySourceId(zxActivity.getActivityId());
        insertZxActivityUser(zxActivity);
        insertZxFj(zxActivity);
        return zxActivityMapper.updateZxActivity(zxActivity);
    }

    /**
     * 批量删除政协活动
     *
     * @param activityIds 需要删除的政协活动主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteZxActivityByActivityIds(Long[] activityIds) {
        zxActivityMapper.deleteZxActivityUserByActivityIds(activityIds);
        return zxActivityMapper.deleteZxActivityByActivityIds(activityIds);
    }

    /**
     * 删除政协活动信息
     *
     * @param activityId 政协活动主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteZxActivityByActivityId(Long activityId) {
        zxActivityMapper.deleteZxActivityUserByActivityId(activityId);
        return zxActivityMapper.deleteZxActivityByActivityId(activityId);
    }

    /**
     * 新增政协活动-用户关联信息
     *
     * @param zxActivity 政协活动对象
     */
    public void insertZxActivityUser(ZxActivity zxActivity) {
        List<ZxActivityUser> zxActivityUserList = zxActivity.getZxActivityUserList();
        Long activityId = zxActivity.getActivityId();
        if (StringUtils.isNotNull(zxActivityUserList)) {
            List<ZxActivityUser> list = new ArrayList<ZxActivityUser>();
            for (ZxActivityUser zxActivityUser : zxActivityUserList) {
                zxActivityUser.setActivityId(activityId);
                list.add(zxActivityUser);
            }
            if (list.size() > 0) {
                zxActivityMapper.batchZxActivityUser(list);
            }
        }
    }

    /**
     * 新增活动附件关联信息
     */
    public void insertZxFj(ZxActivity zxActivity)
    {
        List<ZxFj> zxFjListFj = zxActivity.getZxFjListFj();
        Long activityId = zxActivity.getActivityId();
        if (StringUtils.isNotNull(zxFjListFj))
        {
            List<ZxFj> list = new ArrayList<ZxFj>();
            for (ZxFj zxFj : zxFjListFj)
            {
                zxFj.setType(THR);
                zxFj.setMainId(activityId);
                zxFj.setSourceId(activityId);
                list.add(zxFj);
            }
            if (list.size() > 0)
            {
                zxFjMapper.batchZxFj(list);
            }
        }
    }
}
