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.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 sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
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);

        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("");
            ImageIcon iv=new ImageIcon("");
            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 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()];
            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;
        }
    }

}
