package cn.ftebox.controller.admin;

import cn.ftebox.entity.Blog;
import cn.ftebox.entity.PageBean;
import cn.ftebox.service.BlogService;
import cn.ftebox.util.DateJsonValueProcessor;
import cn.ftebox.util.ResponseUtil;
import cn.ftebox.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 博客信息管理
 */

@Controller
@RequestMapping({"/admin/blog"})
public class BlogAdminController {
    @Resource
    private BlogService blogService;

    /**
     * 保存一条博客信息
     */
    @RequestMapping({"/save.do"})
    public String save(Blog blog, HttpServletResponse response) throws IOException {
        int resultTotal = 0;
        if (blog.getId() == null){
            resultTotal = blogService.add(blog);
        }else {
            resultTotal = blogService.update(blog);
        }
        JSONObject result = new JSONObject();
        if (resultTotal > 0) {
            result.put("success", Boolean.valueOf(true));
        } else {
            result.put("success", Boolean.valueOf(false));
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 查询博客信息列表
     */
    @RequestMapping({"/list.do"})
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows,Blog blog,HttpServletResponse response) throws IOException {
        PageBean pageBean = new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map = new HashMap<>();
        map.put("start",pageBean.getStart());
        map.put("size",pageBean.getPageSize());
        map.put("title", StringUtil.formatLike(blog.getTitle()));
        // 分页查询博客信息列表
        List<Blog> list = blogService.list(map);
        // 获取共有多少条博客信息
        long total = blogService.getTotal(map);
        JSONObject result = new JSONObject();
        JsonConfig config = new JsonConfig();
        config.registerJsonValueProcessor(Date.class,new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(list,config);
        result.put("rows",jsonArray);
        result.put("total",total);
        ResponseUtil.write(response,result);
        return null;
    }

    /**
     * 根据主键查询一条博客信息
     */
    @RequestMapping({"/findById.do"})
    public String findById(@RequestParam("id")String id,HttpServletResponse response) throws IOException {
        Blog blog = blogService.findById(Integer.parseInt(id));
        JSONObject result = new JSONObject(blog);
        ResponseUtil.write(response,result);
        return null;
    }

    /**
     * 根据主键删除一条博客信息
     */
    @RequestMapping({"/delete.do"})
    public String delete(@RequestParam("ids")String ids,HttpServletResponse response) throws IOException {
        String[] idsStr = ids.split(",");
        for (String i : idsStr) {
            blogService.delete(Integer.parseInt(i));
        }
        JSONObject result = new JSONObject();
        result.put("success",Boolean.valueOf(true));
        ResponseUtil.write(response,result);
        return null;
    }
}
