package com.sqx.modules.app.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.resumes.entity.Company;
import com.sqx.modules.resumes.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.concurrent.locks.ReentrantReadWriteLock;

@Slf4j
@RestController
@RequestMapping("/app/userMoney")
@Api("app 钱包 ")
public class AppUserMoneyController {

    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;
    @Autowired
    private CompanyService companyService;
    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);


    @Login
    @GetMapping("/selectConsumeMoney")
    @ApiOperation("查询消费")
    public Result selectConsumeMoney(@RequestAttribute Long userId) {
        Double money = 0.00;
        Company company = companyService.selectCompanyByUserId(userId);
        if (company != null) {
            money = userMoneyDetailsService.selectCompanySumMoney(company.getCompanyId());
        }
        return Result.success().put("money", money);
    }


    @GetMapping("/selectMyMoney")
    @Login
    @ApiOperation("我的钱包余额")
    public Result selectMyMoney(@RequestAttribute Long userId) {
        return Result.success().put("data", userMoneyService.selectUserMoneyByUserId(userId));
    }

    /**
     * 我的收益
     */
    @GetMapping("/selectMyProfit")
    @Login
    @ApiOperation("我的收益")
    public Result selectMyProfit(@RequestAttribute Long userId) {
        return Result.success().put("data", userMoneyService.selectMyProfit(userId));
    }


    /**
     * 余额明细
     */
    @GetMapping("/balanceDetailed")
    @Login
    @ApiOperation("余额明细")
    public Result balanceDetailed(@RequestAttribute Long userId, Long page, Long limit, UserMoneyDetails userMoneyDetails) {
        userMoneyDetails.setUserId(userId);
        return Result.success().put("data", userMoneyService.balanceDetailed(page, limit, userMoneyDetails));
    }

    /**
     * 企业购买简历
     */
    @Login
    @PostMapping("/payResumes")
    @ApiOperation("企业钻石购买简历")
    public Result payResumes(@RequestAttribute Long userId, Long resumesId) {
        reentrantReadWriteLock.writeLock().lock();
        try {
            return userMoneyService.payResumes(userId, resumesId);
        } catch (Exception e) {
            log.error("购买异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("购买异常,请重试");

    }

    /**
     * 企业购买简历
     */
    @Login
    @PostMapping("/countPayResumes")
    @ApiOperation("企业次数购买简历")
    public Result countPayResumes(@RequestAttribute Long userId, Long resumesId) {
        reentrantReadWriteLock.writeLock().lock();
        try {
            return userMoneyService.countPayResumes(userId, resumesId);
        } catch (Exception e) {
            log.error("购买异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("购买异常,请重试");
    }
}
