package com.heyu.api.controller.vv;


import com.heyu.api.alibaba.request.mm.AppPromoterTradeOrderDrawRequest;
import com.heyu.api.common.annotation.AppRealyLogin;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvBuyerDao;
import com.heyu.api.data.dao.vv.VvPromoterTradeOrderAwardDao;
import com.heyu.api.data.dao.vv.VvPromoterTradeOrderLineAwardDao;
import com.heyu.api.data.dao.vv.VvTradeOrderDao;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderAwardEntity;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderLineAwardEntity;
import com.heyu.api.data.enums.ActionEnums;
import com.heyu.api.data.enums.AwardStatusEnums;
import com.heyu.api.data.service.impl.AbstractDrawAction;
import com.heyu.api.data.utils.R;
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.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app/draw")
public class AppDrawController {


    @Autowired
    private VvPromoterTradeOrderAwardDao vvPromoterTradeOrderAwardDao;


    @Autowired
    private VvBuyerDao vvBuyerDao;


    @Autowired
    private VvPromoterTradeOrderLineAwardDao vvPromoterTradeOrderLineAwardDao;


    @Autowired
    private VvTradeOrderDao vvTradeOrderDao;

    /***
     * 奖励 列表
     * http://localhost:8888/app/draw/apply
     */
    @Describe("app申请提现")
    @RequestMapping("/apply")
    @AppRealyLogin
    public R draw(@RequestBody AppPromoterTradeOrderDrawRequest request) {
        List<VvPromoterTradeOrderAwardEntity>
                vvPromoterTradeOrderAwardEntityList = vvPromoterTradeOrderAwardDao.selectVvPromoterTradeOrderAwardByPromoterId(request.getPromoterTradeOrderIdList());

        for (VvPromoterTradeOrderAwardEntity vvPromoterTradeOrderAwardEntity : vvPromoterTradeOrderAwardEntityList) {
            if (AbstractDrawAction.checkFailed(vvPromoterTradeOrderAwardEntity, true, ActionEnums.App_Buyer_Apply_Draw)) {
                return R.error("提现状态不对");
            }
        }


        for (VvPromoterTradeOrderAwardEntity vvPromoterTradeOrderAwardEntity : vvPromoterTradeOrderAwardEntityList) {
            vvPromoterTradeOrderAwardEntity.setStatus(AwardStatusEnums.apply_draw.getStatus());
            vvPromoterTradeOrderAwardEntity.setGmtApplyDraw(new Date());
            vvPromoterTradeOrderAwardDao.updateVvPromoterTradeOrderAwardById(vvPromoterTradeOrderAwardEntity);

            vvBuyerDao.updateVvBuyerByIdPromoterApplyDrawCashAmountPromoterArriveAccountAmount(
                    vvPromoterTradeOrderAwardEntity.getAwardAmount()
                    , vvPromoterTradeOrderAwardEntity.getAwardAmount(),
                    vvPromoterTradeOrderAwardEntity.getId());
        }

        List<VvPromoterTradeOrderLineAwardEntity> vvPromoterTradeOrderLineAwardEntities = vvPromoterTradeOrderLineAwardDao
                .selectVvPromoterTradeOrderLineAwardByPromoterIdList(request.getPromoterTradeOrderIdList());
        for (VvPromoterTradeOrderLineAwardEntity vvPromoterTradeOrderLineAwardEntity : vvPromoterTradeOrderLineAwardEntities) {
            vvPromoterTradeOrderLineAwardEntity.setStatus(AwardStatusEnums.apply_draw.getStatus());
            vvPromoterTradeOrderLineAwardEntity.setGmtApplyDraw(new Date());
            vvPromoterTradeOrderLineAwardDao.updateVvPromoterTradeOrderLineAwardById(vvPromoterTradeOrderLineAwardEntity);
        }

        return R.ok();
    }


}
