package com.jfinal.club._admin.users;

import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Sample;
import com.jfinal.club.common.model.Users;
import com.jfinal.club.util.Cons;
import com.jfinal.kit.PathKit;
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.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Inject
    AdminUsersService usersService;

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

        String sql = " from users ";
        ArrayList<Object> param = new ArrayList<>();
        
        if(StringUtils.isNotBlank(text)){
            text = "%"+text+"%";
            sql += " where name like ? or company like ? or mobile like ? ";
            param.add(text);
            param.add(text);
            param.add(text);
        }
        sql += " order by create_time desc";

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

        setAttr("page", page);

        render("index.html");
    }

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

    public void save( ) {
        
        UploadFile filepath = getFile("filepath", Cons.IDEAS);
        Users model = getModel(Users.class);
        String path = null;

        model.setCreateTime(new Date());
        model.save();
        renderJson(Ret.ok("msg","添加成功!"));
    }

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

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

        model.update( );
        renderJson(Ret.ok("msg","修改成功!"));
    }

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


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

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


    public void export(){
        try {
            List<Users> list = Users.dao.find("select * from users order by create_time desc");;

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

}
