package com.sqx.modules.order.controller.app;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
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.EvaluateService;
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.*;

import java.util.List;

@Slf4j
@Api(tags={"用户端-订单"})
@RestController
@RequestMapping("/app/order")
public class AppOrderController {

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

    @Login
    @ApiOperation("生成订单(未结算状态)")
    @PostMapping(value = "/insertOrder")
    public Result insertOrder(@RequestAttribute Long userId, Long shopId, Long goodsId, Integer num, Long skuId, String skuMessage, Integer orderType){

        return appOrderService.insertOrder(userId, shopId, goodsId, num, skuId, skuMessage, orderType);
    }

    @Login
    @ApiOperation("订单备注")
    @PostMapping("/updateOrdersRemark")
    public Result updateOrdersRemark(Long orderId,String remark){
        TbOrder tbOrder = appOrderService.getById(orderId);
        tbOrder.setRemark(remark);
        appOrderService.updateById(tbOrder);
        return Result.success();
    }

    @Login
    @ApiOperation("直接购买单个商品")
    @PostMapping(value = "buyGoods")
    public Result buyGoods(@RequestAttribute Long userId, Long shopId, Long goodsId, Integer num, Long skuId, String skuMessage, Integer orderType){

        return appOrderService.buyGoods(userId, shopId, goodsId, num, skuId, skuMessage, orderType);
    }

    @Login
    @ApiOperation("生成拼团订单（未添加商品状态）")
    @PostMapping(value = "/shareTheBill")
    public Result shareTheBill(@RequestAttribute Long userId, Long shopId){

        return appOrderService.shareTheBill(userId, shopId);
    }

    @Login
    @ApiOperation("查看拼单里的商品")
    @GetMapping(value = "selectShareTheBill")
    public Result selectShareTheBill(@RequestAttribute Long userId, Long orderId){

        return appOrderService.selectShareTheBill(userId, orderId);
    }

    @Login
    @ApiOperation("拼单添加商品")
    @PostMapping(value = "/joinOrder")
    public Result joinOrder(@RequestAttribute Long userId, Long orderId, Long shopId, Long goodsId, Integer num, Long skuId, String skuMessage){

        return appOrderService.joinOrder(userId, orderId, shopId, goodsId, num, skuId, skuMessage);
    }

    @Login
    @ApiOperation("删除拼单中的商品")
    @PostMapping(value = "deleteByGoods")
    public Result deleteByGoods(@RequestAttribute Long userId, Long id){

        return appOrderService.deleteByGoods(userId, id);
    }

    @Login
    @ApiOperation("清空拼单中的商品（仅限自己选中的商品）")
    @PostMapping(value = "deleteShareTheBill")
    public Result deleteShareTheBill(@RequestAttribute Long userId, Long orderId){

        return appOrderService.deleteShareTheBill(userId, orderId);
    }

    @Login
    @ApiOperation("订单发起人取消(删除)该拼团")
    @PostMapping(value = "deleteOrder")
    public Result deleteOrder(@RequestAttribute Long userId, Long orderId){

        return appOrderService.deleteOrder(userId, orderId);
    }

    @Login
    @ApiOperation("修改订单")
    @PostMapping(value = "/updateOrder")
    public Result updateOrder(@RequestBody TbOrder order){

        return appOrderService.updateOrder(order);
    }


    @Login
    @ApiOperation("使用红包")
    @PostMapping(value = "employCoupon")
    public Result employCoupon(@RequestAttribute Long userId, Long parentId, Long couponId){

        return appOrderService.employCoupon(userId, parentId, couponId);
    }

    @Login
    @ApiOperation("重新进入订单结算页面时，删除选中的优惠券")
    @PostMapping(value = "deleteCouponByOrderId")
    public Result deleteCouponByOrderId(Long orderId){

        return appOrderService.deleteCouponByOrderId(orderId);
    }

    @Login
    @ApiOperation("删除未结算订单")
    @PostMapping(value = "/deleteOrderList")
    public Result deleteOrderList(List<Integer> orderId){

        return appOrderService.deleteOrderList(orderId);
    }

