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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 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 = "获取售后类型")
    @GetMapping("getAfterType")
    public R submit() {
        List<Map<String, String>> result = new ArrayList<>();
        for (AfterSalesTypeEnum value : AfterSalesTypeEnum.values()) {
            if (value.equals(AfterSalesTypeEnum.REFUND)) {
                continue;
            }
            Map<String, String> mapTemp = new HashMap<>();
            mapTemp.put("label", value.getName());
            mapTemp.put("value", value.getValue() + "");
            result.add(mapTemp);
        }
        return R.data(result);
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "提交售后")
    @PostMapping("submit")
    public R submit(@Valid @RequestBody AfterSalesRequest request) {
        if (AfterSalesTypeEnum.RETURN_GOODS.getValue().equals(request.getAfterSalesType())) {
            afterSalesService.submit(request);
        } else if (AfterSalesTypeEnum.PART_REFUND.getValue().equals(request.getAfterSalesType())) {
            afterSalesService.refundSubmit(request);
        } else if (AfterSalesTypeEnum.NOT_ARRIVE_REFUND.getValue().equals(request.getAfterSalesType())) {
            afterSalesService.applyGrouponAfterRefund(request.getOrderId());
        }
        return R.success("ok");
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "取消售后")
    @PostMapping("cancel")
    public R cancel(@RequestParam String afterSalesNo) {
        afterSalesService.cancel(afterSalesNo);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 5)
    @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 = 6)
    @ApiOperation(value = "同意售后")
    @PostMapping("agree")
    public R agree(@RequestParam("afterSalesNo") String afterSalesNo) {
        afterSalesService.agree(afterSalesNo);
        return R.success("ok");
    }

    @ApiOperationSupport(order = 7)
    @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 = 8)
    @ApiOperation(value = "获取售后原因列表")
    public R<List<AfterSalesReason>> getAfterSalesReason() {
        List<AfterSalesReason> list = afterSalesReasonService.lambdaQuery().list();
        return R.data(list);
    }

    @PostMapping("/user/list")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取用户售后列表")
    public R<IPage<AfterSalesVO>> getUserAfterSalesList(@RequestBody Query query,
                                                        @RequestParam(name = "state，0-处理中 1-完成 2-关闭") Integer state) {
        return R.data(afterSalesService.getUserAfterSalesList(Condition.getPage(query), AuthUtil.getUserId(), state));
    }

    @PostMapping("/merchant/list")
    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "获取商家售后列表")
    public R<IPage<AfterSalesVO>> getMerchantAfterSalesList(@RequestBody Query query,
                                                            @RequestParam(name = "state，0-处理中 1-完成 2-关闭") Integer state) {
        return R.data(afterSalesService.getUserAfterSalesList(Condition.getPage(query), AuthUtil.getUserId(), state));
    }

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

}
