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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.pojo.plan.Record;
import com.tofly.pojo.plan.dto.RecordDto;
import com.tofly.xrtygis.commons.Constant;
import com.tofly.xrtygis.commons.system.FileTableEnum;
import com.tofly.xrtygis.custom.util.service.FileInfoService;
import com.tofly.xrtygis.custom.util.service.IFileInfoService;
import com.tofly.xrtygis.entity.TfViewPointManage;
import com.tofly.xrtygis.exception.BusinessException;
import com.tofly.xrtygis.service.TfViewPointManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.tofly.xrtygis.mapper.TfViewPointManageMapper;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 视点管理
 *
 * @author JF
 * @date Tue May 10 00:00:00 CST 2022
 */
@Service("tfViewPointManageService")
public class TfViewPointManageServiceImpl extends ServiceImpl<TfViewPointManageMapper, TfViewPointManage> implements TfViewPointManageService {

    @Autowired
    private IFileInfoService fileInfoService;

    @Value("${xrty.ftp.viewPointFilePath}")
    private String viewPointFilePath;

    @Override
    public void saveTfViewPointManage(List<MultipartFile> fileList, TfViewPointManage tfViewPointManage)  throws BusinessException {
        StringBuilder fileInfo = null;
        // 判断当前是否有文件,则存入返回文件id
        if (null != fileList && fileList.size() > 0) {
            fileInfo = fileInfoService.saveFileList(fileList, viewPointFilePath, FileTableEnum.TF_VIEW_POINT_MANAGE.getTableName());
        }
        // 根据文件id反射填充对象
        TfViewPointManage info = JSONObject.parseObject(JSON.toJSONString(tfViewPointManage), TfViewPointManage.class);
        if(fileInfo!=null){
            info.setPhoto(fileInfo.toString());
        }
        // 对象入库
        this.save(info);
    }

//    @Override
//    public void updateRecordInfo(List<MultipartFile> fileList, RecordDto parDto) throws BusinessException {
//        Record updateRecord = JSONObject.parseObject(JSON.toJSONString(parDto),Record.class);
//        List<Long> fileIdLongList = new ArrayList<>();
//        if (null != fileList && fileList.size() > 0) {
//            StringBuilder fileIdList = iFileInfoService.saveFileList(fileList, recordFilePath, FileTableEnum.TF_YWPN_RECORD_W.getTableName());
//            fileIdLongList = Arrays.stream(fileIdList.toString().split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
//                    .collect(Collectors.toList());
//        }
//        Record record = this.getById(parDto.getId());
//        String lostFileIds = record.getFileIds();
//        String fileIds = parDto.getFileIds();
//        List<Long> removeFileId = new ArrayList<>();
//
//        List<Long> fileIdLongs = new ArrayList<>();
//        if(!StringUtils.isEmpty(fileIds)){
//            fileIdLongs = Arrays.stream(fileIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
//                    .collect(Collectors.toList());
//        }
//        // 判断当前文件是否有更新
//        if(!StringUtils.isEmpty(lostFileIds)&&lostFileIds.equals(fileIds)){
//            List<Long> lostFileIdLongs = Arrays.stream(lostFileIds.split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
//                    .collect(Collectors.toList());
//            List<Long> finalFileIdLongs = fileIdLongs;
//            lostFileIdLongs.forEach(lost->{
//                if(!finalFileIdLongs.contains(lost)){
//                    removeFileId.add(lost);
//                }
//            });
//            // 删除对应文件数据
//            if(removeFileId.size()>0){
//                // 删除远程文件
//                iFileInfoService.removeFileList(removeFileId);
//            }
//            if(lostFileIdLongs.size()>0){
//                // 删除远程文件
//                iFileInfoService.removeFileList(lostFileIdLongs);
//            }
//
//        }
//        // 更新数据信息
//        fileIdLongList.addAll(fileIdLongs);
//        if(fileIdLongList.size()>0){
//            StringBuilder stringBuilder = new StringBuilder("");
//            fileIdLongList.forEach(info -> {
//                stringBuilder.append(info).append(Constant.CSV);
//            });
//            updateRecord.setFileIds(stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString());
//            this.updateById(updateRecord);
//            return;
//        }
//        this.updateById(updateRecord);
//    }

    @Override
    public void deleteTfViewPointManage(List<Long> idList) throws BusinessException {
        List<String> removeFileIdList = new ArrayList<>();
        idList.forEach(info ->{
            TfViewPointManage viewPointManage = this.getById(info);
            String filePaths = viewPointManage.getPhoto();
            if(StringUtils.isNotBlank(filePaths)){
                List<String> removePaths = Arrays.stream(filePaths.split(Constant.CSV)).map(String::trim)
                        .collect(Collectors.toList());
                removeFileIdList.addAll(removePaths);
            }
        });
        if(removeFileIdList.size()>0){
            // 删除远程文件
            fileInfoService.removeFileList(removeFileIdList);
        }
        this.removeByIds(idList);
    }
}
