package com.mcwl.web.controller.personalCenter;


import cn.hutool.core.bean.BeanUtil;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.memberCenter.domain.Member;
import com.mcwl.memberCenter.domain.MemberConsume;
import com.mcwl.memberCenter.domain.vo.MemberConsumeVO;
import com.mcwl.memberCenter.domain.vo.RechargeRecordVO;
import com.mcwl.memberCenter.service.MemberService;
import com.mcwl.myInvitation.service.CommissionService;
import com.mcwl.myInvitation.service.ConsumeService;
import com.mcwl.pay.service.OrderTradeService;
import com.mcwl.resource.domain.dto.ModelImagePageRes;
import com.mcwl.resource.service.*;
import com.mcwl.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(tags = "个人中心")
@RestController
@RequestMapping("personalCenter")
@RequiredArgsConstructor
public class PersonalCenterController {

    private final ModelService modelService;

    private final ModelLikeService modelLikeService;

    private final ModelImageService modelImageService;

    private final WorkFlowService workFlowService;

    private final WorkFlowLikeService workFlowLikeService;

    private final ModelImageLikeService modelImageLikeService;

    private final ISysUserService sysUserService;

    private final MemberService memberService;

    private final OrderTradeService orderTradeService;

    private final ConsumeService consumeService;

    private final CommissionService commissionService;


    /**
     * 我的发布-模型列表
     */
    @ApiOperation(value = "我的发布-模型列表")
    @PostMapping("/selectByUserIdModel")
    public TableDataInfo selectByUserIdModel(@RequestBody ModelImagePageRes imagePageRes) {

        return modelService.listByPage(imagePageRes);
    }

    /**
     * 我的发布-工作流列表
     */
    @ApiOperation(value = "我的发布-工作流列表")
    @PostMapping("/selectByUserIdWorkFlow")
    public TableDataInfo selectByUserIdWorkFlow(@RequestBody ModelImagePageRes imagePageRes) {

        return workFlowService.listByPage(imagePageRes);
    }

    /**
     * 我的发布-图片列表
     */
    @ApiOperation(value = "我的发布-图片列表")
    @PostMapping("/selectByUserIdImage")
    public TableDataInfo selectByUserIdImage(@RequestBody ModelImagePageRes imagePageRes) {

        return modelImageService.listByPage(imagePageRes);
    }

    /**
     * 我的点赞-模型列表
     */
    @ApiOperation(value = "我的点赞-模型列表")
    @PostMapping("/likeModel")
    public TableDataInfo likeModel(@Valid @RequestBody PageDomain pageDomain) {
        return modelLikeService.listByPage(pageDomain);
    }

    /**
     * 我的点赞-工作流列表
     */
    @ApiOperation(value = "我的点赞-工作流列表")
    @PostMapping("/likeWorkFlow")
    public TableDataInfo likeWorkFlow(@Valid @RequestBody PageDomain pageDomain) {
        return workFlowLikeService.listByPage(pageDomain);
    }

    /**
     * 我的点赞-图片列表
     */
    @ApiOperation(value = "我的点赞-图片列表")
    @PostMapping("/likeImage")
    public TableDataInfo likeImage(@Valid @RequestBody PageDomain pageDomain) {
        return modelImageLikeService.listByPage(pageDomain);
    }

    /**
     * 获取积分和金币
     */
    @ApiOperation(value = "获取积分和金币")
    @PostMapping("/getPointAndWallet")
    public R<Map<String, Double>> getPointAndWallet() {

        Long userId = SecurityUtils.getUserId();

        Map<String, Double> map = new HashMap<>();

        SysUser sysUser = sysUserService.selectUserById(userId);

        Member member = memberService.getUseUserMemberByUserId(userId);

        double points = sysUser.getFreePoints();

        if (Objects.nonNull(member)) {
            points = points + member.getPoints();
        }

        map.put("point", points);
        map.put("wallet", sysUser.getWallet());

        return R.ok(map);

    }


    /**
     * 获取金币收入支出消费记录
     */
    @ApiOperation(value = "获取金币收入支出消费记录")
    @PostMapping("/getWalletRecord")
    public TableDataInfo getWalletRecord(@Valid @RequestBody PageDomain pageDomain) {
        return consumeService.getWalletRecord(pageDomain);
    }

    /**
     * 获取金币收入记录
     */
    @ApiOperation(value = "获取金币收入记录")
    @PostMapping("/getWalletIncomeRecord")
    public TableDataInfo getWalletIncomeRecord(@Valid @RequestBody PageDomain pageDomain) {

        return commissionService.getWalletIncomeRecord(pageDomain);
    }

    /**
     * 获取金币充值记录
     */
    @ApiOperation(value = "获取金币充值记录")
    @PostMapping("/getRecord")
    public TableDataInfo getRecord(@Valid @RequestBody PageDomain pageDomain) {
        return orderTradeService.getRecord(pageDomain, "金币充值");
    }

}
