/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.watermeter.config.FilePathBean;
import com.tofly.watermeter.custom.service.ReadDataCheckThreeService;
import com.tofly.watermeter.entity.File;
import com.tofly.watermeter.entity.Yhforeverinfo;
import com.tofly.watermeter.service.FileService;
import com.tofly.watermeter.service.YhforeverinfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.tofly.watermeter.mapper.YhforeverinfoMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户档案抄表永久备注及附件信息
 *
 * @author wangjieliu
 * @date Thu Jan 27 00:00:00 CST 2022
 */
@Service("yhforeverinfoService")
@AllArgsConstructor
public class YhforeverinfoServiceImpl extends ServiceImpl<YhforeverinfoMapper, Yhforeverinfo> implements YhforeverinfoService {

    private final FilePathBean pathBean;
    private final ReadDataCheckThreeService readDataCheckThreeService;
    private final FileService fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String clientInfo(Yhforeverinfo yhforeverinfo, List<MultipartFile> fileList) {
        String yhbh = yhforeverinfo.getYhbh();
        Yhforeverinfo queryYhInfo = this.getOne(Wrappers.<Yhforeverinfo>lambdaQuery().eq(Yhforeverinfo::getYhbh, yhbh));
        //修改后剩余的文件ids
        String fileId = yhforeverinfo.getFileId();
        String clientBz = yhforeverinfo.getClientBz();
        StringBuilder fileInfo = null;
        if (!CollectionUtils.isEmpty(fileList)){
            //保存文件信息
            fileInfo = readDataCheckThreeService.saveFileList(fileList, pathBean.getReadMeter(), "TF_YWPN_YHFOREVERINFO_W");
        }

        //新增
        if (StringUtil.isNull(queryYhInfo)) {
            if (fileInfo != null){
                yhforeverinfo.setFileId(fileInfo.toString());
            }
            this.save(yhforeverinfo);
        } else {
            //查询出来的文件ids
            String queryFileIds = queryYhInfo.getFileId();
            if (StringUtil.isNotEmpty(queryFileIds)) {
                List<String> queryFileIdList = Arrays.asList(queryFileIds.split(","));
                //如果传递的是null，则代表全部删除
                if (StringUtil.isEmpty(fileId)) {
                    List<String> removeFileList = fileService.list(Wrappers.<File>lambdaQuery().in(File::getId, queryFileIdList)
                            .select(File::getFilePath)).stream().map(File::getFilePath).collect(Collectors.toList());
                    //根据路径删除FTP服务器文件
                    removeFileList.forEach(FtpUtil::deleteFile);
                    //删除FILE表信息
                    fileService.removeByIds(queryFileIdList);
                } else {
                    //传递修改后的文件ids集合
                    List<String> transmitFileList = Arrays.asList(fileId.split(","));
                    //过滤得到需要删除的文件ids
                    List<String> removeFileidsList = queryFileIdList.stream().filter(query -> transmitFileList.stream()
                            .noneMatch(input -> query.equals(input))).collect(Collectors.toList());
                    if (removeFileidsList.size()>0){
                        List<String> removeList = fileService.list(Wrappers.<File>lambdaQuery().in(File::getId, removeFileidsList)
                                .select(File::getFilePath)).stream().map(File::getFilePath).collect(Collectors.toList());
                        //根据路径删除FTP服务器文件
                        removeList.forEach(FtpUtil::deleteFile);
                        //删除FILE表信息
                        fileService.removeByIds(removeFileidsList);
                    }
                }
                if (StringUtil.isNotEmpty(fileId)){
                    if (fileInfo != null){
                        yhforeverinfo.setFileId(fileInfo.append(fileId).toString());
                    }
                }else {
                    if (fileInfo != null){
                        yhforeverinfo.setFileId(fileInfo.toString());
                    }
                }
                this.update(Wrappers.<Yhforeverinfo>lambdaUpdate().eq(Yhforeverinfo::getYhbh , yhbh).set(StringUtil.isNotEmpty(clientBz) ,Yhforeverinfo::getClientBz,clientBz)
                        .set(StringUtil.isNotEmpty(yhforeverinfo.getFileId()),Yhforeverinfo::getFileId , yhforeverinfo.getFileId())
                        .set(StringUtil.isEmpty(yhforeverinfo.getFileId()),Yhforeverinfo::getFileId , ""));
            } else {
                if (fileInfo != null){
                    yhforeverinfo.setFileId(fileInfo.toString());
                }

                this.update(Wrappers.<Yhforeverinfo>lambdaUpdate().eq(Yhforeverinfo::getYhbh , yhbh).set(StringUtil.isNotEmpty(clientBz) ,Yhforeverinfo::getClientBz,clientBz)
                        .set(StringUtil.isNotEmpty(yhforeverinfo.getFileId()),Yhforeverinfo::getFileId , yhforeverinfo.getFileId())
                        .set(StringUtil.isEmpty(yhforeverinfo.getFileId()),Yhforeverinfo::getFileId , ""));
            }
        }
        return yhforeverinfo.getFileId();
    }

}
