package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.activity.ZxActivity;
import com.ruoyi.system.domain.conference.ZxConference;
import com.ruoyi.system.domain.member.MemberInfo;
import com.ruoyi.system.domain.member.MemberInfoTree;
import com.ruoyi.system.domain.speak.ZxSpeak;
import com.ruoyi.system.domain.vo.MemberInfoVo;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IMemberInfoService;

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

/**
 * 委员信息Service业务层处理
 * 
 * @author boman
 * @date 2024-03-07
 */
@Service
public class MemberInfoServiceImpl implements IMemberInfoService 
{
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private ZxConferenceMapper zxConferenceMapper;
    @Autowired
    private ZxActivityMapper zxActivityMapper;
    @Autowired
    private ZxSpeakMapper zxSpeakMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询委员信息
     * 
     * @param memberId 委员信息主键
     * @return 委员信息
     */
    @Override
    public MemberInfo selectMemberInfoByMemberId(Long memberId)
    {
        return memberInfoMapper.selectMemberInfoByMemberId(memberId);
    }

    /**
     * 获取委员履职信息详细信息
     */
    @Override
    public MemberInfoVo getInfoJop(Long memberId) {
        MemberInfoVo  memberInfoVo = new MemberInfoVo();
        MemberInfo memberInfo = memberInfoMapper.selectMemberInfoByMemberId(memberId);
        memberInfoVo.setMemberInfo(memberInfo);
        //todo 提案/社情民意还有联民提案的
        ZxConference zxConference = new ZxConference();
        zxConference.setUserId(SecurityUtils.getUserId());
        List<ZxConference> zxConferences = zxConferenceMapper.selectZxConferenceList(zxConference);

        if (zxConferences != null){
            memberInfoVo.setZxConferenceList(zxConferences);
        }
        ZxActivity zxActivity = new ZxActivity();
        zxActivity.setUserId(SecurityUtils.getUserId());
        List<ZxActivity> zxActivities = zxActivityMapper.selectZxActivityList(zxActivity);
        if (zxActivities != null){
            memberInfoVo.setZxActivityList(zxActivities);
        }
        ZxSpeak zxSpeak = new ZxSpeak();
        zxSpeak.setUserId(SecurityUtils.getUserId());
        List<ZxSpeak> zxSpeaks = zxSpeakMapper.selectZxSpeakList(zxSpeak);
        if (zxSpeaks != null){
            memberInfoVo.setZxSpeakList(zxSpeaks);
        }
        return memberInfoVo;
    }

    /**
     * 查询委员信息列表
     * 
     * @param memberInfo 委员信息
     * @return 委员信息
     */
    @Override
    public List<MemberInfo> selectMemberInfoList(MemberInfo memberInfo)
    {
        return memberInfoMapper.selectMemberInfoList(memberInfo);
    }

    /**
     * 获取委员菜单下拉树列表
     */
    @Override
    public List<MemberInfoTree> buildMemberInfoTreeSelect(List<MemberInfo> memberInfo) {
        //查询字典值，政协委员级别
        List<MemberInfoTree> memberInfoTreeList = new ArrayList<>();
        if (memberInfo != null && memberInfo.size() >0){
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType("grade");
            if (dictDataList !=null && dictDataList.size() >0){
                for (SysDictData sysDictData : dictDataList) {
                    MemberInfoTree memberInfoTree = new MemberInfoTree();
                    memberInfoTree.setUserId(sysDictData.getDictValue());
                    memberInfoTree.setName(sysDictData.getDictLabel());
                    memberInfoTreeList.add(memberInfoTree);
                }
                for (MemberInfoTree memberInfoTree : memberInfoTreeList) {
                    //委员级别id
                    String id = memberInfoTree.getUserId();
                    List<MemberInfo> memberInfoList = new ArrayList<>();
                    for (MemberInfo info : memberInfo) {
                        if (info.getGrade().contains(id)){
                            memberInfoList.add(info);
                        }
                    }
                    memberInfoTree.setChildren(memberInfoList);
                }
            }
        }
        return memberInfoTreeList;
    }

    /**
     * 新增委员信息
     * 
     * @param memberInfo 委员信息
     * @return 结果
     */
    @Override
    public int insertMemberInfo(MemberInfo memberInfo)
    {
        memberInfo.setUserId(SecurityUtils.getUserId());
        memberInfo.setCreateTime(DateUtils.getNowDate());
        return memberInfoMapper.insertMemberInfo(memberInfo);
    }

    /**
     * 修改委员信息
     * 
     * @param memberInfo 委员信息
     * @return 结果
     */
    @Override
    public int updateMemberInfo(MemberInfo memberInfo)
    {
        memberInfo.setUpdateTime(DateUtils.getNowDate());
        return memberInfoMapper.updateMemberInfo(memberInfo);
    }

    /**
     * 批量删除委员信息
     * 
     * @param memberIds 需要删除的委员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberIds(Long[] memberIds)
    {
        return memberInfoMapper.deleteMemberInfoByMemberIds(memberIds);
    }

    /**
     * 删除委员信息信息
     * 
     * @param memberId 委员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberId(Long memberId)
    {
        return memberInfoMapper.deleteMemberInfoByMemberId(memberId);
    }
}
