package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.VvPromoterTradeOrderRequest;
import com.heyu.api.alibaba.request.mm.order.PromoterTradeOrderAwardDTO;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvPromoterTradeOrderAwardDao;
import com.heyu.api.data.dao.vv.VvPromoterTradeOrderLineAwardDao;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderAwardEntity;
import com.heyu.api.data.entity.vv.VvPromoterTradeOrderLineAwardEntity;
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.data.utils.SanUtils;
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;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/mm/promoter/trade/order")
public class AdminPromoterTradeOrderController {

    @Autowired
    private VvPromoterTradeOrderAwardDao vvPromoterTradeOrderAwardDao;

    @Autowired
    private VvPromoterTradeOrderLineAwardDao vvPromoterTradeOrderLineAwardDao;

    /***
     * 列表
     * http://localhost:8888/mm/promoter/trade/order/list
     */
    @Describe("推广者奖励列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvPromoterTradeOrderRequest request) {
        PPageUtils pageUtils = PPageUtils.startPage(request.getPageNum(), request.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvPromoterTradeOrderAwardDao.selectAdminByCondition(page,
                                request.getTradeOrderId(),
                                request.getStatus(),
                                request.getPromoterId(),
                                request.getPromoterBuyerId(),
                                request.getPromoterBuyerName(),
                                request.getPromoterBuyerWeixin(),
                                request.getPromoterBuyerPhone(),
                                request.getWasBuyerId(),
                                request.getWasBuyerName(),
                                request.getWasBuyerWeixin(),
                                request.getWasBuyerPhone(),
                                request.getMinCreateTimestamp(),
                                request.getMaxCreateTimestamp(),
                                request.getCreateTimestampSort()
                        );
                    }
                });

        List<VvPromoterTradeOrderAwardEntity> vvPromoterTradeOrderAwardEntities = pageUtils.getRows();
        List<Long> promoterIdList = SanUtils.list2FieldList(vvPromoterTradeOrderAwardEntities, VvPromoterTradeOrderAwardEntity::getPromoterId);

        List<VvPromoterTradeOrderLineAwardEntity> promoterTradeOrderLineAwardList =
                vvPromoterTradeOrderLineAwardDao.selectVvPromoterTradeOrderLineAwardByIdList(promoterIdList);

        Map<Long, List<VvPromoterTradeOrderLineAwardEntity>> promoterTradeOrderLineAwardMap =
                SanUtils.groupByFiled2MapList(promoterTradeOrderLineAwardList, VvPromoterTradeOrderLineAwardEntity::getPromoterId);

        List<PromoterTradeOrderAwardDTO> promoterTradeOrderAwardDTOList = new ArrayList<>();
        for (VvPromoterTradeOrderAwardEntity vvPromoterTradeOrderAwardEntity : vvPromoterTradeOrderAwardEntities) {
            PromoterTradeOrderAwardDTO promoterTradeOrderAwardDTO = new PromoterTradeOrderAwardDTO();
            BeanUtils.copyProperties(vvPromoterTradeOrderAwardEntity, promoterTradeOrderAwardDTO);
            List<ActionDTO> actionDTOS = AbstractDrawAction.getDrawActionDTO(vvPromoterTradeOrderAwardEntity, false);
            promoterTradeOrderAwardDTO.setActionDTOList(actionDTOS);

            promoterTradeOrderAwardDTO.setVvPromoterTradeOrderLineAwardEntityList(
                    promoterTradeOrderLineAwardMap.get(vvPromoterTradeOrderAwardEntity.getPromoterId()));
            promoterTradeOrderAwardDTOList.add(promoterTradeOrderAwardDTO);
        }

        pageUtils.setRows(promoterTradeOrderAwardDTOList);
        return R.ok().setData(pageUtils);
    }


}
