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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.dispatch.dto.DispatchOrderDto;
import com.tofly.dispatch.dto.DispatchOrderReplyDto;
import com.tofly.dispatch.entity.DispatchOrder;
import com.tofly.dispatch.entity.DispatchOrderReply;
import com.tofly.dispatch.service.DispatchOrderReplyService;
import com.tofly.dispatch.service.FileService;
import com.tofly.dispatch.util.UploadFiles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.tofly.dispatch.mapper.DispatchOrderReplyMapper;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 指令回复
 *
 * @author admin
 * @date Fri Nov 11 00:00:00 CST 2022
 */
@Service("dispatchOrderReplyService")
public class DispatchOrderReplyServiceImpl extends ServiceImpl<DispatchOrderReplyMapper, DispatchOrderReply> implements DispatchOrderReplyService {
    @Autowired
    private FileService fileService;
    @Value("${dispatch.ftp.reply}")
    private String filePath;

    @Override
    public boolean OrderReply(DispatchOrderReplyDto dispatchOrderReplyDto, List<MultipartFile> fileList, boolean isAdd) {
        if(isAdd){
            return InsertOrder(dispatchOrderReplyDto,fileList);
        }else{
            return UpdateOrder(dispatchOrderReplyDto,fileList);
        }
    }

    public  boolean InsertOrder(DispatchOrderReplyDto dispatchOrderReplyDto, List<MultipartFile> fileList){
        DispatchOrderReply data= new DispatchOrderReply();
        String fileIds = UploadFiles.saveFiles(fileService, fileList, dispatchOrderReplyDto.getUploadFileids(), dispatchOrderReplyDto.getUploadFileids(),
                filePath, "TF_DIS_DISPATCH_ORDER_REPLY_W");
        if (!StringUtils.isEmpty(fileIds)) {
            dispatchOrderReplyDto.setUploadFileids(fileIds);
        }
        BeanUtils.copyProperties(dispatchOrderReplyDto, data);

        return this.save(data);

    }
    public  boolean UpdateOrder(DispatchOrderReplyDto dispatchOrderReplyDto, List<MultipartFile> fileList){
        DispatchOrderReply data=this.getById(dispatchOrderReplyDto.getId());
        if(data==null){
            throw new RuntimeException("修改参数错误");
        }

        String fileIds=data.getUploadFileids();
        if(StringUtil.isNotEmpty(dispatchOrderReplyDto.getUploadFileids())){
            fileIds=dispatchOrderReplyDto.getUploadFileids();
            if(fileIds.equals("[]")){
                fileIds="";
            }
        }

        String fileIdsList = UploadFiles.saveFiles(fileService, fileList, data.getUploadFileids(), fileIds,
                filePath, "TF_DIS_DISPATCH_ORDER_W");

        dispatchOrderReplyDto.setUploadFileids(fileIdsList);
        DispatchOrderReply order=new DispatchOrderReply();
        BeanUtils.copyProperties(dispatchOrderReplyDto, order);

        return this.updateById(order);

    }


}
