package com.tofly.feesapi.wxgl.wechatofficalacctmgt.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.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxWdbm;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.mapper.WxWdbmMapper;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxWdbmService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/api/wechatofficalacctmgt/wxwdcode")
@Api(tags = "微信公众号-网点编码接口")
public class WxWdbmController {

    @Autowired
    private WxWdbmService wxWdbmService;
    @Autowired
    private WxWdbmMapper wxWdbmMapper;


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页显示数据条数", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pages", value = "共几页,输出参数", dataType = "int"),
            @ApiImplicitParam(name = "maxLimit", value = "每页最大显示数据", dataType = "int"),
            @ApiImplicitParam(name = "orders[0].asc", value = "排序,bool类型", dataType = "String"),
            @ApiImplicitParam(name = "orders[0].column", value = "排序,字段名", dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(标题)", dataType = "String")
    })
    public ResultRespone getWxWdCodePage(Page page, String searchText) {
        QueryWrapper<WxWdbm> queryWrapper = new QueryWrapper<WxWdbm>();
        queryWrapper.eq("1", "1");
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.and(i -> i.like("name", searchText));
        }
        return ResultRespone.success(wxWdbmService.page(page, queryWrapper));
    }


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

    @ApiOperation(value = "新增网点编码")
    @PostMapping
    public ResultRespone save(@RequestBody WxWdbm wxWdbm) {
        //查询当前ID
        int id = wxWdbmMapper.SelectChargeMaxCode();
        wxWdbm.setId(id + 1);
        return ResultRespone.success(wxWdbmService.save(wxWdbm));
    }

    @ApiOperation(value = "修改网点编码")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxWdbm wxWdbm) {
        return ResultRespone.success(wxWdbmService.updateById(wxWdbm));
    }


    @ApiOperation(value = "通过ID删除网点编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "BigDecimal")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable Integer id) {
        return ResultRespone.success(wxWdbmService.removeById(id));
    }


    @ApiOperation(value = "通过ID批量删除网点编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds")
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(wxWdbmService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
