package com.casic.PgInterface.construction.manager;

import com.casic.PgInterface.construction.domain.PgConstruction;
import com.casic.PgInterface.construction.domain.PgCsDocument;
import com.casic.PgInterface.construction.dto.PgCsDocumentDto;
import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by zxh on 2018/3/28.
 */
@Service
public class PgCsDocumentManager extends HibernateEntityDao<PgCsDocument>{

    private PgConstructionManager pgConstructionManager;

    @Resource
    public void setPgConstructionManager(PgConstructionManager pgConstructionManager) {
        this.pgConstructionManager = pgConstructionManager;
    }

    public  String saveFiles(String fileBuffer, String fileName, String fileType){

        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String strDirPath = request.getSession().getServletContext().getRealPath("");

            FileOutputStream fos = null;
            String image_toDir = strDirPath + "\\"+fileType;//存储路径
            String imageName = "";
            imageName += fileType+"/"+fileName;
            byte[] buffer = new BASE64Decoder().decodeBuffer(fileBuffer);//对android传过来的图片字符串进行解码
            File destDir = new File(image_toDir);
            if (!destDir.exists())
                destDir.mkdirs();
            File imageFile = new File(destDir, fileName);
            fos = new FileOutputStream(imageFile);//保存文件
            fos.write(buffer);
            fos.flush();
            fos.close();
            return imageName;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return "";
        }
    }

    /*下载文件*/
    public ResponseEntity<byte[]> downloadFile(PgCsDocument pgCsDocument) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String path = request.getSession().getServletContext().getRealPath("");
            String filePath = path + "\\"+pgCsDocument.getConstruct_doc();
            File file = new File(filePath);//找到具体的文件
            byte[] body = null;
            InputStream inputStream = new FileInputStream(file);
            body = new byte[inputStream.available()];
            inputStream.read(body);
            HttpHeaders headers = new HttpHeaders();
            /*MediaType mediaType = new MediaType("text", "html", Charset.forName("UTF-8"));*/
            headers.add("Content-Disposition",file.getName());
            /*headers.set("contentType","text/html;charset=utf-8");
            headers.setContentType(mediaType);*/
            HttpStatus status = HttpStatus.OK;
            ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, status);

            return entity;
            //inputStream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*查询*/
    public List<PgCsDocumentDto> getAllDocData(String cs_name ,String cs_type,String startTime,String endTime) {

        List<PgCsDocumentDto> pgCsDocumentDtoList = new ArrayList<PgCsDocumentDto>();
        try {
            List<PgConstruction> pgConstructionList = pgConstructionManager.getPgContructionByNameOrType(cs_name,
                    cs_type, null,startTime, endTime);
            if(pgConstructionList==null||pgConstructionList.size()==0)
                return null;
            else
            {
                for(PgConstruction pgConstruction : pgConstructionList)
                {
                    String sql="select * from PG_CSDOCUMENT where PGCONSTRUCTION_ID = " + pgConstruction.getId();
                    List<Object[]> objectList=this.getSession().createSQLQuery(sql).list();

                    for(Object[] objectArray : objectList)
                    {
                        pgCsDocumentDtoList.add(getDtoByObject(objectArray));
                    }
                }
                return pgCsDocumentDtoList;
            }
        } catch (Exception ex) {
            return null;
        }
    }

    private PgCsDocumentDto getDtoByObject(Object[] objectArray)
    {
        PgCsDocumentDto pgCsDocumentDto=new PgCsDocumentDto();

        pgCsDocumentDto.setId(String.valueOf(objectArray[0]));
        pgCsDocumentDto.setActive(String.valueOf(objectArray[1]));
        pgCsDocumentDto.setConstruct_doc(String.valueOf(objectArray[2]));
        pgCsDocumentDto.setDoc_name(String.valueOf(objectArray[3]));
        pgCsDocumentDto.setList_name(String.valueOf(objectArray[4]));

        return pgCsDocumentDto;
    }

}