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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.feesapi.common.util.HttpRequestUtil;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxDbsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.WxXhsq;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.dto.WxCommonDto;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.entity.vo.WxXhsqVo;
import com.tofly.feesapi.wxgl.wechatofficalacctmgt.service.WxXhsqService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 微信-销户申请
 */
@RestController
@RequestMapping("/wxxhsq")
@Api(tags = "微信-销户申请接口")
public class WxXhsqController {

    @Autowired
    private WxXhsqService wxXhsqService;

    @GetMapping("/getpage")
    @ApiOperation(value = "自定义分页查询")
    public ResultRespone<Page<WxXhsqVo>> getWxXhsqPage(Page page, @ModelAttribute WxCommonDto dto) {
        ResultRespone<Page<WxXhsqVo>> resultRespone = new ResultRespone<>();
        try {
            Page<WxXhsqVo> wxPage = wxXhsqService.getWxXhsqPage(page, dto);
            resultRespone.setCode(1);
            resultRespone.setResult(wxPage);
        } catch (RuntimeException e) {
            resultRespone.setCode(-1);
            resultRespone.setMessage(e.getMessage());
        }
        return resultRespone;
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultRespone getWxXhsqPage(Page page, @ModelAttribute WxXhsq wxXhsq) {
        return ResultRespone.success(wxXhsqService.page(page, Wrappers.query(wxXhsq)));
    }


    @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(wxXhsqService.getById(id));
    }


    @ApiOperation(value = "新增微信-销户申请")
    @PostMapping
    public ResultRespone save(@RequestBody WxXhsq wxXhsq) {
        return ResultRespone.success(wxXhsqService.save(wxXhsq));
    }


    @ApiOperation(value = "修改微信-销户申请")
    @PutMapping
    public ResultRespone updateById(@RequestBody WxXhsq wxXhsq) {
        return ResultRespone.success(wxXhsqService.updateById(wxXhsq));
    }


    @ApiOperation(value = "通过ID删除微信-销户申请")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{id}")
    public ResultRespone removeById(@PathVariable String id) {
        return ResultRespone.success(wxXhsqService.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(wxXhsqService.removeByIds(Arrays.asList(ids.split(","))));
    }

    @ApiOperation(value = "修改微信-销户申请")
    @PostMapping("/update")
    public ResultRespone<Boolean> update(@RequestBody WxXhsq update, HttpServletRequest request) {
        String ipaddress = HttpRequestUtil.getClientIp(request);
        return ResultRespone.success(wxXhsqService.updateWxXhsq(update));
    }
}
