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


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.constant.MALLGlobalConfig;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
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.order.entity.dto.DeliveryDTO;
import com.fzm.mall.server.admin.order.service.IDeliveryService;
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-18
 */
@Api(tags = "订单中心")
@RestController
@RequestMapping("/delivery/deliveryPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DeliveryController {

    private final IDeliveryService deliveryService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "订单中心-提货订单-发货")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "订单中心-提货订单-发货", module = OperateModuleEnum.ORDER_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/send")
    public ResponseVO send(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "提货订单编号", required = true) @RequestParam String orderDeliveryId,
                           @ApiParam(value = "快递公司缩写", required = true) @RequestParam String expressType,
                           @ApiParam(value = "快递单号", required = true) @RequestParam String expressNo,
                           @ApiParam(value = "更新时间", required = true) @RequestParam Long updateTime) {

        deliveryService.send(adminId, orderDeliveryId, expressType, expressNo, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "订单中心-提货订单-退货审核")
    @ApiOperationSupport(order = 2)
    @AdminLog(description = "订单中心-提货订单-退货审核", module = OperateModuleEnum.ORDER_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/examine")
    public ResponseVO examine(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                              @ApiParam(value = "提货订单编号", required = true) @RequestParam String orderDeliveryId,
                              @ApiParam(value = "退货商家收货地址", required = true) @RequestParam String addressId,
                              @ApiParam(value = "审核是否通过（通过:true;否:false）") @RequestParam Boolean isApproved,
                              @ApiParam(value = "备注") @RequestParam String remark,
                              @ApiParam(value = "更新时间", required = true) @RequestParam Long updateTime) {

        deliveryService.examine(adminId, orderDeliveryId, addressId, isApproved, remark, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "订单中心-提货订单-主键查询")
    @ApiOperationSupport(order = 3)
    @GetMapping("/{orderDeliveryId}")
    public ResponseVO<DeliveryDTO> read(
            @ApiParam(value = "orderDeliveryId", required = true) @PathVariable(value = "orderDeliveryId") String orderDeliveryId) {
        DeliveryDTO result = deliveryService.read(orderDeliveryId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "订单中心-提货订单-列表查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/list")
    public ResponseVO list(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                           @ApiParam(value = "用户编号") @RequestParam(required = false) String uid,
                           @ApiParam(value = "提货订单编号") @RequestParam(required = false) String orderDeliveryId,
                           @ApiParam(value = "商品编号") @RequestParam(required = false) String gid,
                           @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<DeliveryDTO> 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<DeliveryDTO> result = deliveryService.page(page, uid, orderDeliveryId, gid, deliveryState, merchantId);
        return resFac.getSuccessResponseWithData(result);
    }

    @ApiOperation(value = "订单中心-提货订单-商家审核退货签收")
    @ApiOperationSupport(order = 5)
    @AdminLog(description = "订单中心-提货订单-商家审核退货签收", module = OperateModuleEnum.ORDER_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/returnReceiptExamine")
    public ResponseVO returnReceiptExamine(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                                           @ApiParam(value = "提货订单编号", required = true) @RequestParam String orderDeliveryId,
                                           @ApiParam(value = "审核是否通过（通过:true;否:false）") @RequestParam Boolean isApproved,
                                           @ApiParam(value = "备注") @RequestParam String remark,
                                           @ApiParam(value = "更新时间", required = true) @RequestParam Long updateTime) {

        deliveryService.returnReceiptExamine(adminId, orderDeliveryId, isApproved, remark, updateTime);
        return resFac.getSimpleSuccessResponse();
    }
}
