package com.sqx.modules.order.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.order.dto.OrderFinishByShopDTO;
import com.sqx.modules.order.dto.OrderQueryDto;
import com.sqx.modules.order.entity.Evaluate;
import com.sqx.modules.order.entity.TbOrder;
import com.sqx.modules.order.service.AppOrderService;
import com.sqx.modules.order.service.EvaluateRiderService;
import com.sqx.modules.order.service.EvaluateService;
import com.sqx.modules.sys.controller.AbstractController;
import icu.xuyijie.secureapi.annotation.DecryptParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Slf4j
@Api(tags = {"管理端-订单"})
@RestController
@RequestMapping("/admin/order")
public class OrderController extends AbstractController {

    @Autowired
    private AppOrderService appOrderService;
    @Autowired
    private EvaluateService evaluateService;

    @Autowired
    private EvaluateRiderService evaluateRiderService;

    @ApiOperation("条件查询所有订单")
    @GetMapping(value = "selectAllOrder")
    public Result selectAllOrder(Integer page, Integer limit, Integer status, String phone, Long shopId, String userName, String orderNumber, Integer orderType, String shopName, String transactionId) {
        return appOrderService.selectAllOrder(page, limit, status, phone, shopId, userName, orderNumber, orderType, shopName, transactionId);
    }

    @ApiOperation("条件查询所有订单(管理端)")
    @GetMapping(value = "selectAllOrderAdmin")
    public Result selectAllOrderAdmin(@Valid OrderQueryDto queryDto) {
        return appOrderService.selectAllOrderAdmin(queryDto);
    }

//    @GetMapping("excelOrder")
//    @ApiOperation("导出卡密列表")
//    public void excelOrder(Integer status, String phone, Long shopId, String userName,
//                           String orderNumber, Integer orderType,String shopName,String riderPhone,
//                           String startTime, String endTime, HttpServletResponse response) throws Exception{
//        ExcelData excelData = appOrderService.excelAllOrderAdmin(status, phone, shopId, userName, orderNumber, orderType, shopName, riderPhone, startTime, endTime);
//        ExportExcelUtils.exportExcel(response,"订单列表.xlsx",excelData);
//    }

    @ApiOperation("完成订单")
    @PostMapping(value = "accomplishOrder")
    public Result accomplishOrder(@Valid OrderFinishByShopDTO orderFinishByShopDTO) {
        appOrderService.finishOrderByShop(orderFinishByShopDTO);

        return Result.success();
    }

    @ApiOperation("添加送达照片")
    @PostMapping(value = "add-receive-img/{orderId}")
    public Result addReceiveImg(@PathVariable Long orderId, @DecryptParam String img) {
        appOrderService.addReceiveImg(orderId, img);

        return Result.success();
    }

    @ApiOperation("管理订单")
    @PostMapping(value = "updateOrder")
    public Result updateOrder(@RequestBody TbOrder tborder) {

        return appOrderService.updateOrder(tborder);
    }

    @ApiOperation("管理端退款")
    @PostMapping(value = "adminCancelOrder")
    public Result adminCancelOrder(Long orderId) {

        return appOrderService.adminCancelOrder(orderId);
    }

    @ApiOperation("商家回复评论")
    @PostMapping(value = "shopReplyEvaluate")
    public Result shopReplyEvaluate(String orderNumber, String shopReplyMessage) {

        return appOrderService.shopReplyEvaluate(orderNumber, shopReplyMessage);
    }

    @ApiOperation("商家删除评论")
    @PostMapping("/deleteEvaluate")
    public Result deleteEvaluate(Long evaluateId) {
        evaluateService.removeById(evaluateId);
        return Result.success();
    }

    @ApiOperation("打印票")
    @PostMapping(value = "/print")
    public Result print(Long orderId, Integer isAppend) {
        return appOrderService.print(orderId, isAppend);
    }

    @ApiOperation("获取用户评价列表")
    @GetMapping(value = "getEvaluateList")
    public Result getEvaluateList(Long userId, Evaluate evaluate, Integer page, Integer limit) {
        evaluate.setUserId(userId);
        return Result.success().put("data", appOrderService.getEvaluateList(evaluate, page, limit));
    }

    @ApiOperation("店铺总交易金额")
    @GetMapping(value = "selectAllOrderTotalPrice")
    public Result selectAllOrderTotalPrice(OrderQueryDto queryDto) {
        return appOrderService.selectAllOrderTotalPrice(queryDto);
    }

    @ApiOperation("骑手评价展示")
    @GetMapping(value = "/evaluateRiderPage")
    public Result evaluateRiderPage(String riderName,String phone,String orderNumber,String userPhone,String shopName,Integer riderScore,String start,String end,Integer page, Integer limit) {
        return evaluateRiderService.evaluateRiderPage(riderName,phone,orderNumber,userPhone,shopName,riderScore,start,end,page,limit);
    }

    @ApiOperation("删除骑手评价")
    @GetMapping(value = "/delete")
    public Result delete(Integer evaluateRiderId) {
        evaluateRiderService.removeById(evaluateRiderId);
        return Result.success();
    }
}
