package com.starry.admin.modules.order.controller;

import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderRandomInfoEntity;
import com.starry.admin.modules.order.module.vo.*;
import com.starry.admin.modules.order.service.IPlayOrderInfoService;
import com.starry.admin.modules.order.service.IPlayOrderRandomInfoService;
import com.starry.common.annotation.Log;
import com.starry.common.context.CustomSecurityContextHolder;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;


/**
 * 订单管理
 *
 * @author admin
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/order/order/")
public class PlayOrderInfoController {
    @Resource
    private IPlayOrderInfoService orderInfoService;
    @Resource
    private IPlayOrderRandomInfoService playOrderRandomInfoService;


    /**
     * 分页查询订单列表
     */
    @PostMapping("/listByPage")
    public R listByPage(@Validated @RequestBody PlayOrderInfoQueryVo vo) {
        return R.ok(orderInfoService.selectOrderInfoPage(vo));
    }


    /**
     * 订单退款
     */
    //@PreAuthorize("@customSs.hasPermission('order:order:update')")
    @PostMapping("/orderRefund")
    public R orderRefund(@Validated @RequestBody PlayOrderRefundAddVo vo) {
        PlayOrderInfoEntity orderInfo = orderInfoService.selectOrderInfoById(vo.getOrderId());
        if (orderInfo.getFinalAmount().compareTo(vo.getRefundAmount()) < 0) {
            throw new CustomException("退款金额不能大于支付金额");
        }
        if (orderInfo.getOrderStatus().equals("3")) {
            throw new CustomException("【已完成】的订单无法操作退款");
        }
        if (orderInfo.getOrderStatus().equals("4")) {
            throw new CustomException("【已取消】的订单无法操作退款");
        }
        if (orderInfo.getRefundType().equals("1")) {
            throw new CustomException("每个订单只能退款一次~");
        }
        PlayOrderInfoEntity updateOrderInfo = new PlayOrderInfoEntity();
        updateOrderInfo.setId(orderInfo.getId());
        updateOrderInfo.setRefundType("1");
        // 订单退款，订单状态变为已取消
        updateOrderInfo.setOrderStatus("4");
        updateOrderInfo.setRefundAmount(vo.getRefundAmount());
        updateOrderInfo.setRefundReason(vo.getRefundReason());
        return R.ok(orderInfoService.update(updateOrderInfo));
    }


    /**
     * 跟换店员
     */
    //@PreAuthorize("@customSs.hasPermission('order:order:update')")
    @PostMapping("/orderEditAcceptBy")
    public R orderEditAcceptBy(@Validated @RequestBody PlayOrderEditAcceptByVo vo) {
        orderInfoService.updateStateTo1("2", CustomSecurityContextHolder.getUserId(), vo.getAcceptBy(), vo.getOrderId());
        return R.ok("操作成功");
    }


    /**
     * 后台备注
     */
    //@PreAuthorize("@customSs.hasPermission('order:order:update')")
    @PostMapping("/orderEditBackendRemark")
    public R orderEditBackendRemark(@Validated @RequestBody PlayOrderEditBackendRemarkByVo vo) {
        PlayOrderInfoEntity orderInfo = orderInfoService.selectOrderInfoById(vo.getOrderId());
        PlayOrderInfoEntity updateOrderInfo = new PlayOrderInfoEntity();
        updateOrderInfo.setId(orderInfo.getId());
        updateOrderInfo.setBackendRemark(vo.getBackendRemark());
        return R.ok(orderInfoService.update(updateOrderInfo));
    }


    /**
     * 重新抢单
     */
    //@PreAuthorize("@customSs.hasPermission('order:order:update')")
    @PostMapping("/orderEditRandomSingle")
    public R orderEditRandomSingle(@Validated @RequestBody PlayOrderEditRandomSingleVo vo) {
        PlayOrderInfoEntity orderInfo = orderInfoService.selectOrderInfoById(vo.getOrderId());
        if (!"1".equals(orderInfo.getPlaceType())) {
            throw new CustomException("订单类型错误，无法重新抢单");
        }
        PlayOrderRandomInfoEntity randomInfoEntity = new PlayOrderRandomInfoEntity();
        randomInfoEntity.setId(vo.getOrderId());
        randomInfoEntity.setSex(vo.getSex());
        randomInfoEntity.setLevelId(vo.getLevelId());
        randomInfoEntity.setExcludeHistory(vo.getExcludeHistory());
        randomInfoEntity.setRemark(vo.getRemark());
        playOrderRandomInfoService.update(randomInfoEntity);
        PlayOrderInfoEntity updateOrderInfo = new PlayOrderInfoEntity();
        updateOrderInfo.setId(orderInfo.getId());
        updateOrderInfo.setOrderStatus("0");
        updateOrderInfo.setAcceptBy("");
        updateOrderInfo.setEstimatedRevenueRatio(0);
        updateOrderInfo.setEstimatedRevenue(BigDecimal.ZERO);
        randomInfoEntity.setRemark(vo.getRemark());
        return R.ok(orderInfoService.update(updateOrderInfo));
    }


    /**
     * 根据ID查询订单
     */
    @GetMapping(value = "/{id}")
    public R getInfo(@PathVariable("id") String id) {
        return R.ok(orderInfoService.selectById(id));
    }


    /**
     * 根据ID修改订单信息
     */
    //@PreAuthorize("@customSs.hasPermission('order:order:update')")
    @Log(title = "修改普通订单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable String id, @RequestBody PlayOrderInfoEntity orderInfoEntity) {
        orderInfoEntity.setId(id);
        boolean success = orderInfoService.update(orderInfoEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }


    /**
     * 删除订单
     */
    //@PreAuthorize("@customSs.hasPermission('order:order:delete')")
    @Log(title = "删除订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.ok(orderInfoService.deleteOrderInfoByIds(ids));
    }
}
