package com.jwipc.contract.controller.pc;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractDeliverySaveDTO;
import com.jwipc.contract.dto.ContractReturnOrderSaveDTO;
import com.jwipc.contract.logic.ContractReturnOrderDetailLogic;
import com.jwipc.contract.logic.ContractReturnOrdersLogic;
import com.jwipc.contract.model.query.ContractDeliveryQuery;
import com.jwipc.contract.model.query.ContractReturnOrderQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Set;


/**
 * <p>
 * 合同退货单表 前端控制器
 * </p>
 *
 * @author qzc
 * @since 2025-12-09
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/returnOrder/rest/")
@RequiredArgsConstructor
@Slf4j
public class ContractReturnOrderRestController {

    private final ContractReturnOrderDetailLogic contractReturnOrderDetailLogic;
    private final ContractReturnOrdersLogic contractReturnOrdersLogic;

    /**
     * 查询退货货单明细
     * @param id
     * @return
     */
    @GetMapping("listReturnOrderDetail")
    public PageView listReturnOrderDetail(Long id) {
        return contractReturnOrderDetailLogic.getListReturnDetail(id);
    }

    /**
     * 退货单保存
     */
    @PostMapping("save")
    public ResponseEntity save(@RequestBody ContractReturnOrderSaveDTO saveDTO) {
        final Pair<String, Set<String>> msg = contractReturnOrdersLogic.saveOrUpdate(saveDTO);
        JSONObject result = new JSONObject();
        result.put("id", msg.getKey());
        if (CollUtil.isNotEmpty(msg.getValue())) {
            result.put("msg", msg.getValue());
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.ok(result);
    }
    /**
     * 退货单列表查询
     */
    @GetMapping("list")
    public PageView list(ContractReturnOrderQuery query) {
        return contractReturnOrdersLogic.getContractReturnOrderList(query);
    }

    /**
     * 合同发货单修改
     * @param
     * @return
     */
    @PostMapping("update")
    public ResponseEntity update(@RequestBody ContractReturnOrderSaveDTO dto) {
        contractReturnOrdersLogic.update(dto);
        return ResponseEntity.ok().build();
    }

    /**
     * 确认退货
     * @param id
     * @return
     */
    @PostMapping("confirmReceipt")
    public ResponseEntity confirmReceipt(Long id){
        contractReturnOrdersLogic.confirmretuenOrder(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 退货单删除
     * @param id
     * @return
     */

    @PostMapping("retuenOrderRemove")
    public ResponseEntity retuenOrderRemove(Long id) {
        contractReturnOrdersLogic.retuenOrderRemove(id);
        return ResponseEntity.ok().build();
    }
}
