package com.tmerclub.cloud.cuerecycle.controller.admin;

import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.cuerecycle.manager.CreateOrderManager;
import com.tmerclub.cloud.cuerecycle.mapper.CueOrderFlawMapper;
import com.tmerclub.cloud.cuerecycle.model.dto.CueOrderDTO;
import com.tmerclub.cloud.cuerecycle.model.dto.CueOrderFlawDTO;
import com.tmerclub.cloud.cuerecycle.model.dto.CueRetractedDTO;
import com.tmerclub.cloud.cuerecycle.model.vo.AdminCueOrderVO;
import com.tmerclub.cloud.cuerecycle.model.vo.CueOrderFlawVO;
import com.tmerclub.cloud.cuerecycle.model.vo.CueOrderVO;
import com.tmerclub.cloud.cuerecycle.service.CueOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回收订单
 *
 * @author : frank
 * @create : 2025-04-15
 */
@Tag(name = "后管-回收订单")
@RestController("adminCueOrderController")
@RequestMapping("/admin/cueOrder")
public class CueOrderController {
    @Resource
    CueOrderService cueOrderService;
    @Resource
    CueOrderFlawMapper cueOrderFlawMapper;
    @Resource
    CreateOrderManager createOrderManager;

    /**
     * 分页查询
     *
     * @param pageDTO     分页参数
     * @param cueOrderDTO 查询参数
     * @return 分页数据
     */
    @GetMapping("/page")
    @Operation(summary = "查询回收订单分页列表", description = "查询回收订单分页列表")
    public ServerResponseEntity<PageVO<AdminCueOrderVO>> page(@Valid PageDTO pageDTO, CueOrderDTO cueOrderDTO) {
        PageVO<AdminCueOrderVO> cueOrderPage = cueOrderService.page(pageDTO, cueOrderDTO);
        return ServerResponseEntity.success(cueOrderPage);
    }

    /**
     * 根据主键id获取回收订单
     *
     * @param orderId 主键id
     * @return CueOrderVO
     */
    @GetMapping
    @Operation(summary = "根据id获取回收订单", description = "根据id获取回收订单")
    public ServerResponseEntity<CueOrderVO> getById(@RequestParam Long orderId) {
        CueOrderVO cueOrder = cueOrderService.getById(orderId);
        // 查询瑕疵项目
        CueOrderFlawDTO cueOrderFlawDTO = new CueOrderFlawDTO();
        cueOrderFlawDTO.setOrderId(orderId);
        List<CueOrderFlawVO> list = cueOrderFlawMapper.list(cueOrderFlawDTO);
        cueOrder.setFlawList(list);
        return ServerResponseEntity.success(cueOrder);
    }

    /**
     * 修改审核状态(审核通过,审核拒绝)
     *
     * @param cueOrderDTO 回收订单
     * @return Void
     */
    @PutMapping("/updateAuditStatus")
    @Operation(summary = "修改审核状态(审核通过,审核拒绝)", description = "修改审核状态(审核通过,审核拒绝)")
    public ServerResponseEntity<Void> updateAuditStatus(@Valid @RequestBody CueOrderDTO cueOrderDTO) {
        int update = cueOrderService.updateAuditStatus(cueOrderDTO);
        return update > 0 ? ServerResponseEntity.success() : ServerResponseEntity.showFailMsg("修改失败");
    }

    /**
     * 修改评估状态(评估通过，评估拒绝)
     *
     * @param cueOrderDTO 回收订单
     * @return Void
     */
    @PutMapping("/updateEvaluateStatus")
    @Operation(summary = "修改评估状态(评估通过，评估拒绝)", description = "修改评估状态(评估通过，评估拒绝)")
    public ServerResponseEntity<Void> updateEvaluateStatus(@Valid @RequestBody CueOrderDTO cueOrderDTO) {
        int update = cueOrderService.updateEvaluateStatus(cueOrderDTO);
        return update > 0 ? ServerResponseEntity.success() : ServerResponseEntity.showFailMsg("修改失败");
    }


    /**
     * 球杆退回
     *
     * @param cueRetractedDTO 退回参数
     * @return 结果
     */
    @PutMapping("/cueRetracted")
    @Operation(summary = "球杆退回", description = "球杆退回")
    public ServerResponseEntity<Void> cueRetracted(@Valid @RequestParam CueRetractedDTO cueRetractedDTO) {
        createOrderManager.cueRetracted(cueRetractedDTO);
        return ServerResponseEntity.success();
    }
}