package com.tofly.feesapi.sms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.sms.entity.SmsTxlx;
import com.tofly.feesapi.sms.entity.dto.SmsTxlxDto;
import com.tofly.feesapi.sms.entity.vo.SmsTxlxVo;
import com.tofly.feesapi.sms.service.ISmsTxlxService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;

@RestController
@RequestMapping("/smstxlx")
@Api(tags = "短信模块-短信模板接口")
public class SmsTxlxController {

    @Autowired
    private ISmsTxlxService ISmsTxlxService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<Page<SmsTxlxVo>> getSmsTxlxPage(Page page, @ModelAttribute SmsTxlxDto dto) {
        ResultRespone<Page<SmsTxlxVo>> resultRespone = new ResultRespone<>();
        try {
            Page<SmsTxlxVo> smsTxlxPage = ISmsTxlxService.getSmsTxlxPage(page, dto);
            resultRespone.setCode(1);
            resultRespone.setResult(smsTxlxPage);
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }


    /**
     * 通过id查询短信模板
     */
    @GetMapping("/{code}")
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone getById(@PathVariable("code") String code) {
        return ResultRespone.success(ISmsTxlxService.getById(code));
    }

    /**
     * 新增短信模板
     */
    @ApiOperation(value = "新增短信模板")
    @PostMapping
    public ResultRespone save(@RequestBody SmsTxlx smsTxlx) {
        return ResultRespone.success(ISmsTxlxService.saveMessage(smsTxlx));
    }

    /**
     * 修改短信模板
     */
    @ApiOperation(value = "修改短信模板")
    @PutMapping
    public ResultRespone updateById(@RequestBody SmsTxlx smsTxlx) {
        return ResultRespone.success(ISmsTxlxService.updateById(smsTxlx));
    }

    /**
     * 通过id删除短信模板
     */
    @ApiOperation(value = "通过ID删除短信模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{code}")
    public ResultRespone removeById(@PathVariable String code) {
        return ResultRespone.success(ISmsTxlxService.removeById(code));
    }

    /**
     * 通过id批量删除短信模板
     */
    @ApiOperation(value = "通过ID批量删除短信模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeByIds(String ids) {
        return ResultRespone.success(ISmsTxlxService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
