package com.jfinal.club._admin.downloadrecord;

import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.DownloadRecord;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @descriptions: 下载记录
 * @author: lc
 * @date: 2022-03-05 16:38:30
 * @Version: 1.0
 **/
public class AdminDownloadRecordController extends BaseController {

    @Inject
    AdminDownloadRecordService service;

    /**
     * 列表
     */
    public void index() {

        Integer p = getInt("p", 1);
        Integer types = getInt("types", 0);
        String text = get("text", "");

        setAttr("types",types);
        setAttr("text",text);

        ArrayList<Object> param = new ArrayList<>();
        String sql = "  FROM ( " + "SELECT a.*,b.name,b.company,b.mobile,b.email,c.filename,c.filepath FROM download_record a INNER JOIN users b ON a.uid = b.id INNER JOIN files c ON a.fid = c.id AND a.`type` = 1 " + "UNION all " + "SELECT a.*,b.name,b.company,b.mobile,b.email,c.filename,c.filepath FROM download_record a INNER JOIN users b ON a.uid = b.id INNER JOIN ideas_files c ON a.fid = c.id AND a.`type` = 2 " + ") a  where 1=1 ";

        if(types > 0){
            sql += " and type = ? ";
            param.add(types);
        }

        if(StringUtils.isNotBlank(text)){
            sql += " and ( name like ? or company like ? or mobile like ? or email like ? or filename like ? )";
            param.add("%"+text+"%");
            param.add("%"+text+"%");
            param.add("%"+text+"%");
            param.add("%"+text+"%");
            param.add("%"+text+"%");
        }

        sql += " ORDER BY a.create_time desc ";

        Page<DownloadRecord> ideas = DownloadRecord.dao.paginate(p, getInt("s", 10), "select * ", sql, param.toArray());
        setAttr("page", ideas);

        render("index.html");
    }


    /**
     * 删除
     */
    public void delete() {
        boolean delete = DownloadRecord.dao.deleteById(getInt("id"));
        if (delete) {
            renderJson(Ret.ok("msg", "删除成功!"));
        } else {
            renderJson(Ret.fail("msg", "删除失败"));
        }
    }

    public void export(){
        try {
            List<DownloadRecord> list = DownloadRecord.dao.find("SELECT * FROM (\n" + "SELECT a.*,b.name,b.company,b.mobile,b.email,c.filename,c.filepath FROM download_record a INNER JOIN users b ON a.uid = b.id INNER JOIN files c ON a.fid = c.id AND a.`type` = 1\n" + "UNION all\n" + "SELECT a.*,b.name,b.company,b.mobile,b.email,c.filename,c.filepath FROM download_record a INNER JOIN users b ON a.uid = b.id INNER JOIN ideas_files c ON a.fid = c.id AND a.`type` = 2\n" + ") a ORDER BY a.create_time desc");

            //1生成excel 文件
            service.createExcel(list);
            //2 下载
            File file = new File(PathKit.getWebRootPath( ), "upload" + Cons.EXPORT_DOWNLOADRECORD);
            renderFile(file, URLEncoder.encode(file.getName( ), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
