package com.jfinal.club._admin.producttype;

import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Ideas;
import com.jfinal.club.common.model.Product;
import com.jfinal.club.common.model.ProductType;
import com.jfinal.club.common.model.ProductType;
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 AdminProductTypeController 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);

        String sql = " from product_type  where 1=1 ";

        ArrayList<Object> param = new ArrayList<>();
        if(types > 0){
            sql += " and pid = ? ";
            param.add(types);
        }

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

        sql += " order by sort desc,create_time desc ";

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

        set("types",types);
        render("index.html");
    }

    public void add( ) {
        keepPara("p");
        render("add_edit.html");
    }

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

    public void edit( ) {
        keepPara("p");
        Integer id = getInt("id");
        ProductType productType = ProductType.dao.findById(id);
        setAttr("productType",productType);
        render("add_edit.html");
    }

    public void update( ) {
        UploadFile filepath = getFile("filepath", Cons.IDEASTYPE);
        ProductType model = getModel(ProductType.class);
        String path = null;
        if(filepath != null){
            path = "/upload"+Cons.IDEASTYPE+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;
        }

        Product ideas = Product.dao.findFirst("select * from product where FIND_IN_SET(?,types) ", id);
        if(ideas != null){
            renderJson(Ret.fail("msg", "有数据引用不能删除!"));
            return;
        }

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

}
