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.common.utils.ServletUtils;
import com.fs.live.domain.LiveAfterSales;
import com.fs.live.domain.LiveAfterSalesItem;
import com.fs.live.domain.LiveOrder;
import com.fs.live.param.LiveAfterSalesApplyParam;
import com.fs.live.param.LiveAfterSalesDeliveryParam;
import com.fs.live.param.LiveAfterSalesListUParam;
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.LiveAfterSalesListUVO;
import com.fs.live.vo.LiveOrderItemListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.media.jfxmedia.logging.Logger;
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 getStoreOrderItems(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        log.info("获取订单项列表，订单号：{}", orderId);

        List<LiveOrderItemListUVO> list=itemService.selectLiveOrderItemListUVOByOrderId(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 LiveAfterSalesApplyParam param) {
        log.info("申请售后，用户ID：{}，参数：{}", getUserId(), param);

        return storeAfterSalesService.applyAfterSales(getUserId(), param);
    }
    @Login
    @PostMapping("/revoke")
    @ApiOperation(value = "撤销售后", notes = "撤销售后")
    @RepeatSubmit
    public R revoke(@RequestBody LiveAfterSalesRevokeParam param) throws ParseException {
        log.info("撤销售后，用户ID：{}，参数：{}", getUserId(), param);

        return storeAfterSalesService.revoke(getUserId(), param);
    }
    @Login
    @PostMapping("/addDelivery")
    @ApiOperation(value = "提交物流信息", notes = "提交物流信息")
    public R addDelivery(@Validated @RequestBody LiveAfterSalesDeliveryParam param) {
        log.info("提交物流信息，参数：{}", param);

        param.setUserId(Long.parseLong(getUserId()));
        return storeAfterSalesService.addDelivery(param);
    }


    @Login
    @GetMapping("/getStoreAfterSalesList")
    @ApiOperation(value = "获取售后列表", notes = "获取售后列表")
    public R getStoreAfterSalesList(LiveAfterSalesListUParam param) {
        log.info("获取售后列表，用户ID：{}，参数：{}", getUserId(), param);

        param.setUserId(Long.parseLong(getUserId()));
        PageHelper.startPage(Integer.parseInt(ServletUtils.getParameter("pageNum")) ,Integer.parseInt(ServletUtils.getParameter("pageSize")) );        List<LiveAfterSalesListUVO> list=storeAfterSalesService.selectLiveAfterSalesListUVO(param);
        PageInfo<LiveAfterSalesListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @GetMapping("/getStoreAfterSalesById")
    @ApiOperation(value = "获取售后详情", notes = "获取售后详情")
    public R getStoreAfterSalesById(@RequestParam("id") Long id) {
        log.info("获取售后详情，售后ID：{}", id);

        LiveAfterSales sales=storeAfterSalesService.selectLiveAfterSalesById(id);
        LiveAfterSalesItem map=new LiveAfterSalesItem();
        map.setAfterSalesId(id);
        List<LiveAfterSalesItem>  items=salesItemService.selectLiveAfterSalesItemList(map);
        LiveOrder order=orderService.selectLiveOrderByOrderId(String.valueOf(sales.getOrderId()));
        return R.ok().put("sales",sales).put("items", items).put("order",order);
    }

}
