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.OrderDTO;
import com.fzm.mall.server.admin.order.service.IOrderService;
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.*;

import java.math.BigDecimal;

/**
 * <p>
 * 用户购物订单表 前端控制器
 * </p>
 *
 * @author wulixian
 * @since 2021-03-19
 */
@Api(tags = "订单中心")
@RestController
@RequestMapping("/order/orderPermission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OrderController {

    private final IOrderService orderService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "订单中心-商品订单-修改应付价格")
    @ApiOperationSupport(order = 1)
    @AdminLog(description = "订单中心-商品订单-修改应付价格", module = OperateModuleEnum.ORDER_CENTER, opType = OperateTypeEnum.UPDATE)
    @PutMapping("/consultDiscount")
    public ResponseVO consultDiscount(@ApiParam(value = "管理员id", required = true) @RequestHeader String adminId,
                                      @ApiParam(value = "商品订单编号", required = true) @RequestParam String oid,
                                      @ApiParam(value = "优惠类型(1->客户协商优惠)", required = true) @RequestParam Integer consultDiscountType,
                                      @ApiParam(value = "客户协商优惠金额", required = true) @RequestParam BigDecimal consultDiscountAmount,
                                      @ApiParam(value = "更新时间", required = true) @RequestParam Long updateTime) {

        orderService.consultDiscount(adminId, oid, consultDiscountType, consultDiscountAmount, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

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

        orderService.refundExamine(adminId, oid, isApproved, remark, updateTime);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation(value = "订单中心-商品订单-主键查询")
    @ApiOperationSupport(order = 3)
    @GetMapping("/{oid}")
    public ResponseVO<OrderDTO> read(
            @ApiParam(value = "oid", required = true) @PathVariable(value = "oid") String oid) {
        OrderDTO result = orderService.read(oid);
        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 oid,
                           @ApiParam(value = "每页条数") @RequestParam(required = false) Long pageSize,
                           @ApiParam(value = "页码") @RequestParam(required = false) Long pageNum,

                           @ApiParam(value = "订单状态，1->交易成功，2->交易关闭，3->定金已付，4->待退款，5->已退款，6->支付中，7->待支付") @RequestParam(required = false) Integer orderState) {
        Page<OrderDTO> 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<OrderDTO> result = orderService.page(page, uid, oid, orderState, merchantId);
        return resFac.getSuccessResponseWithData(result);
    }
}
