package com.fs.app.controller.live;

import com.fs.app.annotation.Login;
import com.fs.app.controller.AppBaseController;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.hisStore.param.*;
import com.fs.hisStore.service.IMergedOrderService;
import com.fs.hisStore.vo.FsMergedOrderListQueryVO;
import com.fs.hisStore.vo.MergedAfterSalesVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;

@Api("订单信息接口")
@RestController("AppOrderController")
@AllArgsConstructor
@RequestMapping(value="/app/order")
public class OrderController extends AppBaseController {

    private final IMergedOrderService mergedOrderService;

    @Login
    @ApiOperation("获取我的合并订单列表（商城订单+直播订单）")
    @GetMapping("/getMyMergedOrderList")
    public R getMyMergedOrderList(FsMyStoreOrderQueryParam param, HttpServletRequest request)
    {
        // 设置分页参数
        if (param.getPage() == null || param.getPage() < 1)
        {
            param.setPage(1);
        }
        if (param.getPageSize() == null || param.getPageSize() < 1)
        {
            param.setPageSize(10);
        }
        
        PageHelper.startPage(param.getPage(), param.getPageSize());
        
        // 设置用户ID
        param.setUserId(Long.parseLong(getUserId()));
        
        // 查询合并订单列表
        List<FsMergedOrderListQueryVO> list = mergedOrderService.selectMergedOrderListVO(param);
        
        // 封装分页信息
        PageInfo<FsMergedOrderListQueryVO> listPageInfo = new PageInfo<>(list);
        
        return R.ok().put("data", listPageInfo);
    }

    @Login
    @GetMapping("/getMergedAfterSalesList")
    @ApiOperation(value = "获取合并售后列表", notes = "获取合并售后列表（商城售后+直播售后）")
    public R getMergedAfterSalesList(MergedAfterSalesQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<MergedAfterSalesVO> list = mergedOrderService.selectMergedAfterSalesList(param);
        PageInfo<MergedAfterSalesVO> listPageInfo = new PageInfo<>(list);
        return R.ok().put("data", listPageInfo);
    }

//    @Login
//    @PostMapping("/applyAfterSales")
//    @ApiOperation(value = "申请售后", notes = "申请售后")
//    @RepeatSubmit
//    public R applyAfterSales(@RequestBody MergedAfterSalesParam param) {
//        return mergedOrderService.applyForAfterSales(getUserId(), param);
//    }
//
//    @Login
//    @PostMapping("/revokeAfterSales")
//    @ApiOperation(value = "撤销售后", notes = "撤销售后")
//    @RepeatSubmit
//    public R revokeAfterSales(@RequestBody MergedAfterSalesRevokeParam param) throws ParseException {
//        return mergedOrderService.revokeAfterSales(getUserId(), param);
//    }
//
//    @Login
//    @PostMapping("/addAfterSalesDelivery")
//    @ApiOperation(value = "提交物流信息", notes = "提交物流信息")
//    public R addAfterSalesDelivery(@Validated @RequestBody MergedAfterSalesDeliveryParam param) {
//        param.setUserId(Long.parseLong(getUserId()));
//        return mergedOrderService.addDelivery(param);
//    }

//    @Login
    @GetMapping("/getMergedAfterSalesDetails")
    @ApiOperation(value = "获取合并售后详情", notes = "获取合并售后详情")
    public R getMergedAfterSalesDetails(@RequestParam("salesId") Long salesId, @RequestParam("afterSalesType") Integer afterSalesType) {
        MergedAfterSalesVO vo = mergedOrderService.selectMergedAfterSalesById(salesId, afterSalesType);
        return R.ok().put("sales", vo);
    }

    @Login
    @PostMapping("/deleteOrder")
    @ApiOperation(value = "删除订单", notes = "删除订单（逻辑删除）")
    @RepeatSubmit
    public R deleteOrder(@Validated @RequestBody MergedOrderDeleteParam param) {
        return mergedOrderService.deleteOrder(getUserId(), param);
    }
}
