package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.VvBuyerRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvBuyerAddressDao;
import com.heyu.api.data.dao.vv.VvBuyerDao;
import com.heyu.api.data.entity.vv.VvBuyerEntity;
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.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("/mm/buyer")
public class AdminBuyerController {


    @Autowired
    private VvBuyerDao vvBuyerDao;

    @Autowired
    private VvBuyerAddressDao vvBuyerAddressDao;


    //@Autowired
    //private VvPromoterDao vvPromoterDao;

    /***
     * 列表
     * http://localhost:8888/mm/buyer/list
     */
    @Describe("买家列表")
    @RequestMapping("/list")
    public R list(@RequestBody VvBuyerRequest vvProductRequest) {
        PPageUtils pageUtils = PPageUtils.startPage(vvProductRequest.getPageNum(), vvProductRequest.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvBuyerDao.selectBuyerByCondition(page,
                                vvProductRequest.getBuyerName(),
                                vvProductRequest.getBuyerPhone(),
                                vvProductRequest.getBuyerWeixin(),
                                vvProductRequest.getMinCreateTimestamp(),
                                vvProductRequest.getMaxCreateTimestamp(),
                                vvProductRequest.getIsTest(),
                                vvProductRequest.getPromoterId(),
                                vvProductRequest.getScanCode(),
                                vvProductRequest.getScanCodeAwardCount(),
                                vvProductRequest.getMinPromoterSumAmount(),
                                vvProductRequest.getMaxPromoterSumAmount(),
                                vvProductRequest.getCreateTimestampSort()
                        );
                    }
                });

        List<VvBuyerEntity> vvBuyerEntities = pageUtils.getRows();

        List<Long> buyerIds = new ArrayList<>();

        List<String> promoterIds = new ArrayList<>();

     //   for (VvBuyerEntity buyerEntity : vvBuyerEntities) {
     //       buyerIds.add(buyerEntity.getId());
     //       if (StringUtils.isNotEmpty(buyerEntity.getPromoterId())) {
     //           promoterIds.add(buyerEntity.getPromoterId());
     //       }
     //   }
//
     //   List<VvBuyerAddressEntity> vvBuyerAddressEntities = vvBuyerAddressDao.selectMapByBuyerIds(buyerIds);
     //   Map<Long, List<VvBuyerAddressEntity>> listMap = SanUtils.groupByFiled2MapList(vvBuyerAddressEntities, VvBuyerAddressEntity::getBuyerId);

       //List<VvPromoterEntity> vvPromoterEntities = vvPromoterDao.selectVvPromoterByPromoteIds(promoterIds);
       //Map<String, List<VvPromoterEntity>> vvPromoterGroupBy = SanUtils.groupByFiled2MapList(vvPromoterEntities, VvPromoterEntity::getPromoterId);
       //List<VvBuyerDTO> buyerDTOs = new ArrayList<>();
       //for (VvBuyerEntity vvBuyerAddressEntity : vvBuyerEntities) {
       //    VvBuyerDTO vvBuyerDTO = new VvBuyerDTO();
       //    BeanUtils.copyProperties(vvBuyerAddressEntity, vvBuyerDTO);
       //    vvBuyerDTO.setVvBuyerAddressEntityList(listMap.get(vvBuyerDTO.getId()));

       //    // 获取买家推荐者信息
       //    if (StringUtils.isNotEmpty(vvBuyerDTO.getPromoterId())) {
       //        vvBuyerDTO.setVvPromoterList(vvPromoterGroupBy.get(vvBuyerDTO.getPromoterId()));
       //    }

       //    buyerDTOs.add(vvBuyerDTO);
       //}

        //pageUtils.setRows(buyerDTOs);
        return R.ok().setData(pageUtils);
    }


































}