package com.sys.bus.jx.controller;


import com.jfinal.core.Controller;
import com.jfinal.upload.UploadFile;
import com.sys.bus.jx.service.AttachmentService;
import com.sys.bus.model.Attachment;
import com.sys.bus.model.SysCode;
import com.sys.bus.syscode.service.SysCodeService;
import com.sys.core.model.DataBean;
import com.sys.core.utils.R;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.List;

/**
 * 附件控制器
 */
public class AttachmentController  extends Controller {

    static AttachmentService attachmentService = new AttachmentService();
    static SysCodeService codeService = new SysCodeService();
    public void toAttachmentListPage(){
        int size = 10;
        String exts = "pdf|xlsx|xls|jpg|jpeg|png|doc|docx";
        SysCode sysCode = codeService.getSysCodeName("附件大小(M)");
        if(sysCode!=null) {
            String syscodeValue = sysCode.getSyscodeValue();
            try {
                size = Integer.parseInt(syscodeValue);
            }catch (Exception e){}
        }
        sysCode = codeService.getSysCodeName("附件文件类型");
        if(sysCode!=null && StringUtils.isNotBlank(sysCode.getSyscodeValue())) {
            exts =  sysCode.getSyscodeValue();
        }
        this.setAttr("exts",exts);
        this.setAttr("size",size);
        this.setAttr("entryId",this.getPara("entryId"));
        this.setAttr("type",this.getPara("type"));
        this.render("attachmentList.html");
    }


    //获取附件数据
    public  void getAttachmentList(){
        //String kpiId = this.getPara("kpiId");
        String entryId = this.getPara("entryId");
        List<Attachment> attachmentList = attachmentService.getAttachmentList(entryId);
        this.renderJson(R.isOK("data", attachmentList) );
    }

    //上传附件
    public void uploadAttachment(){
       // String kpiId = this.getPara("kpiId");
        String entryId = this.getPara("entryId");
        //获取文件
        UploadFile file = this.getFile("file");
        String attachmentId = attachmentService.uploadAttachment(file, entryId);
        this.renderJson(R.isOK("attachmentId",attachmentId));
    }
    public void toFeedbackAttachmentListPage(){
        int size = 10;
        String exts = "pdf|xlsx|xls|txt|doc|docx|jpg|png|jpeg";

        this.setAttr("exts",exts);
        this.setAttr("size",size);
        this.setAttr("entryId",this.getPara("entryId"));
        this.setAttr("type",this.getPara("type"));
        this.render("feedbackAttachmentList.html");
    }


    //获取附件数据
    public  void getFeedbackAttachmentList(){
        String entryId = this.getPara("entryId");
        List<Attachment> attachmentList = attachmentService.getFeedbackAttachmentList(entryId);
        this.renderJson(R.isOK("data", attachmentList) );
    }

    //上传附件
    public void uploadFeedbackAttachment(){
        boolean jxjl = (boolean) this.getSession().getAttribute("jxjl");
        boolean admin = (boolean) this.getSession().getAttribute("admin");
        if (!jxjl && !admin) {
            DataBean dBean = new DataBean();
            dBean.setCode(-1);
            dBean.setMsg("您的权限不足，无法进行此操作！");
            this.renderJson(dBean);
            return;
        }
        String entryId = this.getPara("entryId");
        //获取文件
        UploadFile file = this.getFile("file");
        String attachmentId = attachmentService.uploadFeedbackAttachment(file, entryId);
        this.renderJson(R.isOK("attachmentId",attachmentId));
    }

    //上传附件
    public void delAttachment(){
        String attachmentId = this.getPara("attachmentId");
        attachmentService.delAttachment(attachmentId);
        this.renderJson(R.isOK());
    }



    public void downloadAttachment(){
        String attachmentId = this.getPara("attachmentId");
        Attachment attachment = Attachment.dao.findById(attachmentId);
        String attachmentPath = attachment.getAttachmentPath();
        String attachmentAutoname = attachment.getAttachmentAutoname();
        this.renderFile(new File(attachmentPath   + attachmentAutoname),attachment.getAttachmentName());
    }









}
