package com.tashow.cloud.trade.controller.admin.order;

import cn.hutool.core.collection.CollUtil;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.productapi.api.product.ProdApi;
import com.tashow.cloud.productapi.api.product.dto.ProdDO;
import com.tashow.cloud.trade.controller.admin.order.vo.*;
import com.tashow.cloud.trade.convert.order.TradeOrderConvert;
import com.tashow.cloud.trade.dal.dataobject.order.TradeOrderDO;
import com.tashow.cloud.trade.dal.dataobject.order.TradeOrderItemDO;
import com.tashow.cloud.trade.dal.dataobject.order.TradeOrderLogDO;
import com.tashow.cloud.trade.dal.dataobject.order.TradeOrderSubLogDO;
import com.tashow.cloud.trade.service.order.TradeOrderLogService;
import com.tashow.cloud.trade.service.order.TradeOrderQueryService;
import com.tashow.cloud.trade.service.order.TradeOrderUpdateService;
import com.tashow.cloud.userapi.api.user.UserMemberApi;
import com.tashow.cloud.userapi.api.user.dto.UserMemberRespDTO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.common.util.collection.CollectionUtils.convertList;
import static com.tashow.cloud.common.util.collection.CollectionUtils.convertSet;


/**
 * 管理后台 - 交易订单
 */
@RestController
@RequestMapping("/trade/order")
@Validated
@Slf4j
public class TradeOrderController {

    @Resource
    private TradeOrderUpdateService tradeOrderUpdateService;
    @Resource
    private TradeOrderQueryService tradeOrderQueryService;
    @Resource
    private TradeOrderLogService tradeOrderLogService;

    @Resource
    private UserMemberApi userMemberApi;
    @Resource
    private ProdApi prodApi;

    /**
     * 获得交易订单分页
     *
     * @param reqVO
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<PageResult<TradeOrderPageRespVO>> getOrderPage(@Valid TradeOrderPageReqVO reqVO) {
        // 查询订单
        PageResult<TradeOrderDO> pageResult = tradeOrderQueryService.getOrderPage(reqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        // 查询订单项
        List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(
                convertSet(pageResult.getList(), TradeOrderDO::getId));
        //查询用户信息
        Map<Long, UserMemberRespDTO> userMap = userMemberApi.getUserMap(convertList(pageResult.getList(), TradeOrderDO::getUserId));
        // 最终组合
        return success(TradeOrderConvert.INSTANCE.convertPage(pageResult, orderItems,userMap));
    }

    /**
     * 获得交易订单详情
     *
     * @param id 订单编号
     * @return
     */
    @GetMapping("/get-detail")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderDetailRespVO> getOrderDetail(@RequestParam("id") Long id) {
        // 查询订单
        TradeOrderDO order = tradeOrderQueryService.getOrder(id);
        if (order == null) {
            return success(null);
        }
        // 查询订单项
        List<TradeOrderItemDO> orderItems = tradeOrderQueryService.getOrderItemListByOrderId(id);
        //查询订单日志
        List<TradeOrderLogDO> orderLogs = tradeOrderLogService.getOrderLogListByOrderId(id);
        //查询用户信息
        Map<Long, UserMemberRespDTO> userMap = userMemberApi.getUserMap(List.of(order.getId()));
        return success(TradeOrderConvert.INSTANCE.convert(order, orderItems,userMap, orderLogs));
    }

    /**
     * 获得交易订单统计
     *
     * @return
     */
    @GetMapping("/summary")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderSummaryRespVO> getOrderSummary() {
        return success(tradeOrderQueryService.getOrderSummary());
    }

    /**
     * 取消订单
     *
     * @return
     */
    @PutMapping("/cancel")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> cancel(@RequestBody TradeOrderCancelReqVo reqVO) {
        return success(tradeOrderUpdateService.cancelOrderByAdmin(reqVO));
    }

    /**
     * 接单确认
     *,
     */

    @PutMapping("/acceptConfirm")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> acceptConfirm(@RequestParam("id") Long id) {
        tradeOrderUpdateService.acceptConfirmOrderByAdmin(id);
        return success(true);
    }

    /**
     * 服务上报
     *
     * @return
     */
    @PutMapping("/reportServe")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> reportServe(@RequestParam("id") Long id) {
        tradeOrderUpdateService.reportServeOrderByAdmin(id);
        return success(true);
    }

    // 订单备注
    @PutMapping("/update-remark")
    @PreAuthorize("@ss.hasPermission('trade:order:update')")
    public CommonResult<Boolean> updateOrderRemark(@Valid @RequestBody TradeOrderRemarkReqVO reqVO) {
        tradeOrderUpdateService.updateOrderRemark(reqVO);
        return success(true);
    }

    /**
     * 交易快照
     * @return
     */
    @GetMapping("/fastPhoto")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<TradeOrderFastPhotoRespVo> fastPhoto(@Valid TradeOrderFastPhotoReqVo reqVo) {
        //获取交易订单详情
        TradeOrderItemDO orderItem = tradeOrderQueryService.getOrderItem(reqVo.getItemId());
        //获取产品详情
        CommonResult<ProdDO> prodInfo = prodApi.getProdInfo(reqVo.getSpuId());
        return success(TradeOrderConvert.INSTANCE.convert(orderItem,prodInfo.getData()));
    }

    /**
     * 服务信息修改记录
     *
     * @return
     */
    @GetMapping("/subTimeLog/{id}")
    @PreAuthorize("@ss.hasPermission('trade:order:query')")
    public CommonResult<List<TradeOrderSubLogDO>> subTimeLog(@PathVariable("id") Long id) {
        //获取订单预约时间修改列表
        List<TradeOrderSubLogDO> subLogDO = tradeOrderQueryService.getSubTimeLogList(id);
        return success(subLogDO);
    }

}
