package com.gree.mall.manager.controller.template;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.bean.template.TemplateBean;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.template.TemplateLogic;
import com.gree.mall.manager.plus.entity.CommonTemplate;
import com.gree.mall.manager.plus.entity.PubTemplate;
import com.gree.mall.manager.plus.entity.Tag;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

@Slf4j
@RestController
@Api(value = "公共模板", tags = {"公共模板"})
@RequestMapping(value = "/template", produces = "application/json; charset=utf-8")
public class TemplateController {
    @Autowired
    private TemplateLogic templateLogic;

    @GetMapping("/list")
    @ApiOperation("模板列表")
    public ResponseHelper<IPage<CommonTemplate>> list(
            @ApiParam(value = "1=说明管理 2=水印管理",required = false) @RequestParam(required = false) Integer type,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) {
        IPage<CommonTemplate> list = templateLogic.list(type,pageNum, pageSize ,request);
        return ResponseHelper.success(list);
    }


    @PostMapping("/save")
    @ApiOperation("创建模板")
    public ResponseHelper<String> save(
            @RequestBody TemplateBean templateBean,
            HttpServletRequest request
    ) throws ParseException {
        return ResponseHelper.success(templateLogic.save(templateBean,request));
    }


    @PostMapping("/update")
    @ApiOperation("编辑模板")
    public ResponseHelper<IPage<CommonTemplate>> update(
            @RequestBody TemplateBean templateBean,HttpServletRequest request
    ) throws ParseException {
        return ResponseHelper.success(templateLogic.update(templateBean,request));
    }

    @PostMapping("/pub/update")
    @ApiOperation("编辑公共模板")
    public ResponseHelper<String> updatePub(@RequestBody PubTemplate templateBean,HttpServletRequest request) {
        return ResponseHelper.success(templateLogic.updatePub(templateBean,request));
    }

    @GetMapping("/pub/query")
    @ApiOperation("获取公共模板")
    public ResponseHelper<PubTemplate> queryPub(HttpServletRequest request) {
        return ResponseHelper.success(templateLogic.queryPub(request));
    }


    @PostMapping("/delete")
    @ApiOperation("删除模板")
    public ResponseHelper<IPage<CommonTemplate>> delete(@ApiParam(value = "模板id", required = true) @RequestParam String commonTemplateId) {
        return ResponseHelper.success(templateLogic.delete(commonTemplateId));
    }


    @GetMapping("/detail")
    @ApiOperation("模板详情")
    public ResponseHelper<IPage<CommonTemplate>> detail(@ApiParam(value = "模板id", required = true) @RequestParam String commonTemplateId) {
        return ResponseHelper.success(templateLogic.detail(commonTemplateId));
    }


}
