package com.heyu.api.controller.vv;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.AppPromoterTradeOrderRequest;
import com.heyu.api.alibaba.request.mm.order.PromoterTradeOrderAwardDTO;
import com.heyu.api.common.annotation.AppRealyLogin;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvPromoterTradeOrderAwardDao;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderAwardEntity;
import com.heyu.api.data.kuaidi100.ActionDTO;
import com.heyu.api.data.service.impl.AbstractDrawAction;
import com.heyu.api.data.utils.R;
import com.heyu.api.utils.ISelect;
import com.heyu.api.utils.PPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/app/promoter/award")
public class AppPromoterTradeOrderAwardController {

    @Autowired
    private VvPromoterTradeOrderAwardDao vvPromoterTradeOrderAwardDao;


    /***
     * 奖励 列表
     * http://localhost:8888/app/promoter/award/list
     */
    @Describe("我的奖励列表")
    @RequestMapping("/list")
    @AppRealyLogin
    public R list(@RequestBody AppPromoterTradeOrderRequest request) {
        PPageUtils pageUtils = PPageUtils.startPage(request.getPageNum(), request.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvPromoterTradeOrderAwardDao.selectAppByCondition(page,
                                request.getBuyerId(),
                                request.getStatus(),
                                request.getMinCreateTimestamp(),
                                request.getMaxCreateTimestamp(),
                                request.getCreateTimestampSort()
                        );
                    }
                });


        List<VvPromoterTradeOrderAwardEntity> vvPromoterTradeOrderAwardEntities = pageUtils.getRows();
        List<PromoterTradeOrderAwardDTO> promoterTradeOrderAwardDTOList = new ArrayList<>();
        for (VvPromoterTradeOrderAwardEntity vvPromoterTradeOrderAwardEntity : vvPromoterTradeOrderAwardEntities) {
            PromoterTradeOrderAwardDTO promoterTradeOrderAwardDTO = new PromoterTradeOrderAwardDTO();
            BeanUtils.copyProperties(vvPromoterTradeOrderAwardEntity, promoterTradeOrderAwardDTO);
            List<ActionDTO> actionDTOS = AbstractDrawAction.getDrawActionDTO(vvPromoterTradeOrderAwardEntity, true);
            promoterTradeOrderAwardDTO.setActionDTOList(actionDTOS);
            promoterTradeOrderAwardDTOList.add(promoterTradeOrderAwardDTO);
        }
        pageUtils.setRows(promoterTradeOrderAwardDTOList);
        return R.ok().setData(pageUtils);
    }

}
