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.his.domain.FsStoreAfterSales;
import com.fs.his.domain.FsStoreAfterSalesItem;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.param.*;
import com.fs.his.service.IFsStoreAfterSalesItemService;
import com.fs.his.service.IFsStoreAfterSalesService;
import com.fs.his.service.IFsStoreOrderItemService;
import com.fs.his.service.IFsStoreOrderService;
import com.fs.his.vo.FsStoreAfterSalesListUVO;
import com.fs.his.vo.FsStoreOrderItemListUVO;
import com.fs.his.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("/getStoreOrderItems")
    public R getStoreOrderItems(@RequestParam("orderId") Long orderId, HttpServletRequest request){
        List<FsStoreOrderItemListUVO> list=itemService.selectFsStoreOrderItemListUVOByOrderId(orderId);
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(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 FsStoreAfterSalesApplyParam param) {
        return storeAfterSalesService.applyAfterSales(Long.parseLong(getUserId()), param);
    }
    @Login
    @PostMapping("/revoke")
    @ApiOperation(value = "撤销售后", notes = "撤销售后")
    @RepeatSubmit
    public R revoke(@RequestBody FsStoreAfterSalesRevokeParam param) throws ParseException {
        return storeAfterSalesService.revoke(Long.parseLong(getUserId()), param);
    }
    @Login
    @PostMapping("/addDelivery")
    @ApiOperation(value = "提交物流信息", notes = "提交物流信息")
    public R addDelivery(@Validated @RequestBody FsStoreAfterSalesDeliveryParam param) {
        param.setUserId(Long.parseLong(getUserId()));
        return storeAfterSalesService.addDelivery(param);
    }


    @Login
    @GetMapping("/getStoreAfterSalesList")
    @ApiOperation(value = "获取售后列表", notes = "获取售后列表")
    public R getStoreAfterSalesList(FsStoreAfterSalesListUParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsStoreAfterSalesListUVO> list=storeAfterSalesService.selectFsStoreAfterSalesListUVO(param);
        PageInfo<FsStoreAfterSalesListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }
    @Login
    @GetMapping("/getStoreAfterSalesById")
    @ApiOperation(value = "获取售后详情", notes = "获取售后详情")
    public R getStoreAfterSalesById(@RequestParam("id") Long id) {
        FsStoreAfterSales sales=storeAfterSalesService.selectFsStoreAfterSalesById(id);
        FsStoreAfterSalesItem map=new FsStoreAfterSalesItem();
        map.setAfterSalesId(id);
        List<FsStoreAfterSalesItem>  items=salesItemService.selectFsStoreAfterSalesItemList(map);
        FsStoreOrder order=orderService.selectFsStoreOrderByOrderId(sales.getOrderId());
        return R.ok().put("sales",sales).put("items", items).put("order",order);
    }

}
