package com.jfinal.club._admin.recommend;

import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Files;
import com.jfinal.club.common.model.Ideas;
import com.jfinal.club.common.model.Recommend;
import com.jfinal.club.common.model.Recommend;
import com.jfinal.club.util.CommonUtil;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.PropKit;
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.*;

/**
 * @descriptions: 推荐信息
 * @author: lc
 * @date: 2020-05-11 11:38:30
 * @Version: 1.0
 **/
@Log4j
public class AdminRecommendController extends BaseController {

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

        Integer pid = getInt("pid", 0);
        keepPara("text","pid");
        Integer p = getInt("p", 1);
        if(p <= 0){
            p = 1;
        }
        
        String text = get("text","");
        set("text",text);

        String sql = " from recommend where 1=1 ";
        ArrayList<Object> param = new ArrayList<>();

        if(StringUtils.isNotBlank(text)){
            text = "%"+text+"%";
            sql += " and title like ? ";
            param.add(text);
        }
        if(pid > 0){
            sql += " and pid = ? ";
            param.add(pid);
        }
        sql += " order by sort desc,create_time desc";

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

        setAttr("page", page);
        setAttr("ideas", Ideas.dao.findById(pid));

        render("index.html");
    }

    public void add( ) {
        keepPara("p","pid");
        setAttr("ideas", Ideas.dao.findById(getInt("pid")));
        set("productTypeList",Recommend.dao.find("select * from recommend order by sort desc,create_time desc"));
        render("add_edit.html");
    }

    public void save( ) {
        
        UploadFile filepath = getFile("filepath", Cons.IDEAS);
        Recommend model = getModel(Recommend.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( ) {
        setAttr("ideas", Ideas.dao.findById(getInt("pid")));
        keepPara("p","pid");
        Integer id = getInt("id");
        Recommend recommend = Recommend.dao.findById(id);
        setAttr("recommend",recommend);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("s","f");
        render("add_edit.html");
    }

    public void update( ) {
        UploadFile filepath = getFile("filepath", Cons.IDEAS);
        Recommend model = getModel(Recommend.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 = Recommend.dao.deleteById(id);
        if (delete) {
            renderJson(Ret.ok("msg", "删除成功!"));
        } else {
            renderJson(Ret.fail("msg", "删除失败"));
        }
    }


}
