package com.heyu.api.controller.mm;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heyu.api.alibaba.request.mm.order.VVSellerRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvSellerDao;
import com.heyu.api.data.dao.vv.VvSellerWarehouseDao;
import com.heyu.api.data.dto.SellerDTO;
import com.heyu.api.data.entity.vv.VvSellerEntity;
import com.heyu.api.data.entity.vv.VvSellerWarehouseEntity;
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/seller")
public class AdminSellerController {

    @Autowired
    private VvSellerDao vvSellerDao;


    @Autowired
    private VvSellerWarehouseDao vvSellerWarehouseDao;

    /***
     * 列表
     *  http://localhost:8888/mm/seller/list
     */
    @Describe("后台卖家列表")
    @RequestMapping("/list")
    public R list(@RequestBody VVSellerRequest request) {
        PPageUtils pageUtils = PPageUtils.startPage(request.getPageNum(), request.getPageSize())
                .doSelect(new ISelect() {
                    @Override
                    public List doSelect(IPage page) {
                        return vvSellerDao.selectByAdminCondition(page,
                                request.getSellerId(),
                                request.getSellerName(),
                                request.getCreateTimestampSort(),
                                request.getModifyTimestampSort()
                        );
                    }
                });


        List<VvSellerEntity> vvSellerEntities = pageUtils.getRows();

        List<Long> sellerIds = SanUtils.getFieldList(vvSellerEntities, VvSellerEntity::getId);

        List<VvSellerWarehouseEntity> warehouseEntities = vvSellerWarehouseDao.selectVvSellerWarehouseBySellerIdList(sellerIds);

        Map<Long, List<VvSellerWarehouseEntity>> sellerWarehouseMap = SanUtils.groupByFiled2MapList(warehouseEntities, VvSellerWarehouseEntity::getSellerId);

        List<SellerDTO> sellerDTOList = new ArrayList<>();
        for (VvSellerEntity vvSellerEntity : vvSellerEntities) {
            SellerDTO sellerDTO = new SellerDTO();
            BeanUtils.copyProperties(vvSellerEntity, sellerDTO);
            sellerDTO.setSellerWarehouseList(sellerWarehouseMap.get(vvSellerEntity.getId()));
            sellerDTOList.add(sellerDTO);
        }
        pageUtils.setRows(sellerDTOList);
        return R.ok().setData(pageUtils);
    }

    /***
     *  http://localhost:8888/mm/seller/insertOrUpdate
     *
     *
     */
    @Describe("后台添加卖家")
    @RequestMapping("/insertOrUpdate")
    public R list(@RequestBody VvSellerEntity request) {
        if (request.getId() == null) {
            request.setCreateTimestamp(System.currentTimeMillis());
        }
        request.setModifyTimestamp(System.currentTimeMillis());
        vvSellerDao.insertOrUpdateVvSeller(request);
        return R.ok().setData(request);
    }

}
