package com.jfinal.club._admin.sample;

import com.jfinal.aop.Inject;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Product;
import com.jfinal.club.common.model.ProductType;
import com.jfinal.club.common.model.Sample;
import com.jfinal.club.common.model.Sample;
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.Arrays;
import java.util.Date;
import java.util.List;

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

    @Inject
    AdminSampleService sampleService;

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

        Page< Sample > page = Sample.dao.paginate(p, getInt("s",10), "select * ", "from sample where is_submit = 1 order by create_time desc");

        setAttr("page", page);

        render("index.html");
    }


    public void product_list(){

        Integer id = getInt("id", 0);

        Sample sample = Sample.dao.findById(id);
        List<Product> productList = null;

        List<ProductType> productTypes = ProductType.dao.find("select * from product_type order by sort desc,create_time desc");
        if(sample != null && StringUtils.isNotBlank(sample.getProductIds())){
            productList = Product.dao.find("select * from product where id in (" + sample.getProductIds() + ") ");
            productList.forEach(a->{
                if(StringUtils.isNotBlank(a.getTypes())){
                    String[] split = a.getTypes().split(",");

                    String tempStr = "";

                    for (ProductType ps : productTypes) {
                        for (int i = 0; i < split.length; i++) {
                            String s = split[i];
                            if(ps.getId().toString().equals(s)){
                                tempStr += ps.getTitle()+"、";
                            }

                        }
                    }

                    a.put("areaApp",tempStr);

                }
            });
        }
        set("productList",productList);
        render("product_detail.html");
    }

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

    /**
     * 数据导出  所有
     * all 1 导出所有 ，0导出选中
     * ids 选中的id
     */
    public void export(){
        String all = get("all", "1");
        String ids = get("ids", "");
        try {
            List<Sample> list = null;

            if("1".equals(all)){
                list = Sample.dao.find("select * from sample order by create_time desc");
            }

            if("0".equals(all) && StringUtils.isNotBlank(ids)){
                list = Sample.dao.find("select * from sample where id in ("+ids+") order by create_time desc");
            }
            //1生成excel 文件
            sampleService.createExcel(list);
            //2 下载
            File file = new File(PathKit.getWebRootPath( ), "upload" + Cons.EXPORT_SAMPLE);
            renderFile(file, URLEncoder.encode(file.getName( ), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
