package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.VvDrawCashRecordAgreeRequest;
import com.heyu.api.alibaba.request.mm.VvDrawCashRecordRejectRequest;
import com.heyu.api.alibaba.request.mm.VvDrawCashRecordRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.enums.ApprovalEnums;
import com.heyu.api.data.dao.vv.VvBuyerDao;
import com.heyu.api.data.dao.vv.VvDrawCashRecordDao;
import com.heyu.api.data.dao.vv.VvPromoterAwardLineDao;
import com.heyu.api.data.entity.vv.VvDrawCashRecordEntity;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/mm/draw/cash")
public class AdminDrawCashRecordController {


    @Autowired
    private VvDrawCashRecordDao vvDrawCashRecordDao;

    @Autowired
    private VvBuyerDao vvBuyerDao;

    @Autowired
    private VvPromoterAwardLineDao vvPromoterAwardLineDao;

    /***
     * 奖励 列表
     * http://localhost:8888/mm/draw/cash/list
     */
    @Describe("提现申请列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvDrawCashRecordRequest request) {
        PPageUtils pageUtils = PPageUtils.startPage(request.getPageNum(), request.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvDrawCashRecordDao.selectByCondition(page,
                                request.getBuyerId(),
                                request.getBuyerName(),
                                request.getBuyerPhone(),
                                request.getBuyerWeixin(),
                                request.getStatus(),
                                request.getMinCreateTimestamp(),
                                request.getMaxCreateTimestamp(),
                                request.getCreateTimestampSort()
                        );
                    }
                });
        return R.ok().setData(pageUtils);
    }


    /***
     * 奖励 同意
     * http://localhost:8888/mm/activity/agree
     */
    @Describe("同意提现")
    @RequestMapping("/agree")
    public R agree(@RequestBody VvDrawCashRecordAgreeRequest request) {
        VvDrawCashRecordEntity vvDrawCashRecord = vvDrawCashRecordDao.selectVvDrawCashRecordById(request.getDrawCashRecordId());
        if (StringUtils.notEquals(vvDrawCashRecord.getStatus(), ApprovalEnums.apply.getStatus())) {
            return R.error("状态不对");
        }
        vvBuyerDao.updateVvBuyerApplyAmountDrawCashAmountById(vvDrawCashRecord.getDrawCashAmount(),
                vvDrawCashRecord.getDrawCashAmount(), vvDrawCashRecord.getBuyerId());

        return R.ok("提现成功");
    }

    /***
     * 奖励 拒绝
     * http://localhost:8888/mm/activity/agree
     */
    @Describe("拒绝提现")
    @RequestMapping("/reject")
    public R reject(@RequestBody VvDrawCashRecordRejectRequest request) {

        VvDrawCashRecordEntity vvDrawCashRecord = vvDrawCashRecordDao.selectVvDrawCashRecordById(request.getDrawCashRecordId());
        if (StringUtils.notEquals(vvDrawCashRecord.getStatus(), ApprovalEnums.apply.getStatus())) {
            return R.error("状态不对");
        }

        vvDrawCashRecord.setStatus(ApprovalEnums.reject.getStatus());
        vvDrawCashRecord.setRejectReason(request.getRejectReason());
        vvDrawCashRecordDao.updateVvDrawCashRecordById(vvDrawCashRecord);

        vvBuyerDao.updateVvBuyerDrawFailedById(vvDrawCashRecord.getDrawCashAmount(),
                vvDrawCashRecord.getDrawCashAmount(), vvDrawCashRecord.getBuyerId());

        return R.ok("拒绝成功");
    }


}
