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.common.utils.ParseUtils;
import com.fs.live.domain.LiveAfterSales;
import com.fs.live.domain.LiveAfterSalesItem;
import com.fs.live.domain.LiveOrder;
import com.fs.live.domain.LiveOrderItem;
import com.fs.live.param.LiveAfterSalesDeliveryParam;
import com.fs.live.param.LiveAfterSalesParam;
import com.fs.live.param.LiveAfterSalesQueryParam;
import com.fs.live.param.LiveAfterSalesRevokeParam;
import com.fs.live.service.ILiveAfterSalesItemService;
import com.fs.live.service.ILiveAfterSalesService;
import com.fs.live.service.ILiveOrderItemService;
import com.fs.live.service.ILiveOrderService;
import com.fs.live.vo.LiveAfterSalesQueryVO;
import com.fs.live.vo.LiveOrderItemVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@Api("售后接口")
@RestController
@RequestMapping(value="/app/live/storeAfterSales")
public class LiveAfterSalesController extends AppBaseController {

    @Autowired
    private ILiveAfterSalesService storeAfterSalesService;

    @Autowired
    private ILiveAfterSalesItemService salesItemService;
    @Autowired
    private ILiveOrderItemService itemService;
    @Autowired
    private ILiveOrderService orderService;
    @Login
    @ApiOperation("获取订单项列表")
    @GetMapping("/getStoreOrderItems")
    public R getMyStoreOrderById(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        List<LiveOrderItem> list=itemService.selectCheckedByOrderId(orderId);
        LiveOrder order=orderService.selectLiveOrderByOrderId(String.valueOf(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 LiveAfterSalesParam param) {
        return storeAfterSalesService.applyForAfterSales(getUserId(), param);
    }
    @Login
    @PostMapping("/revoke")
    @ApiOperation(value = "撤销售后", notes = "撤销售后")
    @RepeatSubmit
    public R revoke(@RequestBody LiveAfterSalesRevokeParam param) throws ParseException {
        return storeAfterSalesService.revoke(getUserId(), param);
    }
    @Login
    @PostMapping("/addDelivery")
    @ApiOperation(value = "提交物流信息", notes = "提交物流信息")
    public R addDelivery(@Validated @RequestBody LiveAfterSalesDeliveryParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return storeAfterSalesService.addDelivery(param);
    }



    @Login
//    @GetMapping("/getAfterSalesList")
    @GetMapping("/getStoreAfterSalesList")
    @ApiOperation(value = "获取售后列表", notes = "获取售后列表")
    public R getAfterSalesList(LiveAfterSalesQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<LiveAfterSalesQueryVO> list=storeAfterSalesService.selectLiveAfterSalesListQuery(param);
        PageInfo<LiveAfterSalesQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @GetMapping("/getAfterSalesDetails")
    @ApiOperation(value = "获取售后详情", notes = "获取售后详情")
    public R getAfterSalesDetails(@RequestParam("salesId") Long id) {
        LiveAfterSales sales=storeAfterSalesService.selectLiveAfterSalesById(id);
        List<LiveAfterSalesItem>  items=salesItemService.selectLiveAfterSalesItemByAfterId(id);
        return R.ok().put("sales",sales).put("items", items);
    }

    @Login
    @GetMapping("/getStoreAfterSalesById")
    @ApiOperation(value = "获取售后详情", notes = "获取售后详情")
    public R getStoreAfterSalesById(@RequestParam("id") Long id) {
        LiveAfterSales sales=storeAfterSalesService.selectLiveAfterSalesById(id);
        List<LiveAfterSalesItem>  items=salesItemService.selectLiveAfterSalesItemByAfterId(id);
        LiveOrder order=orderService.selectLiveOrderByOrderId(String.valueOf(sales.getOrderId()));
        return R.ok().put("sales",sales).put("items", items).put("order",order);
    }

}
