package com.ylx.web.controller.massage;

import com.ylx.common.annotation.Log;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.massage.domain.dto.AfterSaleOrderDTO;
import com.ylx.massage.domain.dto.AfterSaleOrderFeeBatchDTO;
import com.ylx.massage.domain.dto.AfterSaleOrderUpdateDTO;
import com.ylx.massage.service.IAfterSaleOrderFeeService;
import com.ylx.massage.service.IAfterSaleOrderService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/after/sale/order")
public class AfterSaleOrderController {

    @Resource
    private IAfterSaleOrderFeeService afterSaleOrderFeeService;

    @Resource
    private IAfterSaleOrderService afterSaleOrderService;


    @ApiOperation("批量添加售后费用")
    @Log(title = "批量添加售后费用", businessType = BusinessType.INSERT)
    @PostMapping("/fee/batch")
    public R addFee(@Validated @RequestBody AfterSaleOrderFeeBatchDTO batchDTO) {
        this.afterSaleOrderFeeService.batchSaveFees(batchDTO);
        return R.ok();
    }

    /**
     * 发起售后
     * @param dto
     * @return R
     */
    @ApiOperation("发起售后")
    @Log(title = "发起售后", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody AfterSaleOrderDTO dto) {
        try {
            afterSaleOrderService.add(dto);
            return R.ok();
        } catch (Exception e) {
            log.error("发起售后失败", e);
            return R.fail("发起售后失败:" + e.getMessage());
        }
    }

    /**
     * 取消退货/取消申请
     * @param orderId
     * @return R
     */
    @ApiOperation("取消退货/取消申请")
    @Log(title = "取消退货/取消申请", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{orderId}")
    public R cancel(@PathVariable Long orderId) {
        try {
            this.afterSaleOrderService.cancel(orderId);
            return R.ok();
        } catch (Exception e) {
            log.error("取消退货/取消申请失败", e);
            throw new RuntimeException(e);
        }
    }

    @ApiOperation("售后信息修改物流单号")
    @Log(title = "售后信息修改物流单号", businessType = BusinessType.UPDATE)
    @PutMapping
    public R updateLogisticsNo(@Validated @RequestBody AfterSaleOrderUpdateDTO dto) {
        this.afterSaleOrderService.edit(dto);
        return R.ok();
    }

    @ApiOperation("公众号-修改售后物流单号")
    @Log(title = "公众号修改售后物流单号", businessType = BusinessType.UPDATE)
    @PutMapping("/wx")
    public R wxUpdateLogisticsNo(@Validated @RequestBody AfterSaleOrderUpdateDTO dto) {
        this.afterSaleOrderService.editByWechat(dto);
        return R.ok();
    }

    @ApiOperation("商家同意退货申请")
    @Log(title = "同意退货", businessType = BusinessType.UPDATE)
    @PutMapping("/agree/return/{orderId}")
    public R agreeReturn(@PathVariable Long orderId) {
        this.afterSaleOrderService.agreeReturn(orderId);
        return R.ok();
    }

    @ApiOperation("商家同意换货申请")
    @Log(title = "同意换货", businessType = BusinessType.UPDATE)
    @PutMapping("/agree/exchange/{orderId}")
    public R agreeExchange(@PathVariable Long orderId) {
        this.afterSaleOrderService.agreeExchange(orderId);
        return R.ok();
    }

    /**
     * 商家同意退款申请
     * @param orderId
     * @return R
     */
    @ApiOperation("商家同意退款申请")
    @Log(title = "同意退款", businessType = BusinessType.UPDATE)
    @PutMapping("/agree/refund/{orderId}")
    public R agreeRefund(@PathVariable Long orderId) {
        try {
            this.afterSaleOrderService.agreeRefund(orderId);
            return R.ok();
        } catch (Exception e) {
            log.error("同意退款申请失败", e);
            return R.fail("同意退款申请失败", e.getMessage());
        }
    }

    @ApiOperation("商家确认收货")
    @Log(title = "确认收货", businessType = BusinessType.UPDATE)
    @PutMapping("/confirm/receive/{orderId}")
    public R confirmReceive(@PathVariable Long orderId) {
        this.afterSaleOrderService.confirmReceive(orderId);
        return R.ok();
    }
}
