package com.yihu.editor.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.editor.entity.SystemDictItem;
import com.yihu.editor.entity.YdfHealthEduArticle;
import com.yihu.editor.service.ArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/6/9 0009.
 */
@Controller
public class ArticleController extends BaseController{


    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String toIndex(){
        return "index";
    }

    @RequestMapping(value = "/toList",method = RequestMethod.GET)
    public String toList(){
        return "edit_list";
    }

    @RequestMapping(value = "/toAdd",method = RequestMethod.GET)
    public String toAdd(){
        return "add";
    }

    @RequestMapping(value = "/toEdit",method = RequestMethod.GET)
    public String toEdit(@RequestParam(value = "id")String id,ModelMap modelMap){
        modelMap.addAttribute("id", id);
        return "add";
    }

    /**
     * 初始化列表页
     * @param type 意见反馈的类别
     * @param identity 身份
     * @param status 状态
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String list(
            @RequestParam(required = false,defaultValue = "-1") int type,
            @RequestParam(required = false,defaultValue = "-1") int identity,
            @RequestParam(required = false,defaultValue = "-1") int status,
            @RequestParam(value = "page",defaultValue = "1") int page,
            @RequestParam(value = "rows",defaultValue = "15") int rows) {
        try {
            Page<YdfHealthEduArticle> res = articleService.findFeedback(page,rows,type,identity,status);
            return write(200, "操作成功", page, rows, res);
        } catch (Exception ex) {
            error(ex);
            return error(-1, "操作失败");
        }
    }

    /**
     * 获取文章类型
     * @return
     */
    @RequestMapping(value = "/getArticleType")
    @ResponseBody
    public String getArticleType() throws JsonProcessingException {
        List<SystemDictItem> systemDictItems = articleService.findHealthEduArticleTypeList();
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(systemDictItems);
        //return write(200, "查询成功！", "data", systemDictItems);
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public String save(@ModelAttribute @Valid YdfHealthEduArticle article){
        if(StringUtils.isBlank(article.getId())){
            article.setId(UUID.randomUUID().toString().replaceAll("-",""));
        }
        Date date = new Date();
        if(null==article.getModifyDate()){
            article.setModifyDate(date);
        }
        if(null==article.getCreateDate()){
            article.setCreateDate(date);
        }
        article.setStatus(0);//设置状态
        article.setType(0);//设置成待审核状态
        articleService.save(article);
        return write(200, "保存成功");
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public String del(@RequestParam(value = "id")String id){
        articleService.delete(id);
        return write(200, "删除成功");
    }
}
