package com.fzm.mall.server.admin.distribution.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.distribution.entity.dto.ParcelCenterDTO;
import com.fzm.mall.server.admin.distribution.service.IParcelCenterService;
import com.fzm.mall.server.admin.home_page.model.Admin;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 物流中心-包裹中心 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-03
 */
@Api(tags = "商户-物流中心")
@RestController
@RequestMapping("/parcelCenter/parcelCenterPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ParcelCenterController {

    private final IParcelCenterService parcelCenterService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "物流中心-包裹中心-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/{orderDeliveryId}")
    public ResponseVO<ParcelCenterDTO> read(
            @ApiParam(value = "提货定单号", required = true) @PathVariable(value = "orderDeliveryId") String orderDeliveryId) {
        ParcelCenterDTO result = parcelCenterService.read(orderDeliveryId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "物流中心-包裹中心-列表查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "包裹单号") @RequestParam(required = false) String packageNum,
                           @ApiParam(value = "用户编号") @RequestParam(required = false) String uid,
                           @ApiParam(value = "提货订单编号") @RequestParam(required = false) String orderDeliveryId,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum,

                           @ApiParam(value = "提货状态，0->待发货，1->已发货，2->已收货，3->已撤销，4->待退货，5->已退货") @RequestParam(required = false) Integer deliveryState) {
        Page<ParcelCenterDTO> page = new Page<>();
        if(ObjectUtils.isNotEmpty(pageSize) && ObjectUtils.isNotEmpty(pageNum)){
            page.setSize(pageSize);
            page.setCurrent(pageNum);
        }
        Admin admin = adminService.getByAdminId(adminId);

        String merchantId = null;
        if(!admin.getMerchantId().equals(MALLGlobalConfig.PLATFORM_MERCHANT_ID)){
            merchantId = admin.getMerchantId();
        }
        IPage<ParcelCenterDTO> result = parcelCenterService.page(page, packageNum, uid, orderDeliveryId, deliveryState, merchantId);
        return resFac.getSuccessResponseWithData(result);
    }
}
