package com.jwipc.sys.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.SupplierOrderFileSettingDTO;
import com.jwipc.sys.logic.SupplierOrderFileSettingLogic;
import com.jwipc.sys.model.query.SupplierOrderFileSettingQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 出货检验报告必填设置-接口
 * </p>
 * @author Chen Tianshi
 * @since 2025-08-06 09:34:18
 */
@RestController
@RequestMapping(MODULE_ROOT + "/supplierOrderFileSetting/rest")
@RequiredArgsConstructor
public class SupplierOrderFileSettingRestController {
    private final SupplierOrderFileSettingLogic SupplierOrderFileSettingLogic;

    /**
     * 搜索
     *
     * @param query 查询参数
     * @return 查询结果
     */
    @RequestMapping("/list")
    public PageView list(SupplierOrderFileSettingQuery query) {
        return SupplierOrderFileSettingLogic.findByPage(query);
    }

    /**
     * 通过供应商编码搜索数据
     *
     * @param supplierCode 供应商编码
     * @return 查询结果
     */
    @RequestMapping("/getBySupplierCode")
    public SupplierOrderFileSettingDTO getBySupplierId(String supplierCode) {
        return SupplierOrderFileSettingLogic.getBySupplierCode(supplierCode);
    }

    /**
     * 插入或者更新
     *
     * @param param 参数
     */
    @PostMapping("/saveOrUpdate")
    public ResponseEntity<Boolean> saveOrUpdate(SupplierOrderFileSettingDTO param) {
        SupplierOrderFileSettingLogic.saveOrUpdate(param);
        return ResponseEntity.ok(true);
    }

    /**
     * 列表导出
     *
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<SupplierOrderFileSettingDTO> exportData = SupplierOrderFileSettingLogic.listAll(JSON.parseObject(queryStr, SupplierOrderFileSettingQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"出货检验报告必填设置导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    /**
     * 列表导入
     * @param file 导出的列信息
     */
    @PostMapping("import")
    public ResponseEntity<Boolean> importData(@RequestParam(value = "file") MultipartFile file) {
        SupplierOrderFileSettingLogic.itemImportData(file);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     *
     * @param id 参数
     */
    @PostMapping("/remove")
    public ResponseEntity<Boolean> remove(Long id) {
        SupplierOrderFileSettingLogic.remove(id);
        return ResponseEntity.ok(true);
    }
}
