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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.api.UserClent;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.FileTableEnum;
import com.tofly.gpsboot.exception.BusinessException;
import com.tofly.gpsboot.mapper.TfYwpnInspectdevicetempWMapper;
import com.tofly.gpsboot.service.comm.IFileInfoService;
import com.tofly.gpsboot.service.paln.TfYwpnInspectdevicetempWService;
import com.tofly.pojo.User;
import com.tofly.pojo.plan.TfYwpnInspectdevicetempW;
import com.tofly.pojo.plan.vo.TfYwpnInspectdevicetempWVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
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 JF
 * @date Tue Sep 10 00:00:00 CST 2024
 */
@Service("tfYwpnInspectdevicetempWService")
public class TfYwpnInspectdevicetempWServiceImpl extends ServiceImpl<TfYwpnInspectdevicetempWMapper, TfYwpnInspectdevicetempW> implements TfYwpnInspectdevicetempWService {

    @Value("${xrty.ftp.inspectdevicetemp}")
    private String inspectdevicetempPath;
    @Autowired
    private IFileInfoService iFileInfoService;
    @Autowired
    private UserClent userClent;

    @Override
    public void savaInfo(List<MultipartFile> fileList, TfYwpnInspectdevicetempW entity) throws BusinessException {
        StringBuilder fileIdList = new StringBuilder("");
        // 判断当前是否有文件,则存入返回文件id
        if (null != fileList && fileList.size() > 0) {
            fileIdList = iFileInfoService.saveFileList(fileList, inspectdevicetempPath, FileTableEnum.TF_YWPN_RECORD_W.getTableName());
        }
        // 根据文件id反射填充对象
        TfYwpnInspectdevicetempW info = JSONObject.parseObject(JSON.toJSONString(entity), TfYwpnInspectdevicetempW.class);
        info.setAnnex(fileIdList.toString());
        // 对象入库
        this.save(info);
    }

    @Override
    public void updateInfo(List<MultipartFile> fileList, TfYwpnInspectdevicetempW entity) throws BusinessException {
        TfYwpnInspectdevicetempW updateInspectdevicetemp = JSONObject.parseObject(JSON.toJSONString(entity),TfYwpnInspectdevicetempW.class);
        List<Long> fileIdLongList = new ArrayList<>();
        if (null != fileList && fileList.size() > 0) {
            StringBuilder fileIdList = iFileInfoService.saveFileList(fileList, inspectdevicetempPath, FileTableEnum.TF_YWPN_RECORD_W.getTableName());
            fileIdLongList = Arrays.stream(fileIdList.toString().split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                    .collect(Collectors.toList());
        }
        TfYwpnInspectdevicetempW inspectdevicetemp = this.getById(entity.getId());
        String lostFileIds = inspectdevicetemp.getAnnex();
        String fileIds = entity.getAnnex();
        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);
            });
            updateInspectdevicetemp.setAnnex(stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString());
            this.updateById(updateInspectdevicetemp);
            return;
        }
        this.updateById(updateInspectdevicetemp);
    }

    @Override
    public List<TfYwpnInspectdevicetempWVo> viewConversion(List<TfYwpnInspectdevicetempW> infoList) {
        List<TfYwpnInspectdevicetempWVo> infoVoList = new ArrayList<>();
        infoList.forEach(info -> {
            TfYwpnInspectdevicetempWVo baseInfo = JSONObject.parseObject(JSON.toJSONString(info), TfYwpnInspectdevicetempWVo.class);
            infoVoList.add(baseInfo);
            Long createUser = info.getCreateUser();
            if (null != createUser && 0 != createUser) {
                User userAudit = userClent.getById(createUser).getResult();
                if(null!=userAudit){
                    baseInfo.setCreateUserName(userAudit.getRealName());
                }
            }
            // 文件路径
            if(!StringUtils.isEmpty(info.getAnnex())){
                baseInfo.setFilePathList(iFileInfoService.getFilePathList(Arrays.stream(info.getAnnex().split(Constant.CSV)).map(s -> Long.parseLong(s.trim()))
                        .collect(Collectors.toList())));
            }
        });
        return infoVoList;
    }
}
