package com.yonge.cooleshow.portal.controller;

import com.yonge.cooleshow.mall.common.api.CommonPage;
import com.yonge.cooleshow.mall.common.api.CommonResult;
import com.yonge.cooleshow.mbg.model.OmsOrderReturnReason;
import com.yonge.cooleshow.portal.domain.OmsOrderReturnApplyParam;
import com.yonge.cooleshow.portal.dto.OmsOrderReturnApplyResult;
import com.yonge.cooleshow.portal.dto.OmsOrderReturnApplyVo;
import com.yonge.cooleshow.portal.dto.OmsReturnApplyDto;
import com.yonge.cooleshow.portal.dto.OmsReturnApplyQueryParam;
import com.yonge.cooleshow.portal.service.OmsOrderReturnReasonService;
import com.yonge.cooleshow.portal.service.OmsPortalOrderReturnApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 退货申请管理Controller
 * Created by macro on 2018/10/17.
 */
@Controller
@Api(tags = "OmsPortalOrderReturnApplyController 退货申请管理")
@RequestMapping("/returnApply")
public class OmsPortalOrderReturnApplyController {
    @Autowired
    private OmsPortalOrderReturnApplyService returnApplyService;

    @Autowired
    private OmsOrderReturnReasonService orderReturnReasonService;

    @ApiOperation("申请退货")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody OmsOrderReturnApplyParam returnApply) {
        int count = returnApplyService.create(returnApply);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("分页查询退货原因")
    @RequestMapping(value = "/reason/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnReason>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<OmsOrderReturnReason> reasonList = orderReturnReasonService.list(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(reasonList));
    }

    @ApiOperation("分页查询退货申请")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnApplyVo>> list(@RequestBody OmsReturnApplyQueryParam queryParam) {
        List<OmsOrderReturnApplyVo> returnApplyList = returnApplyService.list(queryParam, queryParam.getPageSize(), queryParam.getPageNum());
        return CommonResult.success(CommonPage.restPage(returnApplyList));
    }

    @ApiOperation("撤销申请")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        int count = returnApplyService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取退货申请详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getItem(@PathVariable Long id) {
        OmsOrderReturnApplyResult result = returnApplyService.getItem(id);
        return CommonResult.success(result);
    }


    @ApiOperation("设置退货单号")
    @PostMapping(value = "/deliverySn")
    @ResponseBody
    public CommonResult getItem(@RequestBody OmsReturnApplyDto applyDto) {
        returnApplyService.setDeliverySn(applyDto);
        return CommonResult.success(true);
    }
}
