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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.gpsboot.common.system.FileTableEnum;
import com.tofly.gpsboot.mapper.plan.FaultMapper;
import com.tofly.gpsboot.mapper.plan.PdamonitorMapper;
import com.tofly.gpsboot.service.comm.IFileInfoService;
import com.tofly.gpsboot.service.paln.FaultService;
import com.tofly.gpsboot.service.paln.PdamonitorService;
import com.tofly.pojo.plan.Fault;
import com.tofly.pojo.plan.Hydrantrecord;
import com.tofly.pojo.plan.Pdamonitor;
import com.tofly.pojo.plan.vo.PdamonitorVo;
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 org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 故障服务接口
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Service("faultService")
public class FaultServiceImpl extends ServiceImpl<FaultMapper, Fault> implements FaultService {

    @Value("${xrty.ftp.fault}")
    private String faultPath;
    @Autowired
    private IFileInfoService iFileInfoService;

    @Override
    public Page<Fault> getFaultPage(Page page, Fault fault) {
        return baseMapper.getFaultPage(page,fault);
    }

    @Override
    public boolean savaFaultInfo(List<MultipartFile> fileList, Fault fault) {
        // 判断当前是否有文件,则存入返回文件id
        if (null != fileList && fileList.size() > 0) {
            StringBuilder fileIdList = iFileInfoService.saveFileList(fileList, faultPath, FileTableEnum.TF_YWPN_FAULT_W.getTableName());
            // 根据文件id反射填充对象
            fault.setFileIds(fileIdList.toString());
        }
        // 对象入库
        return this.save(fault);
    }

    @Override
    public boolean updateFaultInfo(List<MultipartFile> fileList, Fault fault) {
        //查询本地是否有照片,如果有就根据路径删除照片文件,再进行修改
        String newFileIds = "";
        if (null != fileList && fileList.size() > 0) {
            newFileIds = iFileInfoService.saveFileList(fileList, faultPath,FileTableEnum.TF_YWPN_FAULT_W.getTableName()).toString();
        }
        List<String> nowFilePathList = fault.getFilePathList();
        System.out.println("新增文件id "+newFileIds+"保留文件路径 "+ JSON.toJSONString(nowFilePathList));
        String fileIds = iFileInfoService.updateFileList(nowFilePathList, newFileIds, FileTableEnum.TF_YWPN_FAULT_W.getTableName());
        if (StringUtils.isNotBlank(fileIds)) {
            fault.setFileIds(fileIds);
        }
        this.updateById(fault);
        return true;
    }
}
