package com.uas.finance.service.Impl;

import com.uas.dfs.service.FileClient;
import com.uas.finance.dao.AccountApplyAttachDao;
import com.uas.finance.model.AccountApplyAttach;
import com.uas.finance.service.AccountApplyAttachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 应收账款转让接口实现层
 * created by shicr on 2018/1/30
 **/
@Service
public class AccountApplyAttachServiceImpl implements AccountApplyAttachService {

    @Autowired
    private AccountApplyAttachDao dao;

    @Autowired
    private FileClient fileClient;

    /**
     * 应收账款转让附件保存
     *
     * @param file
     * @param attach
     * @return
     */
    @Override
    public AccountApplyAttach save(MultipartFile file, AccountApplyAttach attach) {
        try {
            String path = fileClient.upload(file.getBytes(), file.getSize(), "", null);
            String name = file.getOriginalFilename();
            Long size = file.getSize();
            attach.setName(name);
            attach.setSize(size);
            attach.setPath(path);
            attach.setAaid(attach.getAaid());
            attach.setType(attach.getType());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dao.save(attach);
    }

    /**
     * 查找某出账单下所以附件
     *
     * @param aaid
     * @return
     */
    @Override
    public List<AccountApplyAttach> findByAaid(Long aaid) {
        return dao.findByAaid(aaid);
    }

    /**
     * 删除附件
     *
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
         dao.delete(id);
    }

}
