package com.jwipc.contract.controller.pc;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Pair;
import com.alibaba.fastjson2.JSONObject;
import com.jwipc.contract.constant.ModuleConst;
import com.jwipc.contract.dto.ContractDeliverySaveDTO;
import com.jwipc.contract.logic.ContractDeliveryLogic;
import com.jwipc.contract.model.query.ChoseSupplierListQuery;
import com.jwipc.contract.model.query.ContractDeliveryQuery;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 合同收货单表 前端控制器
 * </p>
 *
 * @author qzc
 * @since 2025-12-09
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/contractDelivery/rest/")
@RequiredArgsConstructor
@Slf4j
public class ContractDeliveryRestController {
    private final ContractDeliveryLogic contractDeliveryLogic;

    @GetMapping("list")
    public PageView list(ContractDeliveryQuery query) {
        return contractDeliveryLogic.getList(query);
    }

    /**
     * 合同发货单保存
     * @param deliveryOrderSaveDTO
     * @return
     */
    @PostMapping("save")
    public ResponseEntity save(@RequestBody ContractDeliverySaveDTO deliveryOrderSaveDTO) {
        final Pair<String, Set<String>> msg = contractDeliveryLogic.saveOrUpdate(deliveryOrderSaveDTO);
        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);
    }

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

    @PostMapping("confirmReceipt")
    public ResponseEntity confirmReceipt(Long id){
        contractDeliveryLogic.confirmReceipt(id);
        return ResponseEntity.ok(true);
    }


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

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

    @PostMapping("contractDeliveryImport")
    public ResponseEntity contractDeliveryImport(MultipartFile file) throws IOException {
        contractDeliveryLogic.contractDeliveryImport(file);
        return ResponseEntity.ok().build();
    }

}
