package org.springblade.modules.api.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.AfterSalesStatusEnum;
import org.springblade.common.enums.AfterSalesTypeEnum;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.request.AfterSalesRequest;
import org.springblade.modules.finance.entity.AfterSales;
import org.springblade.modules.finance.entity.AfterSalesReason;
import org.springblade.modules.finance.service.IAfterSalesReasonService;
import org.springblade.modules.finance.service.IAfterSalesService;
import org.springblade.modules.finance.vo.AfterSalesVO;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * Author pangqijun
 * Date 2023/6/18
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL + "/after-sales")
@Api(value = "售后接口", tags = "11.售后接口")
public class ApiAfterSalesController {

    @Lazy
    @Resource
    private IAfterSalesService afterSalesService;
    @Lazy
    @Resource
    private IAfterSalesReasonService afterSalesReasonService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "提交售后")
    @PostMapping("submit")
    public R submit(@Valid @RequestBody AfterSalesRequest request) {

        request.setAfterSalesType(AfterSalesTypeEnum.RETURN_GOODS.getValue());
        afterSalesService.submit(request);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "提交部分退款售后")
    @PostMapping("refundSubmit")
    public R refundSubmit(@Valid @RequestBody AfterSalesRequest request) {
        request.setAfterSalesType(AfterSalesTypeEnum.PART_REFUND.getValue());
        afterSalesService.refundSubmit(request);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "未到货退款")
    @PostMapping("applyGrouponAfterRefund")
    public R applyGrouponAfterRefund(@RequestParam Long orderId) {
        afterSalesService.applyGrouponAfterRefund(orderId);
        return R.success("申请成功");
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "重新提交售后")
    @PostMapping("reSubmit")
    public R reSubmit(@RequestParam Long id) {
        AfterSales afterSales = afterSalesService.getById(id);
        Assert.notNull(afterSales, "数据不存在");
        afterSales.setStatus(AfterSalesStatusEnum.WAIT.getValue());
        afterSalesService.updateById(afterSales);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "同意售后")
    @PostMapping("agree")
    public R agree(@RequestParam("afterSalesNo") String afterSalesNo) {
        afterSalesService.agree(afterSalesNo);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "驳回售后")
    @PostMapping("refuse")
    public R refuse(@RequestParam("afterSalesNo") String afterSalesNo, @RequestParam("refuseReason") String refuseReason) {
        afterSalesService.refuse(afterSalesNo, refuseReason);
        return R.success("ok");
    }

    @GetMapping("/reason/list")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取售后原因列表")
    public R<List<AfterSalesReason>> getAfterSalesReason() {
        List<AfterSalesReason> list = afterSalesReasonService.lambdaQuery().list();
        return R.data(list);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "绑定物流单号")
    @PostMapping("bindLogisticsNo")
    public R bindLogisticsNo(@RequestParam String afterSalesNo, @RequestParam String logisticsNo) {
        Assert.notBlank(logisticsNo, "请输入物流编号");
        afterSalesService.bindLogisticsNo(afterSalesNo, logisticsNo);
        return R.success("ok");
    }

    @PostMapping("/user/list")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "获取用户售后列表")
    public R<IPage<AfterSalesVO>> getUserAfterSalesList(@RequestBody Query query, @RequestParam(name = "state") Integer state) {
        return R.data(afterSalesService.getUserAfterSalesList(Condition.getPage(query) , AuthUtil.getUserId(), state));
    }

    @GetMapping("/user/detail")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "获取用户售后详情")
    public R<AfterSalesVO> getUserAfterSalesDetail(@RequestParam(name = "afterSalesId") Long afterSalesId) {
        return R.data(afterSalesService.getUserAfterSalesDetail(afterSalesId));
    }
}
