package org.jeecg.modules.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.bean.RechargeReq;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.msgAddress.entity.MsgAddress;
import org.jeecg.modules.msgDistributor.entity.MsgDistributor;
import org.jeecg.modules.msgShop.entity.MsgShop;
import org.jeecg.modules.msgTechnician.entity.MsgTechnician;
import org.jeecg.modules.msgTer.entity.MsgTer;
import org.jeecg.modules.service.UserVipApiService;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Api(tags="预约相关H5接口 - 增删改")
@RestController
@RequestMapping("/post")
@Slf4j
public class PostVipController {


    @Resource
    private UserVipApiService postApiService;

    //修改用户信息
    @Resource
    private IHanHaiMemberService memberService;
    //缓存记录
    @Resource
    private RedisUtil redisUtil;
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //我的 - 增加地址信息
    @ApiOperation(value="我的 - 增加地址信息", notes="我的 - 增加地址信息")
    @PostMapping(value = "/addOrUpdateAddress")
    public Result<?> addOrUpdateAddress(
            @RequestHeader("X-Access-Token") String token,
            MsgAddress address
    ) {
        return postApiService.addOrUpdateAddress(address,token);
    }

    //我的 - 删除地址信息
    @ApiOperation(value="我的 - 删除地址信息", notes="我的 - 删除地址信息")
    @PostMapping(value = "/deleteAddress")
    public Result<?> deleteAddress(
            @RequestHeader("X-Access-Token") String token,
            String id
    ) {
        return postApiService.deleteAddress(id,token);
    }


    //我的 - 增加或修改技师入驻信息
    @ApiOperation(value="我的 - 增加或修改技师入驻信息", notes="我的 - 增加或修改技师入驻信息")
    @PostMapping(value = "/addOrUpdateMsgTer")
    public Result<?> addOrUpdateMsgTer(
            @RequestHeader("X-Access-Token") String token,
            MsgTer msgTechnician
    ) {
        return postApiService.addOrUpdateMsgTer(msgTechnician,token);
    }


    //我的 - 增加或修改分销商入驻信息
    @ApiOperation(value="我的 - 增加或修改分销商入驻信息", notes="我的 - 增加或修改分销商入驻信息")
    @PostMapping(value = "/addOrUpdateShop")
    public Result<?> addOrUpdateShop(
            @RequestHeader("X-Access-Token") String token,
            MsgDistributor bean
    ) {
        return postApiService.addOrUpdateShop(bean,token);
    }

    //我的 - 修改用户信息
    @ApiOperation(value="我的 - 修改用户信息", notes="我的 - 修改用户信息")
    @PostMapping(value = "/updateUser")
    public Result<?> updateUser(
            @RequestHeader("X-Access-Token") String token,
            HanHaiMember member
    ) {
        return postApiService.updateUser(member,token);
    }



    /**
     * 微信支付
     * @param token
     * @param req
     * @return
     */
    @ApiOperation("微信支付")
    @GetMapping("/create")
    public Result<?> createWxPayOrder(@RequestHeader("X-Access-Token") String token, RechargeReq req){
        return postApiService.createPayOrder(token,req);
    }



    /**
     * 微信小程序支付/充值-订单支付回调
     * @param requestBody
     * @return
     */
    @PostMapping("/notify")
    public Object createWxNotify(@RequestBody String requestBody){
        return postApiService.createWxNotify(requestBody);
    }


    /**
     * 展示二维码
     * @return
     */
    @ApiOperation(value="获取并且展示邀请二维码", notes="获取并且展示邀请二维码")
    @GetMapping(value = "/showQrcode")
    public Result<?> getShowQrcode(){
        return postApiService.getShowQrcode();
    }


    /**
     * 展示我的粉丝
     * @return
     */
    @ApiOperation(value="展示我的粉丝", notes="展示我的粉丝")
    @GetMapping(value = "/fans")
    public Result<?> fansList(@RequestHeader("X-Access-Token") String token,
                              @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                              HttpServletRequest req
    ){
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        QueryWrapper<HanHaiMember> queryWrapper = QueryGenerator.initQueryWrapper(new HanHaiMember().setVid(hanHaiMember.getId()), req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        Page<HanHaiMember> page = new Page<HanHaiMember>(pageNo, pageSize);
        IPage<HanHaiMember> pageList = memberService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    //钱包提现
    @ApiOperation(value="钱包提现", notes="钱包提现")
    @PostMapping(value = "/withdraw")
    public Result<?> withdraw(
            @RequestHeader("X-Access-Token") String token,
            @RequestParam(name="price", defaultValue="0") BigDecimal price
    ) {
        return postApiService.walletWithdraw(token,price);
    }


}
