package com.gxwebsoft.shop.controller;

import com.gxwebsoft.apps.entity.Equipment;
import com.gxwebsoft.apps.service.EquipmentService;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.service.OrderRefundService;
import com.gxwebsoft.shop.entity.OrderRefund;
import com.gxwebsoft.shop.param.OrderRefundParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.shop.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

import static com.gxwebsoft.common.core.constants.OrderConstants.RECEIPT_STATUS_RETURN;

/**
 * 售后单记录表控制器
 *
 * @author 科技小王子
 * @since 2023-05-18 13:09:51
 */
@Api(tags = "售后单记录表管理")
@RestController
@RequestMapping("/api/shop/order-refund")
public class OrderRefundController extends BaseController {
    @Resource
    private OrderRefundService orderRefundService;
    @Resource
    private OrderService orderService;
    @Resource
    private EquipmentService equipmentService;

    @PreAuthorize("hasAuthority('shop:orderRefund:list')")
    @OperationLog
    @ApiOperation("分页查询售后单记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<OrderRefund>> page(OrderRefundParam param) {
        // 使用关联查询
        return success(orderRefundService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:list')")
    @OperationLog
    @ApiOperation("查询全部售后单记录表")
    @GetMapping()
    public ApiResult<List<OrderRefund>> list(OrderRefundParam param) {
        // 使用关联查询
        return success(orderRefundService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:list')")
    @OperationLog
    @ApiOperation("根据id查询售后单记录表")
    @GetMapping("/{id}")
    public ApiResult<OrderRefund> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(orderRefundService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:save')")
    @OperationLog
    @ApiOperation("添加售后单记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderRefund orderRefund) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          orderRefund.setUserId(loginUser.getUserId());
        }
        if (orderRefundService.save(orderRefund)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:update')")
    @OperationLog
    @ApiOperation("修改售后单记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderRefund orderRefund) {
        if (orderRefundService.updateById(orderRefund)) {
            if(orderRefund.getTenantId().equals(6)){
              final Integer orderId = orderRefund.getOrderId();
              final Order order = orderService.getById(orderId);
              System.out.println("order = " + order);
              order.setReceiptStatus(RECEIPT_STATUS_RETURN);
              final Equipment equipment = equipmentService.getById(order.getEquipmentId());
              equipment.setUserId(0);
              equipmentService.updateById(equipment);
            }
            return success("操作成功");
        }
        return fail("退租失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:remove')")
    @OperationLog
    @ApiOperation("删除售后单记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderRefundService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:save')")
    @OperationLog
    @ApiOperation("批量添加售后单记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderRefund> list) {
        if (orderRefundService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:update')")
    @OperationLog
    @ApiOperation("批量修改售后单记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OrderRefund> batchParam) {
        if (batchParam.update(orderRefundService, "order_refund_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:orderRefund:remove')")
    @OperationLog
    @ApiOperation("批量删除售后单记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderRefundService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
