package com.gree.mall.manager.controller.order;


import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.PayDetail;
import com.gree.mall.manager.bean.listvo.OrderInfoVO;
import com.gree.mall.manager.bean.order.*;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.order.OrderOfflineLogic;
import com.gree.mall.manager.utils.IpUtil;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


@Slf4j
@RestController
@Api(value = "订单线下销售单管理", tags = {"订单线下销售单管理"})
@RequestMapping(value = "/order/offline", produces = "application/json; charset=utf-8")
public class OrderOfflineController {

    @Autowired
    OrderOfflineLogic orderOfflineLogic;

    @ZfireList
    @PostMapping("/list")
    @ApiOperation(value = "列表")
    public ResponseHelper<IPage<OrderInfoVO>> pageV2(
            @RequestBody ZfireParamBean zfireParamBean
    ) {
        IPage<OrderInfoVO> page = orderOfflineLogic.list(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<OrderInfoVO>() {});
    }

    @PostMapping("/list/export")
    @ApiOperation(value = "导出")
    public void listExport(@RequestBody ZfireParamBean zfireParamBean, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.组装查询条件
        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
        //2.查询要导出的内容
        IPage<OrderInfoVO> page = orderOfflineLogic.list(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParam.getExportFields(), request, response);
    }



    @PostMapping("/save")
    @ApiOperation("保存线下销售单(传orderId则编辑)")
    public ResponseHelper addOffline(
            @ApiParam(required = true)@Valid @RequestBody OrderInfoAddBean orderInfoAddBean
    ) throws RemoteServiceException {
        String orderId = orderOfflineLogic.addOfflineOrder(orderInfoAddBean);
        return ResponseHelper.success(orderId);
    }

    @PostMapping("/detail")
    @ApiOperation("详情")
    public ResponseHelper<OrderInfoDetailBean> detail(
            @RequestParam String orderId
    ){
        OrderInfoDetailBean detail = orderOfflineLogic.detail(orderId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/examine")
    @ApiOperation("审核")
    public ResponseHelper<PayDetail> examine(
            @RequestParam String orderId,
            @RequestParam ExamineStatusEnum examineStatus,
            @RequestParam String examineRemark,
            HttpServletRequest request
    ) throws Exception {
        String ip = IpUtil.getIpAddr(request);
        PayDetail payDetail = orderOfflineLogic.examine(orderId, examineStatus,examineRemark, ip);
        return ResponseHelper.success(payDetail);
    }


}
