package cn.ftebox.controller.admin;

import cn.ftebox.entity.Comment;
import cn.ftebox.entity.PageBean;
import cn.ftebox.service.CommentService;
import cn.ftebox.util.DateJsonValueProcessor;
import cn.ftebox.util.ResponseUtil;
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 java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论管理
 */
@Controller
@RequestMapping({"admin/comment"})
public class CommentAdminController {
    @Resource
    private CommentService commentService;

    /**
     * 评论列表
     */
    @RequestMapping({"/list.do"})
    public String list(@RequestParam(value = "page", required = false) String page, @RequestParam(value = "rows", required = false) String rows, @RequestParam(value = "state", required = false) String state, HttpServletResponse response) throws IOException {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        // 加载翻页，状态参数
        Map<String, Object> map = new HashMap<>();
        map.put("size", pageBean.getPageSize());
        map.put("start", pageBean.getStart());
        map.put("state", state);
        System.out.println(map);
        // 查询评论列表
        List<Comment> commentList = commentService.list(map);
        // 查询评论总数
        Long total = commentService.getTotal(map);
        // 封装成json返回
        JSONObject result = new JSONObject();
        JsonConfig config = new JsonConfig();
        config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(commentList, config);
        result.put("rows", jsonArray);
        result.put("total", total);
        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 id : idsStr) {
            commentService.delete(Integer.parseInt(id));
        }
        JSONObject result = new JSONObject();
        result.put("success", Boolean.TRUE);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 审核评论
     */
    @RequestMapping({"/review.do"})
    public String review(@RequestParam("ids") String ids, @RequestParam("state") String state, HttpServletResponse response) throws IOException {
        String[] idsStr = ids.split(",");
        Comment comment = new Comment();
        for (String id : idsStr) {
            comment.setId(Integer.parseInt(id));
            comment.setState(Integer.parseInt(state));
            commentService.update(comment);
        }
        JSONObject result = new JSONObject();
        result.put("success", Boolean.TRUE);
        ResponseUtil.write(response, result);
        return null;
    }

}
