Newer
Older
PgInterface / src / main / java / com / casic / PgInterface / reservePlan / manager / PgReservePlanManager.java
T440 on 31 May 2019 5 KB 添加定位设备对接
package com.casic.PgInterface.reservePlan.manager;

import com.casic.PgInterface.core.hibernate.HibernateEntityDao;
import com.casic.PgInterface.devTable.domain.PgAlarm;
import com.casic.PgInterface.reservePlan.domain.PgReservePlan;
import com.casic.PgInterface.reservePlan.dto.PgReservePlanDto;
import org.apache.commons.codec.binary.Base64;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
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 javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yxw on 2017/11/8.
 */
@Service
public class PgReservePlanManager extends HibernateEntityDao<PgReservePlan> {

    public List<PgReservePlanDto> getPgReservePlanDto() {
        Criteria criteria=createCriteria(PgReservePlan.class);
        criteria.add(Restrictions.eq("active",1));

        List<PgReservePlan> pgReservePlanList=criteria.list();
        List<PgReservePlanDto> pgReservePlanDtoList = new ArrayList<PgReservePlanDto>();
        for (PgReservePlan pgReservePlan : pgReservePlanList) {
            pgReservePlanDtoList.add(new PgReservePlanDto(pgReservePlan));
        }
        return pgReservePlanDtoList;
    }

    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 = Base64.decodeBase64(fileBuffer.getBytes());

            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 List<PgReservePlanDto> getReservePlanDtoByAlarmType(PgAlarm pgAlarm){
        try {
            Criteria criteria = createCriteria(PgReservePlan.class);
            criteria.add(Restrictions.eq("pgAlarmTypeId", pgAlarm.getAlarmTypeId()));
            criteria.add(Restrictions.eq("active", 1));
            criteria.addOrder(Order.desc("id"));

            List<PgReservePlan> pgReservePlanList = criteria.list();
            List<PgReservePlanDto> pgReservePlanDtoList = new ArrayList<>();
            for (PgReservePlan pgReservePlan : pgReservePlanList) {
                pgReservePlanDtoList.add(new PgReservePlanDto(pgReservePlan));
            }

            return pgReservePlanDtoList;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public boolean getPgReservePlanByFileName(String planName){
        String sql="select count(*) from PG_RESERVE_PLAN where PLAN_NAME = '" +planName+"'";

        Object object=this.getSession().createSQLQuery(sql).uniqueResult();

        if(object==null||String.valueOf(object).equals("0"))
            return false;
        else
            return true;
    }

    /*下载文件*/
    public  ResponseEntity<byte[]> downloadFile(PgReservePlan pgReservePlan) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String path = request.getSession().getServletContext().getRealPath("");
            String filePath = path + "\\"+pgReservePlan.getPlanFile();
            File file = new File(filePath);//找到具体的文件
            byte[] body = null;
            InputStream inputStream = new FileInputStream(file);
            body = new byte[inputStream.available()];
//            body=readInputStream(inputStream);
            inputStream.read(body);

            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Disposition",file.getName());
            HttpStatus status = HttpStatus.OK;
            ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, status);

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

    public byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len=inStream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);;
        }
//        inStream.close();
        return outStream.toByteArray();
    }

}