package com.jfinal.club._admin.ideas;

import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.*;
import com.jfinal.club.common.model.Ideas;
import com.jfinal.club.util.CommonUtil;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * @descriptions:
 * @author: lc
 * @date: 2020-05-11 11:38:30
 * @Version: 1.0
 **/
@Log4j
public class AdminIdeasController extends BaseController {

    /**
     * 列表
     */
    public void index( ) {
        keepPara("text");
        Integer p = getInt("p", 1);
        if(p <= 0){
            p = 1;
        }
        String text = get("text","");
        set("text",text);
        Integer types = getInt("types",0);

        ArrayList<Object> param = new ArrayList<>();
        String sql = " FROM ( " + "SELECT a.*,b.title typeName FROM ideas a  LEFT JOIN ideas_type b ON a.`type` = b.id ORDER BY a.sort DESC,a.create_time DESC " + ") a where 1=1 ";

        if(types > 0){
            sql += " and a.type = ? ";
            param.add(types);
        }
        if(StringUtils.isNotBlank(text)){
            text = "%"+text+"%";
            sql += " and a.title like ? ";
            param.add(text);
        }


        Page< Ideas > ideas = Ideas.dao.paginate(p, getInt("s",10), "select * ", sql,param.toArray());
        setAttr("page", ideas);
        set("ideasTypeList",IdeasType.dao.find("select * from ideas_type order by sort desc,create_time desc"));
        set("types",types);


        render("index.html");
    }

    public void add( ) {

        keepPara("p");
        set("ideasTypeList",IdeasType.dao.find("select * from ideas_type order by sort desc,create_time desc"));
        render("add_edit.html");
    }

    public void save( ) {
        
        UploadFile filepath = getFile("filepath", Cons.IDEAS);
        Ideas model = getModel(Ideas.class);
        String path = null;
        if(filepath != null){
            path = "/upload"+Cons.IDEAS+filepath.getFileName();
            model.setCoverImg(path);
        }
        model.setCreateTime(new Date());
        model.save();
        renderJson(Ret.ok("msg","添加成功!"));
    }

    public void edit( ) {
        keepPara("p");
        Integer id = getInt("id");
        Ideas ideas = Ideas.dao.findById(id);
        setAttr("ideas",ideas);
        set("ideasTypeList",IdeasType.dao.find("select * from ideas_type order by sort desc,create_time desc"));
        render("add_edit.html");
    }

    public void update( ) {
        UploadFile filepath = getFile("filepath", Cons.IDEAS);
        Ideas model = getModel(Ideas.class);

        String path = null;
        if(filepath != null){
            path = "/upload"+Cons.IDEAS+filepath.getFileName();
            model.setCoverImg(path);
        }
        model.update( );
        renderJson(Ret.ok("msg","修改成功!"));
    }

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


    public void recommend_t(){
        Ideas.dao.findById(getInt("id")).set("is_recommend",1).update();
        index();
    }

    public void recommend_f(){
        Ideas.dao.findById(getInt("id")).set("is_recommend",0).update();
        index();
    }

    /**
     * 文件
     */
    public void files(){
        Integer pid = getInt("pid");
        Integer p = getInt("p", 1);
        if(p <= 0){
            p = 1;
        }
        keepPara("pid");
        Page<IdeasFiles> page = IdeasFiles.dao.paginate(p, 10, "select * ", " from ideas_files where pid = ? order by sort desc,create_time desc", pid);
        set("page",page);
        render("filelist.html");
    }

    public void files_add(){
        keepPara("pid");
        render("files_add.html");
    }

    public void files_edit(){
        Integer id = getInt("id");
        keepPara("pid");
        IdeasFiles files = IdeasFiles.dao.findById(id);
        set("files",files);
        render("files_edit.html");
    }

    public void files_save(){
        UploadFile file = getFile("filepath", Cons.FILES);
        Integer pid = getInt("pid", 0);
        Integer sort = getInt("sort", 0);

        if(file == null){
            renderJson(Ret.fail().set("msg","请上传文件"));
        }else{
            String filePath = CommonUtil.getFilePath(file, Cons.FILES);
            IdeasFiles f = new IdeasFiles();
            f.setFilename(file.getFileName());
            f.setCreateTime(new Date());
            f.setFilepath(filePath);
            f.setPid(pid);
            f.setSize(CommonUtil.formatFileUnit(file));
            f.setSort(sort);
            f.save();
            renderJson(Ret.ok());
        }
    }

    public void files_update(){
        UploadFile file = getFile("filepath", Cons.FILES);
        Integer id = getInt("id", 0);
        Integer sort = getInt("sort", 0);

        IdeasFiles f = IdeasFiles.dao.findById(id);
        if(file != null){
            String filePath = CommonUtil.getFilePath(file, Cons.FILES);
            f.setFilename(file.getFileName());
            f.setFilepath(filePath);
            f.setSize(CommonUtil.formatFileUnit(file));
        }
        f.setSort(sort);
        f.update();

        renderJson(Ret.ok());
    }

    public void files_delete(){
        Integer id = getInt("id", 0);
        IdeasFiles files = IdeasFiles.dao.findById(id);
        if(StringUtils.isNotBlank(files.getFilepath())){
            CommonUtil.deleteFile(files.getFilepath());
        }
        IdeasFiles.dao.deleteById(id);
        renderJson(Ret.ok());
    }


}
