 package com.jwipc.tender.controller.pc;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.log.annotation.AuditLogInter;
import com.jwipc.page.PageView;
import com.jwipc.tender.dto.QuotaManagementDTO;
import com.jwipc.tender.dto.QuotaManagementSaveDTO;
import com.jwipc.tender.logic.QuotaManagementLogic;
import com.jwipc.tender.model.query.QuotaEditQuery;
import com.jwipc.tender.model.query.QuotaManagementQuery;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
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.tender.constant.ModuleConst.MODULE_ROOT;

/**
 * @author Al
 * @create 2024-01-31 10:53
 * @desc 模具
 */

@RestController
@RequestMapping(MODULE_ROOT + "/quotaManage/quotaManagement/rest")
@RequiredArgsConstructor
public class QuotaManagementRestController {
    private final QuotaManagementLogic quotaManagementLogic;
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @AuditLogInter(value = "首页查询列表", module = "配额维护")
    @RequestMapping("/list")
    public PageView list(QuotaManagementQuery query) {
        return quotaManagementLogic.findByPage(query);
    }
    /**
     * 列表
     *
     * @param query
     * @return
     */
    @AuditLogInter(value = "首页查询列表", module = "配额清单")
    @RequestMapping("/bid/list")
    public PageView bidList(QuotaManagementQuery query) {
        return quotaManagementLogic.bidFindByPage(query);
    }
    /**
     * 编辑物料查询
     *
     * @param query
     * @return
     */
    @RequestMapping("/edit/list")
    public PageView editFindList(QuotaEditQuery query) {
        return quotaManagementLogic.editFindByPage(query);
    }
    /**
     * 编辑供应商查询
     *
     * @param query
     * @return
     */
    @RequestMapping("/supplier/list")
    public PageView supplierFindList(QuotaEditQuery query) {
        return quotaManagementLogic.supplierFindByPage(query);
    }
    /**
     * 配额保存更新
     * @param param 参数
     */
    @ResponseBody
    @PostMapping("/saveOrUpdate")
    public ResponseEntity saveOrUpdate(@RequestBody List<QuotaManagementSaveDTO> param){
        quotaManagementLogic.saveOrUpdate(param);
            //sysReturnMsg.setData(quotoManagementLogic.saveOrUpdate(quotaListJson, user));
        return ResponseEntity.ok(true);

    }
    /**
     * 配额维护导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @AuditLogInter(value = "导出数据", module = "配额清单")
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr) throws IOException {
        // 查询数据列表，直接调用列表的查询，无需特殊改动
        final List<QuotaManagementDTO> exportData = quotaManagementLogic.listAllExport(JSON.parseObject(queryStr, QuotaManagementQuery.class));
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"配额维护导出-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    /**
     * 导入
     * @param file 导入文件
     * @return
     */
    @PostMapping("import")
    public ResponseEntity<String> importData(@RequestParam(value = "file") MultipartFile file) {
        String message = quotaManagementLogic.importData(file);
        return ResponseEntity.ok(message);
    }
    /**
     * 配额主数据
     * @return 信息
     */
    @RequestMapping("/listAll")
    public ResponseEntity<List<QuotaManagementDTO>> listAll(QuotaManagementQuery query){
        return ResponseEntity.ok(quotaManagementLogic.listAll(query));
    }
    /**
     * 配额主数据
     * @return 信息
     */
    @RequestMapping("/listAllExport")
    public ResponseEntity<List<QuotaManagementDTO>> listAllExport(QuotaManagementQuery query){
        return ResponseEntity.ok(quotaManagementLogic.listAllExport(query));
    }

    /**
     * 提交oa审批
     *
     * @param id id
     */
    @PostMapping("/pushOa")
    public ResponseEntity pushOa(String quotaCode) {
        quotaManagementLogic.pushOa(quotaCode);
        return ResponseEntity.ok(true);
    }
}
