package com.zd.base.message.controller;

import com.zd.base.message.domain.UserOpenId;
import com.zd.base.message.service.IUserOpenIdService;
import com.zd.common.core.domain.R;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.poi.ExcelUtil;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.core.web.page.TableDataInfo;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 用户和微信openID对应表Controller
 *
 * @author hanson
 */
@RestController
@Api(tags = "【用户和微信openID对应表】")
@RequestMapping("/user/wx")
public class UserOpenIdController extends BaseController<UserOpenId> {
    @Autowired
    private IUserOpenIdService userOpenIdService;

    /**
     * 查询用户和微信openID对应表列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询用户和微信openID对应表列表")
    public TableDataInfo<UserOpenId> list(UserOpenId userOpenId) {
        startPage();
        List<UserOpenId> list = userOpenIdService.selectUserOpenIdList(userOpenId);
        return getDataTable(list);
    }

    /**
     * 导出用户和微信openID对应表列表
     */
    @ApiOperation(value = "导出用户和微信openID对应表列表")
    @Log(title = "用户和微信openID对应表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserOpenId userOpenId) throws IOException {
        List<UserOpenId> list = userOpenIdService.selectUserOpenIdList(userOpenId);
        ExcelUtil<UserOpenId> util = new ExcelUtil<>(UserOpenId.class);
        util.exportExcel(response, list, "用户和微信openID对应表数据");
    }

    /**
     * 获取用户和微信openID对应表详细信息
     */
    @ApiOperation(value = "获取用户和微信openID对应表详细信息")
    @GetMapping(value = "/{id}")
    public ResultData<UserOpenId> getInfo(@PathVariable("id") Long id) {
        return ResultData.success(userOpenIdService.selectUserOpenIdById(id));
    }

    /**
     * 获取用户和微信openID对应表详细信息
     */
    @ApiOperation(value = "获取用户和微信openID对应表详细信息")
    @GetMapping(value = "openId/{userId}")
    public R<String> getOpenIdByUserId(@PathVariable("userId") Long userId) {
        UserOpenId openId = new UserOpenId();
        openId.setUserId(userId);
        List<UserOpenId> list = userOpenIdService.selectUserOpenIdList(openId);
        if (list.size() != 1) {
            throw new ServiceException("用户关联错误");
        }
        UserOpenId userOpenId = list.get(0);
        return R.ok(userOpenId.getOpenId());
    }

    /**
     * 根据小程序端传回的code，获取openId
     */
    @ApiOperation(value = "获取openID")
    @Log(title = "获取openID", businessType = BusinessType.INSERT)
    @GetMapping("getOpenId")
    public ResultData<UserOpenId> getOpenId(String code) {
        return ResultData.success(userOpenIdService.getOpenId(code));
    }
    /**
     * 新增用户和微信openID对应表
     */
    @ApiOperation(value = "新增用户和微信openID对应表")
    @Log(title = "用户和微信openID对应表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData<Boolean> add(@RequestBody UserOpenId userOpenId) {
        return ResultData.result(userOpenIdService.insertUserOpenId(userOpenId));
    }

    /**
     * 修改用户和微信openID对应表
     */
    @ApiOperation(value = "修改用户和微信openID对应表")
    @Log(title = "用户和微信openID对应表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData<Boolean> edit(@RequestBody UserOpenId userOpenId) {
        return ResultData.result(userOpenIdService.updateUserOpenId(userOpenId));
    }

    /**
     * 删除用户和微信openID对应表
     */
    @ApiOperation(value = "删除用户和微信openID对应表")
    @PreAuthorize(hasPermi = "airbottle:id:remove")
    @Log(title = "用户和微信openID对应表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData<Boolean> remove(@PathVariable Long[] ids) {
        return ResultData.result(userOpenIdService.deleteUserOpenIdByIds(ids));
    }
}
