package com.ssj.sys.controller;

import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.template.CommentTemplate;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.sys.template.CommentTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yanghuan
 * @title: CommentTemplateController
 * @projectName ssj
 * @description: TODO
 * @date 2019/8/13 14:37
 */
@Controller
@RequestMapping("/sys/comment")
public class CommentTemplateController extends BaseController {


    @Autowired
    private CommentTemplateService commentTemplateService;

    @RequestMapping("/commentList")
    public String appinfoList(Model model,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {

        Map<String, Object> params = new HashMap<>(16);
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI().toString());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<CommentTemplate> page = commentTemplateService.findByPage(params, initPage(pageNo, pageSize, Sort.Direction.DESC, "createTime"));
        sp.setRowCnt((int) page.getTotalElements());
        model.addAttribute("list", page.getContent());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);

        return "sys/comment/commentList";
    }

    @RequestMapping("/commentAdd")
    public String appinfoAdd(Model model, @RequestParam(defaultValue = "0") String id) throws Exception {
        CommentTemplate entity = commentTemplateService.getById(id);
        if (entity == null) {
            entity = new CommentTemplate();
        }
        model.addAttribute("entity", entity);
        return "sys/comment/commentAdd";
    }

    @RequestMapping(value = "/commentSave", method = RequestMethod.POST)
    @ResponseBody
    public Response appinfoSave(CommentTemplate entity) {
        logger.info(entity.toString());
        Response response = new Response();
        if (StringUtil.isBlank(entity.getId())) {
            entity.setCreateTime(new Date());
        }
        commentTemplateService.save(entity);
        response.success();
        return response;
    }

    @RequestMapping(value = "/commentDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response messageDelete(String id) {
        Response response = new Response();
        commentTemplateService.delete(id);
        response.success();
        return response;
    }
}
