package cn.iocoder.yudao.module.payment.controller;


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.core.page.PageDomain;
import cn.iocoder.yudao.module.core.page.TableSupport;
import cn.iocoder.yudao.module.core.text.Convert;
import cn.iocoder.yudao.module.core.text.ServletUtils;
import cn.iocoder.yudao.module.inspection.service.AppInspectionPartnerService;
import cn.iocoder.yudao.module.payment.entity.OrderInfo;
import cn.iocoder.yudao.module.payment.entity.commentVo;
import cn.iocoder.yudao.module.payment.service.OrderInfoService;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.module.shop.service.IShopMallPartnersService;
import cn.iocoder.yudao.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RequestMapping("/admin-api/orderApi")
@RestController
public class OrderController extends BaseController {

    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private IShopMallPartnersService shopMallPartnersService;
    @Autowired
    private AppInspectionPartnerService partnerService;


    @PostMapping("/createOrder")
    public CommonResult createOrder(@RequestBody OrderInfo orderInfo) throws Exception {
        return CommonResult.success(orderInfoService.createOrder(orderInfo));
    }


    @GetMapping("/orderDetail")
    public CommonResult orderDetail(Long goodsId, String type, String title, Integer amount) {
        return CommonResult.success(orderInfoService.orderDetail(goodsId, type, title, amount));
    }

    @GetMapping("/pickCarDetail")
    public CommonResult pickCarDetail(Long pickCarId) {
        return CommonResult.success(orderInfoService.pickCarDetail(pickCarId));
    }

    //取消支付
    @PostMapping("/cancelPay")
    public CommonResult cancelPay(Long orderId) {
        orderInfoService.cancelPay(orderId);
        return CommonResult.ok();
    }

    @GetMapping("/orderList")
    public CommonResult orderList(String status, String title,
                                  @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                  @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) {
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);
        IPage<OrderInfo> orderInfos = orderInfoService.orderList(page,status, title);
        return success(orderInfos);

    }

    //  用于自营维修保养核销
    @PostMapping("/validation")
    public CommonResult validation(@RequestBody Map<String, Object> requestBody) {
        Object accessCodeValue = requestBody.get("accessCode");
        if (accessCodeValue != null) {
            String accessCode = accessCodeValue.toString();
            return CommonResult.success(orderInfoService.validation(accessCode));
        } else {
            return CommonResult.error(500,"核销码错误");
        }
    }

    // 用于商城核销
    @PostMapping("/validationMall")
    public CommonResult validationMall(@RequestBody Map<String, Object> requestBody) {
        Object accessCodeValue = requestBody.get("accessCode");
        if (accessCodeValue != null) {
            String accessCode = accessCodeValue.toString();
            return CommonResult.success(orderInfoService.validationMall(accessCode));
        } else {
            return CommonResult.error(500,"核销码错误");
        }
    }

    // 核销记录By核销人Id 微信端
    @GetMapping("/validationListWx")
    public CommonResult validationListWx(@RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) {
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);
        IPage<OrderInfo> list = orderInfoService.validationListWx(page);
        return success(list);
    }

    // 商城核销记录By核销人Id PC
    @GetMapping("/validationListPc")
    public CommonResult validationListPc(@RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) {
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);
        IPage<OrderInfo> list = orderInfoService.validationListPc(page);
        return success(list);
    }


    //评论商品和星级
    @PostMapping("/reviewOrder")
    public CommonResult reviewOrder(String orderId, Integer starLevel, String reviewStr) throws Exception {
        orderInfoService.reviewOrder(orderId, starLevel, reviewStr);
        return CommonResult.ok();
    }

    /**
     * 查询检测订单列表
     */
    @GetMapping("/orderListSystem")
    public CommonResult orderListSystem(OrderInfo shopInspectionOrder,
                                        @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                        @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) {
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);
        IPage<OrderInfo> list = orderInfoService.orderListSystem(page,shopInspectionOrder);
        return success(list);
    }

    /**
     * 订单评价
     */

    @PostMapping("/commentOrder")
    public CommonResult commentOrder(@RequestBody OrderInfo orderInfo) throws Exception {
        OrderInfo curOrderInfo = orderInfoService.getById(orderInfo.getId());
        if (!Objects.equals(curOrderInfo.getUserId(), getUserId())) {
            return CommonResult.error(500,"非当前用户订单，禁止评论");
        }
        return CommonResult.success(orderInfoService.commentOrder(orderInfo));
    }

    /**
     * 获取尚未评价的订单
     */

    @GetMapping("/getNoCommentOrder")
    public CommonResult getNoCommentOrder() {
        return CommonResult.success(orderInfoService.getNoCommentOrder());
    }

    /**
     * 获取商品的评价列表
     */

    @GetMapping("/getCommentOrderList")
    public CommonResult getCommentOrderList(Long partnerId,
                                            @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                                            @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) {
        //判断商家是否开启评论区
        ShopMallPartners partners = partnerService.getById(partnerId);
        if (StringUtils.isEmpty(partners.getOpenComment())||partners.getOpenComment().equals("0")){
            return success(new ArrayList<>());
        }
        Page<commentVo> page = new Page<>(pageNum, pageSize);
        IPage<commentVo> commentOrderList = orderInfoService.getCommentOrderList(page,partnerId);
        return success(commentOrderList);
    }

    // pc端合作商订单列表
    @GetMapping("/listPc")
    public CommonResult listPc(OrderInfo orderInfo,
                               @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                               @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize) {
        // 当前合作商的商品
        ShopMallPartners shopMallPartners = shopMallPartnersService.selectShopMallPartnersByUserId(getUserId(),"sc");
        orderInfo.setPartnerId(shopMallPartners.getPartnerId());
        PageDomain pageDomain = TableSupport.getPageDomain();
        Page<OrderInfo> page = new Page<>(pageNum, pageSize);
        IPage<OrderInfo> list = orderInfoService.orderListPc(page,orderInfo);
        return success(list);
    }

    @GetMapping("/getOrderInfo/{id}")
    public CommonResult getOrderInfo(@PathVariable("id") Long id) throws Exception {
        OrderInfo curOrderInfo = orderInfoService.getById(id);
        if (!Objects.equals(curOrderInfo.getUserId(), getUserId())) {
            return CommonResult.error(500,"非当前用户订单");
        }
        return CommonResult.success(curOrderInfo);
    }
}