    @Login
    @ApiOperation("查看所有订单")
    @GetMapping(value = "/selectAllOrderList")
    public Result selectOrderList(@RequestAttribute Long userId, Long shopId, Integer status, Integer orderType, Integer page, Integer limit){

        return appOrderService.selectOrderList(userId, shopId, status, orderType, page, limit);
    }

    @Login
    @ApiOperation("查看直接购买商品详情")
    @GetMapping(value = "selectBuyGoods")
    public Result selectBuyGoods(@RequestAttribute Long userId, Long orderId){

        return appOrderService.selectBuyGoods(userId, orderId);
    }

    @Login
    @ApiOperation("用户在该商品下的订单回显")
    @GetMapping(value = "echoOrder")
    public Result echoOrder(@RequestAttribute Long userId, Long shopId, Long goodsId){

        return appOrderService.echoOrder(userId, shopId, goodsId);
    }

    @Login
    @ApiOperation("修改购物车中的商品数量")
    @GetMapping(value = "updateGoodsNum")
    public Result updateGoodsNum(Long orderGoodsId, Integer type, Integer num, Long shopId){

        return appOrderService.updateGoodsNum(orderGoodsId, type, num, shopId);
    }

    @Login
    @ApiOperation("查看购物车中的商品")
    @GetMapping(value = "selectShoppingTrolley")
    public Result selectShoppingTrolley(@RequestAttribute Long userId){

        return appOrderService.selectShoppingTrolley(userId);
    }

    @Login
    @ApiOperation("查看购物车中某个商铺的商品")
    @GetMapping(value = "selectShoppingTrolleyByShopId")
    public Result selectShoppingTrolleyByShopId(@RequestAttribute Long userId, Long shopId){

        return appOrderService.selectShoppingTrolleyByShopId(userId, shopId);
    }

    @Login
    @ApiOperation("清空购物车")
    @PostMapping(value = "emptyShoppingTrolley")
    public Result emptyShoppingTrolley(@RequestAttribute Long userId, Long shopId){

        return appOrderService.emptyShoppingTrolley(userId, shopId);
    }

    @Login
    @ApiOperation("待取餐/配送")
    @GetMapping(value = "/waitTakeFood")
    public Result waitTakeFood(@RequestAttribute Long userId, Integer orderType, Integer page, Integer limit, Integer status){

        return appOrderService.waitTakeFood(userId, orderType, page, limit, status);
    }

    @ApiOperation("查看订单详情")
    @GetMapping(value = "selectOrderById")
    public Result selectOrderById(Long orderId){

        return appOrderService.selectOrderById(orderId);
    }

    @Login
    @ApiOperation("用户选择完成订单")
    @PostMapping(value = "accomplishOrder")
    public Result accomplishOrder(Long orderId){
        log.info("操作完成订单3："+orderId);
        return appOrderService.accomplishOrder( orderId);
    }

    @Login
    @ApiOperation("用户取消订单")
    @GetMapping(value = "userCancelOrder")
    public Result userCancelOrder(Long orderId){

        return appOrderService.userCancelOrder(orderId);
    }

    @Login
    @ApiOperation("用户评价订单（商铺、商品）")
    @PostMapping(value = "insertEvaluate")
    public Result insertEvaluate(@RequestAttribute Long userId, @RequestBody Evaluate evaluate){
        evaluate.setUserId(userId);
        return appOrderService.insertEvaluate(evaluate);
    }
    @Login
    @ApiOperation("获取我的评价列表")
    @GetMapping(value = "getEvaluateList")
    public Result getEvaluateList(@RequestAttribute Long userId, Evaluate evaluate,Integer page,Integer limit){
        evaluate.setUserId(userId);
        return Result.success().put("data", appOrderService.getEvaluateList(evaluate,page,limit));
    }
    @Login
    @ApiOperation("用户删除评价")
    @GetMapping(value = "deleteEvaluateById")
    public Result deleteEvaluateById(@RequestAttribute("userId")Long userId, Long id){
        return  appOrderService.deleteEvaluateById(userId,id);
    }

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


}
