
package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.VvPromoterBuyerRequest;
import com.heyu.api.alibaba.request.mm.order.VvPromoterBuyerDTO;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvPromoterBuyerDao;
import com.heyu.api.data.dao.vv.VvPromoterDao;
import com.heyu.api.data.entity.vv.VvPromoterBuyerEntity;
import com.heyu.api.data.entity.vv.VvPromoterEntity;
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.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/buyer")
public class AdminPromoterBuyerController {

    @Autowired
    private VvPromoterBuyerDao vvPromoterBuyerDao;


    @Autowired
    private VvPromoterDao vvPromoterDao;

    /***
     * 列表
     * http://localhost:8888/mm/promoter/buyer/list
     */
    @Describe("推广者列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvPromoterBuyerRequest vvPromoterRequest) {
        PPageUtils pageUtils = PPageUtils.startPage(vvPromoterRequest.getPageNum(), vvPromoterRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvPromoterBuyerDao.selectBuyerByCondition(page,
                                vvPromoterRequest.getPromoterId(),
                                vvPromoterRequest.getPromoterBuyerId(),
                                vvPromoterRequest.getPromoterBuyerName(),
                                vvPromoterRequest.getPromoterBuyerWeixin(),
                                vvPromoterRequest.getPromoterBuyerPhone(),
                                vvPromoterRequest.getMinCreateTimestamp(),
                                vvPromoterRequest.getMaxCreateTimestamp(),
                                vvPromoterRequest.getCreateTimestampSort()
                        );
                    }
                });


        List<VvPromoterBuyerEntity> vvPromoterBuyerEntities = pageUtils.getRows();

        List<Long> promoterIdList = SanUtils.getFieldList(vvPromoterBuyerEntities, VvPromoterBuyerEntity::getPromoterId);

        List<VvPromoterEntity> vvPromoterEntities = vvPromoterDao.selectVvPromoterByIdList(promoterIdList);

        Map<Long, VvPromoterEntity> vvPromoterEntityMap = SanUtils.groupByFiled2Map(vvPromoterEntities, VvPromoterEntity::getId);
        List<VvPromoterBuyerDTO > vvPromoterBuyerDTOList = new ArrayList<>();
        for (VvPromoterBuyerEntity vvPromoterBuyerEntity : vvPromoterBuyerEntities) {
            VvPromoterBuyerDTO vvPromoterBuyerDTO = new VvPromoterBuyerDTO();
            vvPromoterBuyerDTO.setVvPromoterEntity(vvPromoterEntityMap.get(vvPromoterBuyerEntity.getPromoterId()));
        }

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

}