package com.ruoyi.applet.contoller;

import com.alibaba.fastjson.JSONObject;
import com.cyl.h5.pojo.request.OrderPayRequest;
import com.cyl.h5.pojo.response.OrderPayResponse;
import com.cyl.manager.ums.domain.Member;
import com.cyl.wechat.WechatPayService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.LocalDataUtil;
import com.ruoyi.model.domain.AppUsers;
import com.ruoyi.model.domain.AppletUsersTeacher;
import com.ruoyi.model.service.IAppUsersService;
import com.ruoyi.model.service.IAppletUsersTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(description = "伴宠师-伴宠师认证")
@RestController
@RequestMapping("/applet/UserTeacher")
public class ApiAppletUserTeacherController {
    @Autowired
    private IAppUsersService appUsersService;
    @Autowired
    private IAppletUsersTeacherService appletUsersTeacherService;
    @Autowired
    private WechatPayService wechatPayService;
    @Autowired
    private RedisService redisService;


    @ApiOperation("伴宠师工作台-伴宠师列表数据查询")
    @GetMapping("/userList")
    public AjaxResult userList(AppletUsersTeacher usersTeacher){
        List<AppletUsersTeacher> userList = appletUsersTeacherService.selectAppletUsersTeacherList(usersTeacher);
        return AjaxResult.success("伴宠师列表数据",userList);
    }


    @ApiOperation("伴宠师工作台-查询用户是否提交过数据")
    @GetMapping("/getUserOne/{appUserId}")
    public AjaxResult getUserOne(@PathVariable("appUserId") Long appUserId){
        AppletUsersTeacher appletUsersTeacher = appletUsersTeacherService.selectAppletUsersTeacherByUserId(appUserId);
        return AjaxResult.success("认证信息",appletUsersTeacher);
    }





    @ApiOperation("伴宠师工作台-伴宠师基本信息添加")
    @PostMapping("/insertUser")
    public AjaxResult insertUser(@RequestBody AppletUsersTeacher usersTeacher){
        if(usersTeacher.getUserId() == null){
           return AjaxResult.error("userId用户信息标识不能为空");
        }
        usersTeacher.setStatus(0L);
        AppletUsersTeacher appletUsersTeacher = appletUsersTeacherService.selectAppletUsersTeacherByUserId(usersTeacher.getUserId());
        if(appletUsersTeacher != null){
            return AjaxResult.error("您已经提交过了请不要重复提交");
        }
        return toAjax(appletUsersTeacherService.insertAppletUsersTeacher(usersTeacher));
    }

    @ApiOperation("伴宠师工作台-伴宠师基本信息修改")
    @PostMapping("/udpateUser")
    public AjaxResult udpateUser(@RequestBody AppletUsersTeacher usersTeacher){
        if(usersTeacher.getId() == null){
            return AjaxResult.error("标识不能为空");
        }
        return toAjax(appletUsersTeacherService.updateAppletUsersTeacher(usersTeacher));
    }

    @ApiOperation("伴宠师工作台-伴宠师基本信息删除")
    @PostMapping("/deleteUser")
    public AjaxResult deleteUser(long id){
        return toAjax(appletUsersTeacherService.deleteAppletUsersTeacherById(id));
    }




    //保证金缴纳
    @ApiOperation("伴宠师工作台-伴宠师基本信息删除")
    @PostMapping("/payPrice")
    public ResponseEntity<OrderPayResponse> orderPay(@RequestBody OrderPayRequest req){
        log.info("订单支付","提交的数据："+ JSONObject.toJSONString(req));
        String redisKey = "h5_oms_bcs_pay_"+req.getPayId();
        String redisValue = req.getPayId()+"_"+System.currentTimeMillis();
        try {
            redisService.lock(redisKey, redisValue, 60);
            AppUsers member = appUsersService.selectAppUsersByUserId(req.getMemberId());
            String orderNo = "";
            String desc = "";
            Integer totalAmount = 1;
            String openId =member.getOpenid();
            Long memberId = member.getUserId();
            String appId = "";
            String s = wechatPayService.jsapiPayForBCHSHI(
                    orderNo,
                    desc,
                    totalAmount,
                    openId,
                    memberId,
                    appId
            );
            return null;
        }catch (Exception e){
            log.error("支付方法异常", e);
            throw new RuntimeException(e.getMessage());
        }finally {
            try{
                redisService.unLock(redisKey,redisValue);
            }catch (Exception e){
                log.error("",e);
            }
        }
    }





    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
