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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.enums.MaterialCategoryEnum;
import com.xjrsoft.common.utils.RedisUtil;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.config.CommonPropertiesConfig;
import com.xjrsoft.module.concat.service.IXjrUserService;
import com.xjrsoft.module.form.dto.FormExecuteAddOrUpdateDto;
import com.xjrsoft.module.form.service.IFormExecuteService;
import com.xjrsoft.module.material.dto.*;
import com.xjrsoft.module.material.entity.*;
import com.xjrsoft.module.material.mapper.MaterialTaskAppendixMapper;
import com.xjrsoft.module.material.mapper.MaterialTaskAssignMapper;
import com.xjrsoft.module.material.mapper.MaterialTaskMapper;
import com.xjrsoft.module.material.service.IMaterialTaskService;
import com.xjrsoft.module.material.service.IMaterialTypeService;
import com.xjrsoft.module.material.vo.MaterialTaskAssignListVo;
import com.xjrsoft.module.material.vo.MaterialTaskVo;
import com.xjrsoft.module.material.vo.MaterialTypeVo;
import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.organization.entity.UserDeptRelation;
import com.xjrsoft.module.organization.service.IUserService;
import com.xjrsoft.module.organization.service.IWeChatService;
import com.xjrsoft.module.student.entity.BaseStudentSchoolRoll;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import com.xjrsoft.module.teacher.entity.XjrUser;
import com.xjrsoft.module.teacher.vo.XjrUserVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
* @title: 材料提交任务
* @Author baicai
* @Date: 2023-10-31
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class MaterialTaskServiceImpl extends MPJBaseServiceImpl<MaterialTaskMapper, MaterialTask> implements IMaterialTaskService {
    private final MaterialTaskMapper materialTaskMaterialTaskMapper;

    private final MaterialTaskAssignMapper materialTaskMaterialTaskAssignMapper;
    private final MaterialTaskAppendixMapper materialTaskMaterialTaskAppendixMapper;

    private final IMaterialTypeService materialTypeService;
    private final IWeChatService weChatService;
    private final IXjrUserService xjrUserService;
    private final IFormExecuteService formExecuteService;

    private final CommonPropertiesConfig commonPropertiesConfig;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long add(AddMaterialTaskDto dto) {
        MaterialTask materialTask = BeanUtil.toBean(dto, MaterialTask.class);
        //新增任务
        materialTask.setStatus(2);
        if(dto.getUserList() == null && !dto.getUserList().isEmpty()){
            materialTask.setAssignPerson(dto.getUserList().size());
        }
        materialTaskMaterialTaskMapper.insert(materialTask);

        //新增任务人
        for (MaterialAssignUserDto materialAssignUserDto : dto.getUserList()){
            if(materialAssignUserDto.getUserType() == 1){
                List<XjrUserVo> xjrUserVos = xjrUserService.selectJoinList(XjrUserVo.class,
                    new MPJLambdaWrapper<XjrUser>()
                    .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, XjrUser::getId)
                    .eq(UserDeptRelation::getDeptId, materialAssignUserDto.getUserId())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                for (XjrUserVo xjrUserVo : xjrUserVos) {
                    materialTaskMaterialTaskAssignMapper.insert(new MaterialTaskAssign(){{
                        setMaterialTaskId(materialTask.getId());
                        setUserId(xjrUserVo.getId());
                        setUserType(materialAssignUserDto.getUserType());
                        setUserNumber(xjrUserVo.getName());
                        setStatus(1);
                        setExecutiveStatus(1);
                        setSendMessage(dto.getSendMessage());
                    }});
                }
            }else if(materialAssignUserDto.getUserType() == 2){
                List<XjrUserVo> xjrUserVos = xjrUserService.selectJoinList(XjrUserVo.class,
                    new MPJLambdaWrapper<XjrUser>()
                    .leftJoin(BaseStudentSchoolRoll.class, BaseStudentSchoolRoll::getUserId, XjrUser::getId)
                    .eq(BaseStudentSchoolRoll::getClassId, materialAssignUserDto.getUserId())
                    .eq(XjrUser::getDeleteMark, DeleteMark.NODELETE.getCode())
                );
                for (XjrUserVo xjrUserVo : xjrUserVos) {
                    materialTaskMaterialTaskAssignMapper.insert(new MaterialTaskAssign(){{
                        setMaterialTaskId(materialTask.getId());
                        setUserId(xjrUserVo.getId());
                        setUserType(materialAssignUserDto.getUserType());
                        setUserNumber(xjrUserVo.getName());
                        setStatus(1);
                        setExecutiveStatus(1);
                        setSendMessage(dto.getSendMessage());
                    }});
                }
            }else if(materialAssignUserDto.getUserType() == 3){
                materialTaskMaterialTaskAssignMapper.insert(new MaterialTaskAssign(){{
                    setMaterialTaskId(materialTask.getId());
                    setUserId(materialAssignUserDto.getUserId());
                    setUserType(materialAssignUserDto.getUserType());
                    setUserNumber(materialAssignUserDto.getUserNumber());
                    setStatus(1);
                    setExecutiveStatus(1);
                    setSendMessage(dto.getSendMessage());
                }});
            }
        }

//        materialTaskMaterialTaskMapper.insert(materialTask);
//        if (materialTask.getMaterialTaskAssignList() != null && materialTask.getMaterialTaskAssignList().size() > 0) {
//            for (MaterialTaskAssign materialTaskAssign : materialTask.getMaterialTaskAssignList()) {
//                materialTaskAssign.setMaterialTaskId(materialTask.getId());
//                materialTaskMaterialTaskAssignMapper.insert(materialTaskAssign);
//            }
//        }
//        for (MaterialTaskAppendix materialTaskAppendix : materialTask.getMaterialTaskAppendixList()) {
//            materialTaskAppendix.setMaterialTaskId(materialTask.getId());
//            materialTaskMaterialTaskAppendixMapper.insert(materialTaskAppendix);
//        }

        return materialTask.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(MaterialTask materialTask) {
        materialTaskMaterialTaskMapper.updateById(materialTask);
        //********************************* MaterialTaskAssign  增删改  开始 *******************************************/
        {
            if (materialTask.getMaterialTaskAssignList() != null && materialTask.getMaterialTaskAssignList().size() > 0) {
                // 查出所有子级的id
                List<MaterialTaskAssign> materialTaskAssignList = materialTaskMaterialTaskAssignMapper.selectList(Wrappers.lambdaQuery(MaterialTaskAssign.class).eq(MaterialTaskAssign::getMaterialTaskId, materialTask.getId()).select(MaterialTaskAssign::getId));
                List<Long> materialTaskAssignIds = materialTaskAssignList.stream().map(MaterialTaskAssign::getId).collect(Collectors.toList());
                //原有子表单 没有被删除的主键
                List<Long> materialTaskAssignOldIds = materialTask.getMaterialTaskAssignList().stream().map(MaterialTaskAssign::getId).filter(Objects::nonNull).collect(Collectors.toList());
                //找到需要删除的id
                List<Long> materialTaskAssignRemoveIds = materialTaskAssignIds.stream().filter(item -> !materialTaskAssignOldIds.contains(item)).collect(Collectors.toList());

                for (MaterialTaskAssign materialTaskAssign : materialTask.getMaterialTaskAssignList()) {
                    //如果不等于空则修改
                    if (materialTaskAssign.getId() != null) {
                        materialTaskMaterialTaskAssignMapper.updateById(materialTaskAssign);
                    }
                    //如果等于空 则新增
                    else {
                        //已经不存在的id 删除
                        materialTaskAssign.setMaterialTaskId(materialTask.getId());
                        materialTaskMaterialTaskAssignMapper.insert(materialTaskAssign);
                    }
                }
                //已经不存在的id 删除
                if (materialTaskAssignRemoveIds.size() > 0) {
                    materialTaskMaterialTaskAssignMapper.deleteBatchIds(materialTaskAssignRemoveIds);
                }
            }
        }
        //********************************* MaterialTaskAssign  增删改  结束 *******************************************/

        //********************************* MaterialTaskAppendix  增删改  开始 *******************************************/
        {
            // 查出所有子级的id
            List<MaterialTaskAppendix> materialTaskAppendixList = materialTaskMaterialTaskAppendixMapper.selectList(Wrappers.lambdaQuery(MaterialTaskAppendix.class).eq(MaterialTaskAppendix::getMaterialTaskId, materialTask.getId()).select(MaterialTaskAppendix::getId));
            List<Long> materialTaskAppendixIds = materialTaskAppendixList.stream().map(MaterialTaskAppendix::getId).collect(Collectors.toList());
            //原有子表单 没有被删除的主键
            List<Long> materialTaskAppendixOldIds = materialTask.getMaterialTaskAppendixList().stream().map(MaterialTaskAppendix::getId).filter(Objects::nonNull).collect(Collectors.toList());
            //找到需要删除的id
            List<Long> materialTaskAppendixRemoveIds = materialTaskAppendixIds.stream().filter(item -> !materialTaskAppendixOldIds.contains(item)).collect(Collectors.toList());

            for (MaterialTaskAppendix materialTaskAppendix : materialTask.getMaterialTaskAppendixList()) {
                //如果不等于空则修改
                if (materialTaskAppendix.getId() != null) {
                    materialTaskMaterialTaskAppendixMapper.updateById(materialTaskAppendix);
                }
                //如果等于空 则新增
                else {
                    //已经不存在的id 删除
                    materialTaskAppendix.setMaterialTaskId(materialTask.getId());
                    materialTaskMaterialTaskAppendixMapper.insert(materialTaskAppendix);
                }
            }
            //已经不存在的id 删除
            if (materialTaskAppendixRemoveIds.size() > 0) {
                materialTaskMaterialTaskAppendixMapper.deleteBatchIds(materialTaskAppendixRemoveIds);
            }
        }
        //********************************* MaterialTaskAppendix  增删改  结束 *******************************************/

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(List<Long> ids) {
        materialTaskMaterialTaskMapper.deleteBatchIds(ids);
        materialTaskMaterialTaskAssignMapper.delete(Wrappers.lambdaQuery(MaterialTaskAssign.class).in(MaterialTaskAssign::getMaterialTaskId, ids));
        materialTaskMaterialTaskAppendixMapper.delete(Wrappers.lambdaQuery(MaterialTaskAppendix.class).in(MaterialTaskAppendix::getMaterialTaskId, ids));

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submited(MaterialTaskAssignAppendixDto materialTaskAssignAppendixDto) {

        if(materialTaskAssignAppendixDto.getMaterialCategory().equals(MaterialCategoryEnum.MT0001.getCode())){
            if (materialTaskAssignAppendixDto.getMaterialTaskAppendixList() != null && materialTaskAssignAppendixDto.getMaterialTaskAppendixList().size() > 0) {

                MaterialTaskAssign materialTaskAssign = materialTaskMaterialTaskAssignMapper.selectByIdDeep(materialTaskAssignAppendixDto.getMaterialTaskAssignId());
                Integer oldStatus = materialTaskAssign.getStatus();
                materialTaskAssign.setStatus(2);

                if (oldStatus == 1) {
                    MaterialTask materialTask = materialTaskMaterialTaskMapper.selectByIdDeep(materialTaskAssign.getMaterialTaskId());

                    LocalDate requiredCompleteTime = materialTask.getRequiredCompleteTime();
                    LocalDate today = LocalDate.now();

                    int comparison = today.compareTo(requiredCompleteTime);
                    if (comparison > 0) {//超时提交
                        materialTaskAssign.setExecutiveStatus(3);
                        materialTask.setTimeDelay(materialTask.getTimeDelay() + 1);
                    } else {//准时提交（包含当天）
                        materialTaskAssign.setExecutiveStatus(2);
                        materialTask.setOnTimePerson(materialTask.getOnTimePerson() + 1);
                    }
                    materialTask.setSubmitedPerson(materialTask.getSubmitedPerson() + 1);
                    materialTaskMaterialTaskMapper.updateById(materialTask);
                }
                materialTaskMaterialTaskAssignMapper.updateById(materialTaskAssign);

                if (oldStatus == 4) { // 重做删除原来的
                    materialTaskMaterialTaskAppendixMapper.delete(Wrappers.lambdaQuery(MaterialTaskAppendix.class).eq(MaterialTaskAppendix::getMaterialTaskId, materialTaskAssignAppendixDto.getMaterialTaskAssignId()));
                }

                for (AddMaterialTaskAppendixDto materialTaskAppendixDto : materialTaskAssignAppendixDto.getMaterialTaskAppendixList()) {
                    MaterialTaskAppendix materialTaskAppendix = new MaterialTaskAppendix();
                    materialTaskAppendix.setMaterialTaskId(materialTaskAssignAppendixDto.getMaterialTaskAssignId());
                    materialTaskAppendix.setFileId(materialTaskAppendixDto.getFileId());
                    materialTaskMaterialTaskAppendixMapper.insert(materialTaskAppendix);
                }
            }
        }

        if(materialTaskAssignAppendixDto.getMaterialCategory().equals(MaterialCategoryEnum.MT0002.getCode())){
            if (materialTaskAssignAppendixDto.getTemplateId() != null && !materialTaskAssignAppendixDto.getFormData().isEmpty()) {

                MaterialTaskAssign materialTaskAssign = materialTaskMaterialTaskAssignMapper.selectByIdDeep(materialTaskAssignAppendixDto.getMaterialTaskAssignId());
                Integer oldStatus = materialTaskAssign.getStatus();
                materialTaskAssign.setStatus(2);

                //状态 1=未提交 2=待审核 3=通过 4=重做
                if (oldStatus == 1) {
                    MaterialTask materialTask = materialTaskMaterialTaskMapper.selectByIdDeep(materialTaskAssign.getMaterialTaskId());

                    LocalDate requiredCompleteTime = materialTask.getRequiredCompleteTime();
                    LocalDate today = LocalDate.now();

                    int comparison = today.compareTo(requiredCompleteTime);
                    if (comparison > 0) {//超时提交
                        materialTaskAssign.setExecutiveStatus(3);
                        materialTask.setTimeDelay(materialTask.getTimeDelay() + 1);
                    } else {//准时提交（包含当天）
                        materialTaskAssign.setExecutiveStatus(2);
                        materialTask.setOnTimePerson(materialTask.getOnTimePerson() + 1);
                    }
                    materialTask.setSubmitedPerson(materialTask.getSubmitedPerson() + 1);
                    materialTaskMaterialTaskMapper.updateById(materialTask);
                }
                materialTaskMaterialTaskAssignMapper.updateById(materialTaskAssign);

                if (oldStatus == 4) { // 重做删除原来的
                    materialTaskMaterialTaskAppendixMapper.delete(Wrappers.lambdaQuery(MaterialTaskAppendix.class).eq(MaterialTaskAppendix::getMaterialTaskId, materialTaskAssignAppendixDto.getMaterialTaskAssignId()));
                }

                long id = formExecuteService.addByTemplateId(new FormExecuteAddOrUpdateDto(){{
                    setReleaseId(materialTaskAssignAppendixDto.getTemplateId());
                    setFormData(materialTaskAssignAppendixDto.getFormData());
                }});

                MaterialTaskAppendix materialTaskAppendix = new MaterialTaskAppendix();
                materialTaskAppendix.setMaterialTaskId(materialTaskAssignAppendixDto.getMaterialTaskAssignId());
                materialTaskAppendix.setFileId(id);
                materialTaskMaterialTaskAppendixMapper.insert(materialTaskAppendix);
            }
        }
        return true;
    }

    @Override
    public MaterialTaskVo getInfoById(Long id) {
        MPJLambdaWrapper<MaterialTask> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .orderByDesc(MaterialTask::getId)
                .eq(MaterialTask::getId, id)
                .select(MaterialTask::getId)
                .selectAs(MaterialType::getName, MaterialTaskVo::getMaterialTypeIdCn)
                .selectAs(MaterialType::getCreateDate, MaterialTaskVo::getCreateDate)
                .selectAs(DictionaryDetail::getName, MaterialTaskVo::getMaterialCategoryCn)
                .select(MaterialTask.class, x -> VoToColumnUtil.fieldsToColumns(MaterialTaskVo.class).contains(x.getProperty()))
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, MaterialTask::getMaterialCategory)
                .leftJoin(MaterialType.class, MaterialType::getId, MaterialTask::getMaterialTypeId);
        MaterialTaskVo materialTaskVo = this.selectJoinOne(MaterialTaskVo.class, queryWrapper);

        if (materialTaskVo == null) {
            return materialTaskVo;
        }

        MaterialTypeVo materialTypeVo = materialTypeService.getInfoById(materialTaskVo.getMaterialTypeId());
        if(materialTypeVo == null){
            return materialTaskVo;
        }
        if(materialTaskVo.getMaterialCategory().equals(MaterialCategoryEnum.MT0001.getCode())){
            materialTaskVo.setFolderId(materialTypeVo.getFolderId());
            materialTaskVo.setFolderIdCn(materialTypeVo.getFolderIdCn());
            materialTaskVo.setFiles(materialTypeVo.getFiles());
        }
        if(materialTaskVo.getMaterialCategory().equals(MaterialCategoryEnum.MT0002.getCode())){
            materialTaskVo.setFormReleaseId(materialTypeVo.getFormReleaseId());
            materialTaskVo.setFormReleaseIdCn(materialTypeVo.getFormReleaseIdCn());
        }


//        MPJLambdaWrapper<MaterialTaskAssign> materialTaskAssignMPJLambdaWrapper = new MPJLambdaWrapper<>();
//        materialTaskAssignMPJLambdaWrapper
//                .orderByDesc(MaterialTaskAssign::getCreateDate)
//                .eq(MaterialTaskAssign::getMaterialTaskId, id)
//                .select(MaterialTask::getId)
//                .selectAs(MaterialType::getName, MaterialTaskVo::getMaterialTypeIdCn)
//                .selectAs(MaterialType::getCreateDate, MaterialTaskVo::getCreateDate)
//                .selectAs(DictionaryDetail::getName, MaterialTaskVo::getMaterialCategoryCn)
//                .select(MaterialTask.class, x -> VoToColumnUtil.fieldsToColumns(MaterialTaskVo.class).contains(x.getProperty()))
//                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, MaterialTask::getMaterialCategory)
//                .leftJoin(MaterialType.class, MaterialType::getId, MaterialTask::getMaterialTypeId);
//        MaterialTaskVo materialTaskVo = materialTaskMaterialTaskAssignMapper.selectJoinOne(MaterialTaskVo.class, queryWrapper);

//        List<MaterialTaskAppendixVo> newsAppendixVoList = materialTaskAppendixService.selectJoinList(MaterialTaskAppendixVo.class,
//                new MPJLambdaWrapper<MaterialTaskAppendix>().eq(MaterialTaskAppendix::getMaterialTaskId, id)
//                        .select(MaterialTaskAppendix::getId)
//                        .select(MaterialTaskAppendix.class, x -> VoToColumnUtil.fieldsToColumns(MaterialTaskAppendixVo.class).contains(x.getProperty()))
//                        .leftJoin(File.class, File::getId, MaterialTaskAppendix::getFileId, ext -> ext.selectAs(File::getFileName, MaterialTaskAppendixVo::getFileName)
//                                .selectAs(File::getFolderId, MaterialTaskAppendixVo::getFolderId)
//                                .selectAs(File::getFileUrl, MaterialTaskAppendixVo::getFileUrl))
//        );
//
//        if (newsAppendixVoList.size() > 0) {
//            materialTaskVo.setMaterialTaskAppendixList(newsAppendixVoList);
//        }
        return materialTaskVo;
    }

    @Override
    public void sendMessage(Long materialTaskId) {
        MaterialTask materialTask = this.getById(materialTaskId);
        if(materialTask == null){
            return;
        }

        LambdaQueryWrapper<MaterialTaskAssign> materialTaskAssignLambdaQueryWrapper = new LambdaQueryWrapper<>();
        materialTaskAssignLambdaQueryWrapper
                .orderByDesc(MaterialTaskAssign::getCreateDate)
                .eq(MaterialTaskAssign::getMaterialTaskId, materialTaskId)
                .select(MaterialTaskAssign::getId)
                .select(MaterialTaskAssign.class, x -> VoToColumnUtil.fieldsToColumns(MaterialTaskAssign.class).contains(x.getProperty()));
        List<MaterialTaskAssign> materialTaskAssignList = materialTaskMaterialTaskAssignMapper.selectList(materialTaskAssignLambdaQueryWrapper);

        if(!materialTaskAssignList.isEmpty()){
            materialTask.setMaterialTaskAssignList(materialTaskAssignList);
        }

        CompletableFuture.runAsync(() -> {
            this.sendMessageUtil(materialTask);
        });
    }

    @Override
    public List<MaterialTaskAssignListVo> assignList(MaterialTaskAssignListDto dto) {

        MPJLambdaWrapper<MaterialTaskAssign> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .distinct()
                .like(dto.getName() != null && !dto.getName().equals(""), XjrUser::getName, dto.getName())
                .eq(MaterialTaskAssign::getMaterialTaskId, dto.getMaterialTaskId())
                .eq(dto.getState() != null, MaterialTaskAssign::getStatus, dto.getState())
                .select(MaterialTaskAssign::getId)
                .selectAs(XjrUser::getName, MaterialTaskAssignListVo::getName)
                .selectAs(MaterialTaskAppendix::getFileId, MaterialTaskAssignListVo::getFolderId)
                .selectAs(MaterialTaskAppendix::getFileId, MaterialTaskAssignListVo::getFormReleaseId)
                .select(MaterialTask::getMaterialCategory)
                .select(MaterialTaskAssign.class, x -> VoToColumnUtil.fieldsToColumns(MaterialTaskAssignListVo.class).contains(x.getProperty()))
                .innerJoin(XjrUser.class, XjrUser::getId, MaterialTaskAssign::getUserId, ext -> ext.selectAs(XjrUser::getUserName, MaterialTaskAssignListVo::getUserName))
                .leftJoin(UserDeptRelation.class, UserDeptRelation::getUserId, MaterialTypeAssign::getUserId)
                .leftJoin(Department.class, Department::getId, UserDeptRelation::getDeptId, ext -> ext.selectAs(Department::getName, MaterialTaskAssignListVo::getDeptName))
                .leftJoin(MaterialTask.class, MaterialTask::getId, MaterialTaskAssign::getMaterialTaskId)
                .leftJoin(MaterialTaskAppendix.class, MaterialTaskAppendix::getMaterialTaskId, MaterialTaskAssign::getId)
//                .select("(\n" +
//                        "SELECT id FROM xjr_file WHERE id IN (\n" +
//                        "SELECT file_id FROM material_task_appendix WHERE material_task_id = t.id\n" +
//                        ") ORDER BY create_date DESC LIMIT 1\n" +
//                        ") AS folderId ")
        ;

        List<MaterialTaskAssignListVo> pageOutput = materialTaskMaterialTaskAssignMapper.selectJoinList(MaterialTaskAssignListVo.class, queryWrapper);

        for (MaterialTaskAssignListVo materialTaskAssignListVo : pageOutput){
            materialTypeService.handleFileAndTemplate(materialTaskAssignListVo, materialTaskAssignListVo.getMaterialCategory());
        }

        return pageOutput;
    }

    private boolean sendMessageUtil(MaterialTask materialTask ) {
        IUserService userService = SpringUtil.getBean(IUserService.class);
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        List<User> userList = redisUtil.get(GlobalConstant.USER_CACHE_KEY, new TypeReference<List<User>>() {
        });
        //如果缓存中不存在用户信息，就直接去数据库查询,并保存到缓存中去
        if (userList.size() == 0) {
            userList = userService.list();
            redisUtil.set(GlobalConstant.USER_CACHE_KEY, userList);
        }

        if(materialTask.getCreateUserId() == null){
            return false;
        }

        User createUser = userList.stream().filter(u -> materialTask.getCreateUserId().equals(u.getId())).findFirst().orElse(new User());

        for (MaterialTaskAssign materialTaskAssign : materialTask.getMaterialTaskAssignList()) {

            User materialTaskAssignUser = userList.stream().filter(u -> materialTaskAssign.getUserId().equals(u.getId())).findFirst().orElse(new User());

            WeChatSendMessageDto weChatSendMessageDto = new WeChatSendMessageDto();
            weChatSendMessageDto.setUserId(materialTaskAssignUser.getOpenId());
            weChatSendMessageDto.setTemplateId("qmpXORPM1Cocqn503Qa4OkNNQ0uxlj2-ed9m6uWO-v4");
            //weChatSendMessageDto.setUrl(StrUtil.format("{}/pages/message/notice/detail?id={}", commonPropertiesConfig.getDomainApp(), id));
            weChatSendMessageDto.setMsgId(materialTask.getId().toString());
            JSONObject data = new JSONObject();

            JSONObject data1 = new JSONObject();
            data1.put("value", materialTask.getName());
            data.put("thing23", data1);

            JSONObject data2 = new JSONObject();
            data2.put("value", createUser.getName());
            data.put("thing18", data2);

            JSONObject data3 = new JSONObject();
            data3.put("value", materialTask.getRequiredCompleteTime());
            data.put("time17", data3);
            weChatSendMessageDto.setContent(data);
            weChatService.sendTemplateMessage(weChatSendMessageDto);
        };
        return true;
    }
}
