package com.jwipc.inquiry.controller.rest;

import com.jwipc.inquiry.constant.ModuleConst;
import com.jwipc.inquiry.dto.*;
import com.jwipc.inquiry.logic.InquiryLogic;
import com.jwipc.inquiry.model.query.InquiryConvertContractQuery;
import com.jwipc.inquiry.model.query.InquiryQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author shenyang.ou
 * @date 2024/2/18 15:35
 * @desc 询价
 */
@RestController
@RequestMapping(ModuleConst.MODULE_ROOT + "/rest")
@RequiredArgsConstructor
public class InquiryRestController {
    private final InquiryLogic inquiryLogic;
    /**
     * 列表
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(InquiryQuery query) {
        query.setUserId(LoginUtil.getUserId());
        return inquiryLogic.findByPage(query);
    }

    /**
     * 询价编辑供应商列表
     * @param inquiryId 询价id
     * @return
     */
    @RequestMapping("/supplierListByInquiryId")
    public ResponseEntity<List<InquiryEditSupplierDTO>> supplierListByInquiryId(Long inquiryId) {
        return ResponseEntity.ok(inquiryLogic.supplierListByInquiryId(inquiryId));
    }

    /**
     * 询价复制编辑供应商列表
     * @param inquiryId 询价id
     * @return
     */
    @RequestMapping("/supplierCopyListByInquiryId")
    public ResponseEntity<List<InquiryEditSupplierDTO>> supplierCopyListByInquiryId(Long inquiryId) {
        return ResponseEntity.ok(inquiryLogic.supplierCopyListByInquiryId(inquiryId));
    }

    /**
     * 导入物料信息
     * @param file 导入文件
     * @return
     */
    @PostMapping("importMaterialData")
    public ResponseEntity<List<InquiryMaterialImportDTO>> importMaterialData(@RequestParam(value = "file") MultipartFile file) {
        List<InquiryMaterialImportDTO> inquiryMaterialImportDTOS = inquiryLogic.importMaterialData(file);
        return ResponseEntity.ok(inquiryMaterialImportDTOS);
    }

    /**
     * 导入供应商信息
     * @param file 导入文件
     * @return
     */
    @PostMapping("importSupplierData")
    public ResponseEntity<List<InquirySupplierImportDTO>> importSupplierData(@RequestParam(value = "file") MultipartFile file,String companyCode) {
        List<InquirySupplierImportDTO> inquirySupplierImportDTOS = inquiryLogic.importSupplierData(file,companyCode);
        return ResponseEntity.ok(inquirySupplierImportDTOS);
    }

    /**
     * 根据物料编码和工厂编码获取配额供应商
     * @param materialCode 物料编码
     * @param factoryCode 工厂编码
     */
    @PostMapping("getSupplierByMaterialCodeAndFactoryCode")
    public ResponseEntity<List<InquirySupplierImportDTO>> getSupplierByMaterialCodeAndFactoryCode(String materialCode,String factoryCode,String companyCode) {
        List<InquirySupplierImportDTO> inquirySupplierImportDTOS = inquiryLogic.getSupplierByMaterialCodeAndFactoryCode(materialCode,factoryCode,companyCode);
        return ResponseEntity.ok(inquirySupplierImportDTOS);
    }

    /**
     * 保存更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody InquiryParamDTO param) {
        inquiryLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 发布
     *
     * @param id 询价id
     */
    @PostMapping("/publish")
    public ResponseEntity publish(Long id) {
        inquiryLogic.publish(id);
        return ResponseEntity.ok(true);
    }


    /**
     * 截止
     *
     * @param id 询价id
     */
    @PostMapping("/end")
    public ResponseEntity end(Long id) {
        inquiryLogic.end(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 关闭
     * 对于截止的报价/议价 可以直接进行不定价关闭 status
     * @param id 询价id
     */
    @PostMapping("/close")
    public ResponseEntity close(Long id) {
        inquiryLogic.close(id);
        return ResponseEntity.ok(true);
    }

    /**
     * 作废
     *
     * @param id 询价id
     */
    @PostMapping("/cancel")
    public ResponseEntity cancel(Long id,String cancelRemark) {
        inquiryLogic.cancel(id,cancelRemark);
        return ResponseEntity.ok(true);
    }


    /**
     * 删除
     *
     * @param id 询价id
     */
    @PostMapping("/remove")
    public ResponseEntity remove(Long id) {
        inquiryLogic.remove(id);
        return ResponseEntity.ok(true);
    }


    /**
     * 获取物料报价情况统计
     * @param inquiryId 询价id
     * @return 物料报价情况统计
     */
    @RequestMapping("/material/quote/list")
    public ResponseEntity<List<InquiryMaterialQuoteDTO>> materialQuoteList(Long inquiryId) {
        return ResponseEntity.ok(inquiryLogic.materialQuoteList(inquiryId));
    }


    /**
     * 获取询价单
     * @param xjId 询价单ID
     * @param type 查询类型(可选：2-合同列表)
     * @return
     */
    @PostMapping("/xjlist")
    public ResponseEntity xjlistFindByList(Long xjId, Integer type) {
        return ResponseEntity.ok(inquiryLogic.xjlistFindByList(xjId, type));
    }

    @PostMapping("/inquiryContractList")
    public ResponseEntity inquiryContractList(InquiryConvertContractQuery query) {
        if (StringUtils.isEmpty(query.getSupplierCode()) && StringUtils.isEmpty(query.getSupplierName())){
            return ResponseEntity.ok(new ArrayList<>());
        }
        return ResponseEntity.ok(inquiryLogic.inquiryContractList(query));
    }

    /**
     * 获取生成费用合同询价单数据
     * @param query 查询参数
     * @return 询价单数据
     */
    @PostMapping("/getInquiryCostContractList")
    public ResponseEntity<List<InquiryCostContractDTO>> getInquiryCostContractList(InquiryConvertContractQuery query) {
        if (Objects.isNull(query.getInquiryId()) && StringUtils.isEmpty(query.getSupplierCode())){
            return ResponseEntity.ok(new ArrayList<>());
        }
        return ResponseEntity.ok(inquiryLogic.getInquiryCostContractList(query));
    }


}
