package cn.pluss.platform.controller.notice.template;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.entity.MerchantMoveRecords;
import cn.pluss.platform.entity.MerchantMoveSetting;
import cn.pluss.platform.entity.NoticeTemplate;
import cn.pluss.platform.notice.template.NoticeTemplateService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 自定义消息通知模板表 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2021-08-26
 */
@Controller
@RequestMapping("/notice/template")
public class NoticeTemplateController extends BaseModelController<NoticeTemplateService, NoticeTemplate> {

    @Resource
    private DictService dictService;


    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public ModelAndView index(ModelAndView view) {
        List<Dict> templateTypeList = dictService.getByCode(Arrays.asList("notice_template_type"));
        view.addObject("templateTypeList", JSONObject.toJSONString(templateTypeList));
        view.setViewName("notice/templateList");
        return view;
    }

    @Override
    public Result<Page<NoticeTemplate>> page(Page<NoticeTemplate> page, NoticeTemplate condition, OrderItem orderItem) {
        orderItem.setColumn("createTime");
        orderItem.setAsc(false);
        return super.page(page, condition, orderItem);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        List<Dict> templateTypeList = dictService.getByCode(Arrays.asList("notice_template_type"));
        if (id != null) {
            NoticeTemplate template = new NoticeTemplate().setId(id);
            template = baseService.getOne(new QueryWrapper<>(template));
            request.setAttribute("entity", template);
        }
        request.setAttribute("templateTypeList", templateTypeList);
        return "notice/templateDetail";
    }
}
