package com.happy.service.impl;

import com.happy.Model.FileInfo;
import com.happy.Until.Func;
import com.happy.Until.ResponseUtil;
import com.happy.dao.FileInfoDao;
import com.happy.dto.IPage;
import com.happy.service.FileService;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service("FileService")
public class FileImplService implements FileService {

    @Resource
    public FileInfoDao fileDao;

    @Override
    public int insertFile(FileInfo file) {
        return fileDao.insertFile(file);
    }

    @Override
    public int updateFile(FileInfo file) {
        return fileDao.updateFile(file);
    }

    @Override
    public int delFile(String id) {
        return fileDao.delFile(id);
    }

    @Override
    public int delLinkFile(String linkId) {
        return fileDao.delLinkFile(linkId);
    }

    @Override
    public FileInfo getById(String id) {
        return fileDao.getById(id);
    }

    @Override
    public IPage<FileInfo> queryPage(String sqlx, int page, int rows) {
        IPage<FileInfo> iPage = new IPage();
        List<FileInfo> fileList = fileDao.queryPage(sqlx,page,rows);
        int total = fileDao.queryTotal(sqlx);
        iPage.setPageList(fileList);
        iPage.setPage(page);
        iPage.setTotalPage( (int)Math.ceil((double)total/rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<FileInfo> queryList(String sqlx) {
        return fileDao.queryList(sqlx);
    }

    @Override
    public List<FileInfo> queryListByLinkId (String linkId)
    {
        if (Func.checkNull(linkId))
            return new ArrayList<>();

        StringBuilder fileSqlx = new StringBuilder("");
        fileSqlx.append(" and link_id = '").append(linkId).append("'");
        List<FileInfo> fileInfoList = queryList(fileSqlx.toString());
        return fileInfoList;
    }

    @Override
    public FileInfo getOen(String sqlx){
        List<FileInfo> list = fileDao.queryList(sqlx);
        if(list!=null && list.size()>0) return list.get(0);
        return null;
    }

    @Override
    public void uploadImp(List<File> fileList, HttpServletRequest request) {
        String filepath=request.getServletContext().getRealPath("/download/");
        if(fileList!=null && fileList.size()>0 && filepath!=null){
            //遍历file
            for(File file : fileList){
                try {
                    File destFile = new File(filepath, file.getName());
                    // 获取图片大小
                    long size = file.length();
                    // 获取图片尺寸
                    BufferedImage sourceImg = null;
                    sourceImg = ImageIO.read(new FileInputStream(file));
                    System.out.println(String.format("%.1f",file.length()/1024.0));// 源图大小
                    System.out.println(sourceImg.getWidth()); // 源图宽度
                    System.out.println(sourceImg.getHeight()); // 源图高度
                    // 保存图片到指定位置
                    FileUtils.copyFile(file, destFile);
                    // 返回图片url路径
                    String url = "http://192.168.57.1:8080/zhotel/download/" + file.getName();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public int batchInsertFile(List<FileInfo> fileInfoList) {
        int i=0;
        for (FileInfo fileInfo : fileInfoList) {
            i=fileDao.insertFile(fileInfo);
        }
        return i;
    }
}
