package com.tofly.feesapi.wxgl.wechatbasicmgt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxMessage;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.dto.CreateWxMessageDto;
import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.dto.WxMessagedto;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Collections;

/**
 * 微信模板消息
 */
@RestController
@RequestMapping("/api/wechatbasicmgt/wxmessage")
@Api(tags = "微信管理—微信模板消息接口")
public class WxMessageController {

    @Autowired
    private WxMessageService wxMessageService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone<PageResult<WxMessage>> getWxMessagePage(@ModelAttribute PageDto pageDto, @ModelAttribute WxMessagedto wxMessagedto) {
        return ResultRespone.success(wxMessageService.getWxMessagePage(pageDto, wxMessagedto));
    }


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

    @ApiOperation(value = "新增微信模板消息")
    @PostMapping
    public ResultRespone save(@RequestBody WxMessage wxMessage) {
        return ResultRespone.success(wxMessageService.save(wxMessage));
    }

    @ApiOperation(value = "修改微信模板消息")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxMessage wxMessage) {
        return ResultRespone.success(wxMessageService.updateById(wxMessage));
    }

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

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


    @ApiOperation(value = "批量生成短信")
    @PostMapping("/plscMsg")
    public ResultRespone plscMsg(@RequestBody CreateWxMessageDto createWxMessageDto) {
        if (StringUtil.isEmpty(createWxMessageDto.getSccbb())) {
            throw new FeesException("抄表册不能为空！", FeesErrorCode.BUSINESS_ERROR);
        }
        return ResultRespone.success(wxMessageService.plScMsg(createWxMessageDto.getType(), Arrays.asList(createWxMessageDto.getSccbb().split(",")), createWxMessageDto));
    }

    @ApiOperation(value = "发送微信欠费信息")
    @PostMapping("/plUpdateMsg")
    public ResultRespone plUpdateMsg(@RequestBody CreateWxMessageDto createWxMessageDto) {
        return ResultRespone.success(wxMessageService.plUpdateMsg(createWxMessageDto.getType(), createWxMessageDto.getPch(), createWxMessageDto.getMsg()));
    }

    @ApiOperation(value = "发送微信欠费信息(单个)")
    @PostMapping("/plUpdateMsgOne")
    public ResultRespone plUpdateMsgOne(@RequestBody CreateWxMessageDto createWxMessageDto) {
        return ResultRespone.success(wxMessageService.plUpdateMsgOne(createWxMessageDto.getType(), createWxMessageDto.getId(), createWxMessageDto.getMsg()));
    }

    @ApiOperation(value = "通过PCH删除微信状态为0的模板消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pch", value = "pch", required = true, dataType = "String")
    })
    @DeleteMapping("/delbypch/{pch}")
    public ResultRespone removeBypch(@PathVariable String pch) {
        return ResultRespone.success(wxMessageService.deleteByPch(pch));
    }

}
