package com.ruoyi.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.code.DictConstant;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.controller.req.SaleDetailPageReq;
import com.ruoyi.controller.resp.HomeDataResp;
import com.ruoyi.controller.resp.RankingResp;
import com.ruoyi.controller.resp.SaleDataResp;
import com.ruoyi.controller.resp.SaleDetailPageResp;
import com.ruoyi.frequency.customer.entity.Customer;
import com.ruoyi.frequency.customer.service.CustomerService;
import com.ruoyi.frequency.order.service.OrderService;
import com.ruoyi.frequency.sale.entity.Sale;
import com.ruoyi.frequency.sale.mapper.SaleMapper;
import com.ruoyi.frequency.sale.service.SaleService;
import com.ruoyi.frequency.store.entity.Store;
import com.ruoyi.frequency.store.service.StoreService;
import com.ruoyi.frequency.wallerecord.mapper.WalletRecordMapper;
import com.ruoyi.frequency.withdrawalrecord.mapper.WithdrawalRecordMapper;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.PageBasic;
import com.ruoyi.vo.RyController;
import com.ruoyi.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 推广员端
 *
 * @author liwenlong
 * @date 2023-10-12
 */
@CrossOrigin
@RestController
@RequestMapping("/api/sale")
@Api(tags = "推广员端")
public class ApiSaleController extends RyController {
    @Autowired
    private BusinessUtil businessUtil;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private SaleService saleService;

    @Autowired
    private OrderService orderService;


    @PostMapping("/homeData")
    @ApiOperation(value = "首页数据", notes = "首页数据")
    public ResponseData<HomeDataResp> homeData() {

        UserVo userVo = getUserVo();
        HomeDataResp homeDataResp = new HomeDataResp();

        //平台设置比例系数
        Integer scale = Integer.valueOf(businessUtil.getDict(DictConstant.平台设置比例系数).getDictValue());

        //查询入驻设计师
        int customerNum = customerService.count(new QueryWrapper<Customer>().lambda()
                .ne(Customer::getDelFlag, PublicCommon.删除));
        homeDataResp.setCustomerNum(customerNum * scale);

        //查询入驻技术
        int storeNum = storeService.count(new QueryWrapper<Store>().lambda()
                .ne(Store::getDelFlag, PublicCommon.删除));
        homeDataResp.setStoreNum(storeNum * scale);

        //查询入驻推广员
        int saleNum = saleService.count(new QueryWrapper<Sale>().lambda()
                .ne(Sale::getDelFlag, PublicCommon.删除));
        homeDataResp.setSaleNum(saleNum * scale);

        //佣金排名
        Integer commissionRanking = saleService.commissionRanking(userVo);
        homeDataResp.setCommissionRanking(commissionRanking);

        //推广排名
        Integer saleRanking = saleService.saleRanking(userVo);
        homeDataResp.setSaleRanking(saleRanking);

        //推广邀请码
        Sale sale = saleService.getById(userVo.getUserId());
        if (sale != null) {
            homeDataResp.setInviteCode(sale.getInviteCode());
            homeDataResp.setAvatar(sale.getAvatar());
            homeDataResp.setNickname(sale.getNickname());
            homeDataResp.setName(sale.getName());
            homeDataResp.setInviteNum(sale.getInviteNum());
            homeDataResp.setIncome(sale.getIncome());
        }

        return ResponseData.success(homeDataResp);
    }

    @PostMapping("/commissionRankingPage")
    @ApiOperation(value = "佣金排名列表", notes = "佣金排名列表")
    public ResponseData<PageResult<Page<RankingResp>>> commissionRankingPage(@RequestBody PageBasic req) {
        return saleService.commissionRankingPage(req, getUserVo());
    }

    @PostMapping("/saleRankingPage")
    @ApiOperation(value = "推广人数排名列表", notes = "推广人数排名列表")
    public ResponseData<PageResult<Page<RankingResp>>> saleRankingPage(@RequestBody PageBasic req) {
        return saleService.saleRankingPage(req, getUserVo());
    }


    @PostMapping("/saleDetailPage")
    @ApiOperation(value = "明细", notes = "明细")
    public ResponseData<PageResult<Page<SaleDetailPageResp>>> saleDetailPage(@RequestBody SaleDetailPageReq req) {
        return saleService.saleDetailPage(req, getUserVo());
    }


    @Autowired
    private SaleMapper saleMapper;

    @Autowired
    private WalletRecordMapper walletRecordMapper;

    @Autowired
    private WithdrawalRecordMapper withdrawalRecordMapper;

    @PostMapping("/saleData")
    @ApiOperation(value = "我的页面数据", notes = "我的页面数据")
    public ResponseData<SaleDataResp> saleData() {

        UserVo userVo = getUserVo();

        Sale sale = saleService.getById(userVo.getUserId());

        SaleDataResp saleDataResp = saleMapper.saleOrderAmount(sale.getId());
        saleDataResp.setNum(sale.getInviteNum());
        saleDataResp.setIncome(sale.getIncome());
        saleDataResp.setBalance(sale.getBalance());
        //可提现金额(去除本月收益)
        //获取本月开始时间
        Date beginOfMonth = DateUtil.beginOfMonth(new Date());

        BigDecimal withdrawableAmount = walletRecordMapper.selectMonthBeforeAmount(beginOfMonth, userVo);
        saleDataResp.setWithdrawableAmount(withdrawableAmount.compareTo(BigDecimal.ZERO) == 1 ? withdrawableAmount : BigDecimal.ZERO);

        //已提现金额
        BigDecimal withdrawnAmount = withdrawalRecordMapper.withdrawnAmount(userVo);
        saleDataResp.setWithdrawnAmount(withdrawnAmount);

        return ResponseData.success(saleDataResp);
    }

}
