package com.xinelu.manage.controller.textmessage;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.HttpStatus;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.PageDomain;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.core.page.TableSupport;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.dto.textmessage.TextMessageDTO;
import com.xinelu.manage.dto.textmessage.TextMessageTaskDTO;
import com.xinelu.manage.service.textmessage.ITextMessageService;
import com.xinelu.manage.vo.textmessage.TextMessageTaskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信模板信息Controller
 *
 * @author xinelu
 * @date 2024-02-28
 */
@Api(tags = "短信库控制器")
@RestController
@RequestMapping("/manage/message")
public class TextMessageController extends BaseController {
    @Resource
    private ITextMessageService textMessageService;

    /**
     * 查询短信模板信息列表
     */
    @ApiOperation("查询短信模板信息列表")
    @GetMapping("/list")
    public TableDataInfo list(TextMessageDTO textMessageDTO) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        List<TextMessageTaskVO> list = textMessageService.selectTextMessageList(textMessageDTO);
        //处理上面查询的list集合
        int num = list.size();
        list = list.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(num);
        return rspData;
    }

    /**
     * 导出短信模板信息列表
     */
    @ApiOperation("导出短信模板信息列表")
    @PreAuthorize("@ss.hasPermi('manage:message:export')")
    @Log(title = "短信模板信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TextMessageDTO textMessageDTO) {
        List<TextMessageTaskVO> voList = textMessageService.selectTextMessageList(textMessageDTO);
        ExcelUtil<TextMessageTaskVO> util = new ExcelUtil<TextMessageTaskVO>(TextMessageTaskVO.class);
        util.exportExcel(response, voList, "短信模板信息数据");
    }

    /**
     * 获取短信模板信息详细信息
     */
    @ApiOperation("获取短信模板信息详细信息")
    @PreAuthorize("@ss.hasPermi('manage:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(textMessageService.selectTextMessageById(id));
    }

    /**
     * 新增短信模板信息
     */
    @ApiOperation("新增短信模板信息")
    @PreAuthorize("@ss.hasPermi('manage:message:add')")
    @Log(title = "短信模板信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TextMessageTaskDTO textMessage) {
        return toAjax(textMessageService.insertTextMessage(textMessage));
    }

    /**
     * 修改短信模板信息
     */
    @ApiOperation("修改短信模板信息")
    @PreAuthorize("@ss.hasPermi('manage:message:edit')")
    @Log(title = "短信模板信息", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody TextMessageTaskDTO textMessageTaskDTO) {
        return toAjax(textMessageService.updateTextMessage(textMessageTaskDTO));
    }

    /**
     * 删除短信模板信息
     */
    @ApiOperation("删除短信模板信息")
    @PreAuthorize("@ss.hasPermi('manage:message:remove')")
    @Log(title = "短信模板信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(textMessageService.deleteTextMessageByIds(ids));
    }

    /**
     * 查询话术变量
     */
    @GetMapping("/selectTextMessageVariable")
    public AjaxResult selectTextMessageVariable(Long id) {
        return textMessageService.selectTextMessageVariable(id);
    }
}
