package com.sys.bus.jx.service;

import com.jfinal.upload.UploadFile;
import com.sys.bus.model.Attachment;
import com.sys.bus.model.Entry;
import com.sys.bus.model.Kpi;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.utils.Tool;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class AttachmentService {
    static SysCodeService sysCodeService =  new SysCodeService();


    public String uploadAttachment(UploadFile file,String entryId) {
        String fileName =  new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date())+ UUID.randomUUID().toString().replace("-","");
        int fileSize = (int)file.getFile().length()/1024;
        if(file.getFileName().length()>50){
            throw new RuntimeException("附件名称太长！请减少到50个字！");
        }
        String basePath = sysCodeService.getSysCodeName("附件路径").getSyscodeValue();
        //上传文件
        File upFile = new File(basePath + fileName);
        if (!upFile.getParentFile().exists()) {
            upFile.getParentFile().mkdirs();
        }

        file.getFile().renameTo(upFile);
        Attachment attachment = new Attachment();

        Entry entry = Entry.dao.findById(entryId);
        //保存到附件表中
        String uuid = Tool.uuid();
        attachment.setAttachmentId(uuid);
        attachment.setAttachmentAutoname(fileName);
        attachment.setAttachmentName(file.getFileName());
        attachment.setAttachmentPath(basePath);
        attachment.setAttachmenSize(fileSize);
//        attachment.setAttachmentKpiid(kpiId);
        attachment.setAttachmentEntryid(entryId);
        attachment.setAttachmentEntryno(entry.getEntryNo());

        attachment.save();
        return uuid;
    }

//    public String uploadAttachment(UploadFile file,String kpiId) {
//        String fileName =  new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date())+ UUID.randomUUID().toString().replace("-","");
//        int fileSize = (int)file.getFile().length()/1024;
//        String basePath = sysCodeService.getSysCodeName("附件路径").getSyscodeValue();
//        //上传文件
//        File upFile = new File(basePath + fileName);
//        if (!upFile.getParentFile().exists()) {
//            upFile.getParentFile().mkdirs();
//        }
//        file.getFile().renameTo(upFile);
//        Attachment attachment = new Attachment();
//        Kpi kpi = Kpi.dao.findById(kpiId);
//        String entryid = kpi.getKpiEntryid();
//        Entry entry = Entry.dao.findById(entryid);
//        if(file.getFileName().length()>50){
//            throw new RuntimeException("附件名称太长！请减少到50个字！");
//        }
//        //保存到附件表中
//        String uuid = Tool.uuid();
//        attachment.setAttachmentId(uuid);
//        attachment.setAttachmentAutoname(fileName);
//        attachment.setAttachmentName(file.getFileName());
//        attachment.setAttachmentPath(basePath);
//        attachment.setAttachmenSize(fileSize);
//        attachment.setAttachmentKpiid(kpiId);
//        attachment.setAttachmentEntryid(entryid);
//        attachment.setAttachmentEntryno(entry.getEntryNo());
//
//        attachment.save();
//        return uuid;
//    }

//    public List<Attachment> getAttachmentList(String kpiId){
//        Kpi kpi = Kpi.dao.findById(kpiId);
//        String sql = " select * from ct_jx_attachment   where attachment_kpiid in (select kpi_id from ct_jx_kpi where kpi_entryid = ? and kpi_index = ?) ";
//        return  Attachment.dao.find(sql,kpi.getKpiEntryid(),kpi.getKpiIndex());
//    }
    public List<Attachment> getAttachmentList(String entryId){
        String sql = " select * from ct_jx_attachment   where attachment_entryid =?";
        return  Attachment.dao.find(sql,  entryId);
    }
    public String uploadFeedbackAttachment(UploadFile file,String entryId) {
        String fileName =  new SimpleDateFormat("yyMMddHHmmssSSS").format(new Date())+ UUID.randomUUID().toString().replace("-","");
        int fileSize = (int)file.getFile().length()/1024;
        String basePath = sysCodeService.getSysCodeName("附件路径").getSyscodeValue();
        //上传文件
        File upFile = new File(basePath + fileName);
        if (!upFile.getParentFile().exists()) {
            upFile.getParentFile().mkdirs();
        }
        boolean suc = file.getFile().renameTo(upFile);
        if(!suc){
            throw new RuntimeException("附件保存失败！保存全路径："+basePath + fileName);
        }
        Attachment attachment = new Attachment();
       Entry entry = Entry.dao.findById(entryId);
        if(file.getFileName().length()>50){
            throw new RuntimeException("附件名称太长！请减少到50个字！");
        }
        //保存到附件表中
        String uuid = Tool.uuid();
        attachment.setAttachmentId(uuid);
        attachment.setAttachmentAutoname(fileName);
        attachment.setAttachmentName(file.getFileName());
        attachment.setAttachmentPath(basePath);
        attachment.setAttachmenSize(fileSize);
//        attachment.setAttachmentKpiid(kpiId);
        attachment.setAttachmentEntryfeedback(entryId);
        attachment.setAttachmentEntryno(entry.getEntryNo());

        attachment.save();
        return uuid;
    }

    public List<Attachment> getFeedbackAttachmentList(String entryId){
        String sql = " select * from ct_jx_attachment   where attachment_entryfeedback = ?";
        return  Attachment.dao.find(sql,entryId);
    }

    public  void delAttachment(String attachmentId) {
        Attachment att = Attachment.dao.findById(attachmentId);

        File file = new File(att.getAttachmentPath() + att.getAttachmentAutoname());
        if(file.exists()){
            file.delete();
        }
        att.delete();
    }

}
