package cn.ftebox.controller.admin;

import cn.ftebox.entity.BlogType;
import cn.ftebox.entity.PageBean;
import cn.ftebox.service.BlogService;
import cn.ftebox.service.BlogTypeService;
import cn.ftebox.util.ResponseUtil;
import net.sf.json.JSONArray;
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 java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 博客类型管理
 */
@Controller
@RequestMapping({"/admin/blogType"})
public class BlogTypeAdminController {
    @Resource
    private BlogTypeService blogTypeService;

    @Resource
    private BlogService blogService;

    /**
     * 博客类型列表
     */
    @RequestMapping({"/list.do"})
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, 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());
        // 查询博客类型列表
        List<BlogType> blogTypeList = blogTypeService.list(map);
        // 查询总共有多少条数据
        Long total = blogTypeService.getTotal(map);
        // 将数据写入response
        JSONObject result = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(blogTypeList);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 保存博客类别信息
     */
    @RequestMapping({"/save.do"})
    public String save(BlogType blogType, HttpServletResponse response) throws IOException {
        int resultTotal = 0;
        if (blogType.getId() == null) {
            // 添加
            resultTotal = blogTypeService.add(blogType);
        } else {
            // 更新
            resultTotal = blogTypeService.update(blogType);
        }

        // 将数据写入response
        JSONObject result = new JSONObject();
        if (resultTotal > 0) {
            result.put("success", Boolean.valueOf(true));
        } else {
            result.put("success", Boolean.valueOf(true));
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 删除博客类型
     */
    @RequestMapping({"/delete.do"})
    public String delete(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String[] idsStr = ids.split(",");
        JSONObject result = new JSONObject();
        for (String id : idsStr) {
            int sum = blogService.getBlogByTypeId(Integer.valueOf(id));
            System.out.println(sum);
            if (sum > 0) {
                result.put("exists", "删除失败！此博客类型下文章不为空！");
            } else {
                blogTypeService.delete(Integer.valueOf(id));
                result.put("success", Boolean.TRUE);
            }
        }
        ResponseUtil.write(response, result);
        return null;
    }

}
