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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.xrtygis.constant.Constant;
import com.tofly.xrtygis.entity.TfWaterInfo;
import com.tofly.xrtygis.entity.vo.FileInfo;
import com.tofly.xrtygis.mapper.TfWaterInfoMapper;
import com.tofly.xrtygis.service.IFileInfoService;
import com.tofly.xrtygis.service.TfWaterInfoService;
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.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 水厂信息表
 *
 * @author admin
 * @date Fri Mar 11 00:00:00 CST 2022
 */
@Service("tfWaterInfoService")
public class TfWaterInfoServiceImpl extends ServiceImpl<TfWaterInfoMapper, TfWaterInfo> implements TfWaterInfoService {

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

    @Autowired
    private IFileInfoService fileInfoService;

    @Override
    public boolean saveWaterInfo(List<MultipartFile> fileList, TfWaterInfo waterInfo) {
        if (fileList != null && fileList.size() != 0) {
            List<FileInfo> annexFileInfo = fileInfoService.saveFileInfoList(fileList, waterInfoPath, "TF_WATER_INFO");
            waterInfo.setAnnex(JSON.toJSONString(annexFileInfo));
        }
        // 对象入库
        this.save(waterInfo);
        return true;
    }

    @Override
    public boolean updateWaterInfo(List<MultipartFile> fileList, TfWaterInfo waterInfo) {
        TfWaterInfo query = this.getById(waterInfo.getId());
        String agoAnnexFileInfo = query.getAnnex();
        String newAnnex = fileInfoService.getNewAnnex(waterInfo.getAnnex(), fileList, agoAnnexFileInfo, waterInfoPath, "TF_WATER_INFO");
        waterInfo.setAnnex(newAnnex);
        return this.updateById(waterInfo);
    }

    @Override
    public boolean removeWaterInfoByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(Constant.CSV)).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        idList.forEach(info ->{
            TfWaterInfo carInsurance = this.getById(info);
            String annex = carInsurance.getAnnex();
            if (StringUtils.isNotBlank(annex)) {
                List<FileInfo> fileInfoList = JSON.parseArray(annex, FileInfo.class);
                fileInfoService.removeFileInfoList(fileInfoList);
            }
        });
        this.removeByIds(idList);
        return true;
    }
}
