/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.finance.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springblade.common.base.BaseEntity;
import org.springblade.common.enums.*;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderGrouponGoods;
import org.springblade.modules.finance.entity.OrderRefund;
import org.springblade.modules.finance.service.*;
import org.springblade.modules.finance.vo.OrderGrouponGoodsVO;
import org.springblade.modules.finance.wrapper.OrderGrouponGoodsWrapper;
import org.springblade.modules.mall.service.IStoreService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.finance.entity.AfterSales;
import org.springblade.modules.finance.vo.AfterSalesVO;
import org.springblade.modules.finance.wrapper.AfterSalesWrapper;
import org.springblade.core.boot.ctrl.BladeController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 售后表 控制器
 *
 * @author BladeX
 * @since 2023-06-15
 */
@RestController
@RequestMapping("finance/aftersales")
@Api(value = "售后表", tags = "售后表接口")
public class AfterSalesController extends BladeController {

	@Resource
	private IAfterSalesService afterSalesService;
	@Resource
	private IOrderService orderService;
	@Resource
	private IStoreService storeService;
	@Resource
	private IOrderGrouponGoodsService orderGrouponGoodsService;
	@Resource
	private IOrderRefundService orderRefundService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入afterSales")
	public R<AfterSalesVO> detail(AfterSales afterSales) {
		AfterSalesVO vo = afterSalesService.getUserAfterSalesDetail(afterSales.getId());
		vo.setOrderNo(vo.getOrder().getOrderNo());
		if (AfterSalesTypeEnum.RETURN_GOODS.getValue().equals(vo.getType())) {
			for (OrderGrouponGoods goods : vo.getGoodsList()) {
				goods.setGoodsNum(vo.getReturnGoodsNum());
			}
		}

		return R.data(vo);
	}

	/**
	 * 分页 售后表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入afterSales")
	public R<IPage<AfterSalesVO>> list(AfterSales afterSales, Query query) {
		IPage<AfterSales> pages = afterSalesService.page(Condition.getPage(query), Condition.getQueryWrapper(afterSales));
		return R.data(AfterSalesWrapper.build().pageVO(pages));
	}

	/**
	 * 供应商分页 售后表
	 */
	@GetMapping("/storeList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入afterSales")
	public R<IPage<AfterSalesVO>> storeList(AfterSales afterSales, Query query) {
		afterSales.setStoreId(storeService.getByUserId().getId());
//        afterSales.setStatus(AfterSalesStatusEnum.PROCESSED.getValue());
		IPage<AfterSales> pages = afterSalesService.page(Condition.getPage(query), Condition.getQueryWrapper(afterSales));
		return R.data(AfterSalesWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 售后表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入afterSales")
	public R<IPage<AfterSalesVO>> page(AfterSalesVO afterSales, Query query) {
		IPage<AfterSalesVO> pages = afterSalesService.selectAfterSalesPage(Condition.getPage(query), afterSales);
		return R.data(pages);
	}

	/**
	 * 新增 售后表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入afterSales")
	public R save(@Valid @RequestBody AfterSales afterSales) {
		return R.status(afterSalesService.save(afterSales));
	}

	/**
	 * 修改 售后表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入afterSales")
	public R update(@Valid @RequestBody AfterSales afterSales) {
		return R.status(afterSalesService.updateById(afterSales));
	}

	/**
	 * 新增或修改 售后表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入afterSales")
	public R submit(@Valid @RequestBody AfterSales afterSales) {
		return R.status(afterSalesService.saveOrUpdate(afterSales));
	}

	
	/**
	 * 删除 售后表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(afterSalesService.removeByIds(Func.toLongList(ids)));
	}

	@PostMapping("/confirm")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "确认收货")
	public R confirm(@RequestParam Long orderId, @RequestParam Long afterSalesId) {
		afterSalesService.confirm(orderId, afterSalesId);
		return R.success("操作成功");
	}

	@PostMapping("/refund")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "退款")
	public R refund(@RequestParam Long orderId, @RequestParam Long afterSalesId) {
		AfterSales afterSales = afterSalesService.getById(afterSalesId);
		Assert.notNull(afterSales, "售后数据不存在");
		if (!AfterSalesTypeEnum.REFUND.getValue().equals(afterSales.getType())) {
			return R.fail("当前订单不支持后台退款");
		}

		if (YesOrNoEnum.YES.getValue().equals(afterSales.getRefundState())) {
			throw new ServiceException("该售后订单已退款");
		}

		Order order = orderService.getById(orderId);
		Assert.notNull(order, "订单数据不存在");
		orderService.commonRefund(order, afterSalesId);
		return R.success("操作成功");
	}

	@PostMapping("/refundApply")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "退款审核")
	public R refundApply(@RequestParam Long afterSalesId, @RequestParam Integer auditStatus, @RequestParam String remark) {
		AfterSales afterSales = afterSalesService.getById(afterSalesId);
		Assert.notNull(afterSales, "售后数据不存在");

		if (YesOrNoEnum.YES.getValue().equals(afterSales.getRefundState())) {
			throw new ServiceException("该售后订单已退款");
		}
		// 通过
		if (auditStatus == 1) {
			Order order = orderService.getById(afterSales.getOrderId());
			if (AfterSalesTypeEnum.REFUND.getValue().equals(afterSales.getType())) {
				afterSales.setRefundState(2);// 退款中
				Assert.notNull(order, "订单数据不存在");
				orderService.commonRefund(order, afterSalesId);
			} else if (AfterSalesTypeEnum.RETURN_GOODS.getValue().equals(afterSales.getType())) {
				afterSalesService.confirm(order.getId(), afterSalesId);
			} else if (AfterSalesTypeEnum.PART_REFUND.getValue().equals(afterSales.getType())) {
				afterSalesService.agree(afterSales.getAfterSalesNo());
			} else {
				return R.fail("参数异常");
			}
		} else if (auditStatus == 2) {// 驳回
			afterSales.setStatus(AfterSalesStatusEnum.REFUSE.getValue());
			afterSales.setRefuseReason(remark);
			afterSalesService.updateById(afterSales);
		} else {
			return R.fail("参数错误");
		}
		return R.success("操作成功");
	}

	@Resource
	private IWxPayService wxPayService;
	@GetMapping("test")
	public R test(@RequestParam("openId") String openId) {
		wxPayService.profitSharingAddReceiver(openId);
		return R.success("ok");
	}
}
