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.feesapi.common.core.ResultRespone;


import com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxUser;
import com.tofly.feesapi.wxgl.wechatbasicmgt.service.WxUserService;
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/wechatbasicmgt/wxcustomer")
@Api(tags = "微信管理-微信解绑接口")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    @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 = "bindFlag", value = "是否绑定，0,1", dataType = "String"),
            @ApiImplicitParam(name = "searchText", value = "模糊查询(用户编号、用户姓名、用户地址)", required = false, dataType = "String")
    })
    public ResultRespone getWxCustomerPage(Page page, String searchText, String bindFlag) {
        QueryWrapper<WxUser> queryWrapper = new QueryWrapper<WxUser>();
        queryWrapper.eq("1", "1");
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.and(i -> i.like("yhbh", searchText).or().like("name", searchText).or().like("address", searchText));
        }
        if (StringUtils.isNotBlank(bindFlag) && bindFlag != "all") {
            queryWrapper.in("wxbdbz", bindFlag);
        }
        return ResultRespone.success(wxUserService.page(page, queryWrapper));
    }


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


    @ApiOperation(value = "新增微信解绑")
    @PostMapping
    public ResultRespone save(@RequestBody WxUser wxUser) {
        return ResultRespone.success(wxUserService.save(wxUser));
    }


    @ApiOperation(value = "修改微信解绑")
    @PutMapping
    public ResultRespone<Boolean> updateById(@RequestBody WxUser wxUser) {
        return ResultRespone.success(wxUserService.update2(wxUser));
    }

    @ApiOperation(value = "通过ID删除微信解绑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customerNo", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{customerNo}")
    public ResultRespone removeById(@PathVariable String customerNo) {
        return ResultRespone.success(wxUserService.removeById(customerNo));
    }

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