package com.ruoyi.cms.controller.cms;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.cms.domain.WechatGroup;
import com.ruoyi.cms.domain.vo.WechatGroupVo;
import com.ruoyi.cms.service.IWechatGroupService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/cms/wechatGroup")
@Api(tags = "后台：转发对象管理")
public class WechatGroupController extends BaseController {

    @Autowired
    private IWechatGroupService wechatGroupService;


    @ApiOperation("查询转发对象列表")
    @PreAuthorize("@ss.hasPermi('application:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(WechatGroup wechatGroup) {
        startPage();
        List<WechatGroupVo> list = wechatGroupService.selectWechatGroupList(wechatGroup);
        return getDataTable(list);
    }

    @ApiOperation("新增转发对象")
    @PreAuthorize("@ss.hasPermi('application:group:add')")
    @PostMapping
    public AjaxResult add(@RequestBody WechatGroup wechatGroup) {
        return toAjax(wechatGroupService.save(wechatGroup));
    }

    @ApiOperation("修改转发对象")
    @PreAuthorize("@ss.hasPermi('application:group:update')")
    @PutMapping
    public AjaxResult update(@RequestBody WechatGroup wechatGroup) {
        return toAjax(wechatGroupService.updateById(wechatGroup));
    }

    @ApiOperation("删除转发对象")
    @PreAuthorize("@ss.hasPermi('application:group:del')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return toAjax(wechatGroupService.removeBatchByIds(CollUtil.newArrayList(ids)));
    }

    @GetMapping("/enableList")
    @Anonymous
    public List<WechatGroupVo> enableList(WechatGroup wechatGroup) {
        wechatGroup.setIsPush(1);
        return wechatGroupService.selectWechatGroupList(wechatGroup)
                .stream().peek(e->e.setPhoneNumber(""))
                .collect(Collectors.toList());
    }
}
