package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.store.domain.FsStoreAfterSales;
import com.fs.store.domain.FsStoreAfterSalesItem;
import com.fs.store.domain.FsStoreOrder;
import com.fs.store.param.*;
import com.fs.store.service.*;
import com.fs.store.vo.FsStoreAfterSalesQueryVO;
import com.fs.store.vo.FsStoreOrderItemVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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
@RequestMapping(value="/app/storeAfterSales")
public class StoreAfterSalesController extends  AppBaseController {

    @Autowired
    private IFsStoreAfterSalesService storeAfterSalesService;
    @Autowired
    private IFsStoreAfterSalesItemService salesItemService;
    @Autowired
    private IFsStoreOrderItemService itemService;
    @Autowired
    private IFsStoreOrderService orderService;
    @Login
    @ApiOperation("获取订单项列表")
    @GetMapping("/getMyStoreOrderItemByOrderId")
    public R getMyStoreOrderById(@RequestParam("orderId") Long orderId){
        FsStoreOrder order=orderService.selectFsStoreOrderById(orderId);
        if (order == null) {
            return R.error("订单不存在");
        }
        R denied = denyIfNotCurrentUser(order.getUserId());
        if (denied != null) {
            return denied;
        }
        List<FsStoreOrderItemVO> list=itemService.selectFsStoreOrderItemListByOrderId(orderId);
        order.setUserPhone(ParseUtils.parsePhone(order.getUserPhone()));
        order.setUserAddress(ParseUtils.parseIdCard(order.getUserAddress()));
        return R.ok().put("order", order).put("items",list);
    }


    @Login
    @PostMapping("/applyAfterSales")
    @ApiOperation(value = "申请售后", notes = "申请售后")
    @RepeatSubmit
    public R applyAfterSales(@RequestBody FsStoreAfterSalesParam storeAfterSalesParam) {
        return storeAfterSalesService.applyForAfterSales(Long.parseLong(getUserId()), storeAfterSalesParam);
    }
    @Login
    @PostMapping("/revoke")
    @ApiOperation(value = "撤销售后", notes = "撤销售后")
    @RepeatSubmit
    public R revoke(@RequestBody FsStoreAfterSalesRevokeParam param) throws ParseException {
        return storeAfterSalesService.revoke(Long.parseLong(getUserId()), param.getSalesId());
    }
    @Login
    @PostMapping("/addDelivery")
    @ApiOperation(value = "提交物流信息", notes = "提交物流信息")
    public R addDelivery(@Validated @RequestBody FsStoreAfterSalesDeliveryParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return storeAfterSalesService.addDelivery(param);
    }

    @Login
    @GetMapping("/getAfterSalesList")
    @ApiOperation(value = "获取售后列表", notes = "获取售后列表")
    public R getAfterSalesList(FsStoreAfterSalesQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsStoreAfterSalesQueryVO> list=storeAfterSalesService.selectFsStoreAfterSalesListQuery(param);
        PageInfo<FsStoreAfterSalesQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @GetMapping("/getAfterSalesDetails")
    @ApiOperation(value = "获取售后详情", notes = "获取售后详情")
    public R getAfterSalesDetails(@RequestParam("salesId") Long id) {
        FsStoreAfterSales sales=storeAfterSalesService.selectFsStoreAfterSalesById(id);
        if (sales == null) {
            return R.error("售后单不存在");
        }
        R denied = denyIfNotCurrentUser(sales.getUserId());
        if (denied != null) {
            return denied;
        }
        FsStoreAfterSalesItem map=new FsStoreAfterSalesItem();
        map.setStoreAfterSalesId(id);
        List<FsStoreAfterSalesItem>  items=salesItemService.selectFsStoreAfterSalesItemList(map);
        return R.ok().put("sales",sales).put("items", items);
    }

}
