package com.xjrsoft.module.oa.service.impl;

import cn.hutool.core.convert.Convert;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.oa.entity.FileReceive;
import com.xjrsoft.module.oa.entity.WfOaPull;
import com.xjrsoft.module.oa.entity.WfOaPush;
import com.xjrsoft.module.oa.mapper.FileReceiveMapper;
import com.xjrsoft.module.oa.service.IFileReceiveService;
import com.xjrsoft.module.oa.service.IWfOaPullService;
import com.xjrsoft.module.oa.service.IWfOaPushService;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.teacher.entity.BaseTeacher;
import com.xjrsoft.module.teacher.mapper.BaseTeacherMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @title: 文件接收管理
 * @Author fanxp
 * @Date: 2023-11-03
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class FileReceiveServiceImpl extends MPJBaseServiceImpl<FileReceiveMapper, FileReceive> implements IFileReceiveService {

    private final IWfOaPullService pullService;

    private final IWfOaPushService pushService;

    private final FileReceiveMapper receiveMapper;

    private final BaseTeacherMapper baseTeacherMapper;

    @Transactional
    @Override
    public void pullDataCache(Long id) {
        WfOaPull wfOaPull = pullService.getById(id);
        String orgStr = wfOaPull.getPullOrgan();
        List<Long> deptIds = Convert.toList(Long.class, orgStr.split(","));

        save(id,deptIds,2);
    }

    @Transactional
    @Override
    public void pushDataCache(Long id) {
        WfOaPush wfOaPush = pushService.getById(id);
        String orgStr = wfOaPush.getRecipients();
        List<Long> deptIds = Convert.toList(Long.class, orgStr.split(","));

        save(id,deptIds,1);
    }

    /**
     * 保存数据
     * @param dataId 数据编号
     * @param deptIds 部门编号
     * @param type 文件类型 1=发文 2=收文
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void save(Long dataId, List<Long> deptIds, Integer type) {
        Set<Long> userIds = baseTeacherMapper.selectJoinList(BaseTeacher.class,
                        new MPJLambdaWrapper<BaseTeacher>()
                                .innerJoin(UserDeptRelation.class, UserDeptRelation::getUserId, BaseTeacher::getUserId)
                                .in(UserDeptRelation::getDeptId, deptIds)
                                .select(UserDeptRelation.class, x -> VoToColumnUtil.fieldsToColumns(UserDeptRelation.class).contains(x.getProperty())))
                .stream().map(BaseTeacher::getUserId).collect(Collectors.toSet());

        for (Long userId : userIds) {
            FileReceive temp = new FileReceive();
            temp.setDataId(dataId);
            temp.setReceiverUserId(userId);
            temp.setFileType(type);
            receiveMapper.insert(temp);
        }
    }
}
