package com.jwipc.order.controller.pc;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.jwipc.order.entity.BasicForcaseData;
import com.jwipc.order.entity.BasicForcaseQuota;
import com.jwipc.order.entity.BasicPlan;
import com.jwipc.order.logic.BasicPlanLogic;
import com.jwipc.order.model.query.BasicForeignQuotaQuery;
import com.jwipc.order.model.query.BasicPlanQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

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

/**
 * <p>
 * 标准预测计划 前端控制器
 * </p>
 *
 * @Author: swq
 * @Date: 2024/3/12 14:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/basicPlan/rest")
public class BasicPlanRestController {
    private final BasicPlanLogic basicPlanLogic;

    /**
     * 标准预测计划 列表加载
     *
     * @param query
     * @return
     */
    @RequestMapping("/list")
    public PageView list(BasicPlanQuery query) {
        if (LoginUtil.isSupplier()) {
            query.setSupplierId(LoginUtil.getSupplierId());
            query.setSupplierCode(LoginUtil.getSupplierCode());
        }
        PageView pageView = basicPlanLogic.selectListByPage(query);
        return pageView;
    }

    /**
     * 标准预测计划导入
     *
     * @param file      计划文件
     * @param startDate 开始时间
     * @return 导入是否成功
     */
    @PostMapping("/import")
    public ResponseEntity importData(@RequestPart("file") MultipartFile file, String startDate) {

        if (!basicPlanLogic.impAppraiseScore(file, startDate, LoginUtil.getUserId())) {
            return ResponseEntity.badRequest().body("操作失败");
        }
        return ResponseEntity.ok(StringPool.EMPTY);
    }

    /**
     * 根据计划ID获取回复数据
     *
     * @param planId 计划ID
     * @return 回复数据
     */
    @PostMapping("/getReplyData")
    public ResponseEntity getReplyData(long planId) {
        List<BasicForcaseQuota> quotaList = basicPlanLogic.selectQuotaList(planId);
        return ResponseEntity.ok(quotaList);
    }

    /**
     * 明细查看
     *
     * @param planId 计划ID
     * @return 明细查看数据
     */
    @PostMapping("/get/date/interval")
    public ResponseEntity getDateDataInterval(long planId) {
        List<BasicForcaseData> dataList = basicPlanLogic.selectDataList(planId);
        return ResponseEntity.ok(dataList);
    }

    @PostMapping("/get/date/data")
    public ResponseEntity getDateData(long planId, Integer page, Integer limit, String q) {
        return basicPlanLogic.getDateData(planId, page, limit, q);
    }

    /**
     * 供应商回复 查看汇总列表
     */
    @GetMapping("/get/reply/supplier")
    public ResponseEntity getSupplierReply(BasicForeignQuotaQuery query) {
        return basicPlanLogic.getSupplierReply(query);
    }

    /**
     * 保存供应商回复数据
     *
     * @param data 回复数据
     * @return 是否成功
     */
    @PostMapping("/save/reply/detail")
    public ResponseEntity saveSupplierReply(String data, boolean isPurchaseReplace, String detail_remark) {
        return basicPlanLogic.saveSupplierReply(data, isPurchaseReplace, detail_remark, LoginUtil.getUserId());
    }

    /**
     * 根据详情ID获取计划详情时间数据
     *
     * @param detailId 详情ID
     * @return 详情时间数据
     */
    @PostMapping("/by/detail_id/get/plan_data")
    public ResponseEntity byDetailIdGetPlanData(Long detailId, String supplierCode) throws ParseException {
        return basicPlanLogic.byDetailIdGetPlanData(detailId, supplierCode);
    }

    /**
     * 回复导出
     *
     * @param planId   计划ID
     * @param status   第一次回复的状态
     * @param response 响应
     */
    @RequestMapping("/get/reply/export")
    public void getReplyExport(HttpServletRequest request, HttpServletResponse response, Long planId,
                               Integer status, String isRiskMaterial, String supplier, String purchaseGroup) throws
            ParseException
    {
        basicPlanLogic.getReplyExport(request, response, planId, status, isRiskMaterial, supplier, purchaseGroup);
    }

    /**
     * 回复导入
     *
     * @param planId 计划ID
     */
    @RequestMapping("/reply/import")
    public ResponseEntity replyImport(@RequestPart("file") MultipartFile file, Long planId) {
        return basicPlanLogic.replyImport(file, planId);
    }

    /**
     * 检查标准预测计划发布或回复情况
     *
     * @param planId    标准预测计划ID
     * @param checkType 检查类型 1:发布情况 2:回复情况
     * @return 结果
     */
    @GetMapping("/check/plan/release/reply/situation")
    public ResponseEntity checkPlanReleaseReplySituation(Long planId, Integer checkType) {
        return basicPlanLogic.checkPlanReleaseReplySituation(planId, checkType);
    }

    /**
     * 采购员确认 [确定OR拒绝]
     *
     * @param isBatch       是否批量操作
     * @param detailId      明细ID
     * @param supplierCode  供应商编码
     * @param replyIds      回复IDS
     * @param refusalReason 拒绝原因
     * @return 是否确认成功
     */
    @PostMapping("/confirm/reply/by/ids")
    public ResponseEntity getConfirmData(
            Boolean isBatch, Long detailId, String supplierCode, String replyIds, String refusalReason,
            String confirmType
    )
    {
        return basicPlanLogic.getConfirmData(isBatch, detailId, supplierCode, replyIds, refusalReason, confirmType);
    }

    /**
     * 获取待发布数据
     *
     * @param page  当前页码
     * @param rows  每页显示行数
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/find/wait/release/data")
    public ResponseEntity findWaitReleaseData(Integer page, Integer rows, BasicPlanQuery query) {
        return basicPlanLogic.findWaitReleaseData(page, rows, query);
    }

    /**
     * 根据计划类型和计划ID下载计划文件
     *
     * @param planId   计划ID
     * @param type     计划类型 要货计划、标准预测、外协预测 外包物料到货需求
     * @param request  请求
     * @param response 响应
     * @return 文件
     */
    @GetMapping("/download/plan/file")
    public String downloadPlanFileByTypeAndPlanId(Long planId,
                                                  @RequestParam(value = "type", defaultValue = "") String type,
                                                  HttpServletRequest request,
                                                  HttpServletResponse response,
                                                  @RequestParam(value = "downFileName", defaultValue = "") String downFileName,
                                                  @RequestParam(value = "downFilePath", defaultValue = "") String downFilePath)
    {
        return basicPlanLogic.downloadPlanFileByTypeAndPlanId(planId, type, request, response, downFileName,
                downFilePath);
    }


    /**
     * 保存发布数据
     *
     * @param planId 计划ID
     * @param data   待保存数据
     * @return 结果
     */
    @PostMapping("/save/release/data")
    public ResponseEntity saveReleaseData(Long planId, String data) {
        return basicPlanLogic.saveReleaseData(planId, data);
    }

    //发布数据导出
    @RequestMapping("/deriveData")
    public void downLoadPcn(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam(value = "planId") Long planId,
                            @RequestParam(value = "purchaseGroup") String purchaseGroup)
    {
        basicPlanLogic.downLoadPcn(request, response, planId, purchaseGroup);
    }

    @PostMapping("/uploadData")
    public Map<String, Object> impXunJiaMaterial(@RequestPart("file") MultipartFile file) {
        return basicPlanLogic.impXunJiaMaterial(file);
    }

    @PostMapping("/save/reply/details")
    public ResponseEntity saveSupplierReplys(String data, boolean isPurchaseReplace) {
        return basicPlanLogic.saveSupplierReplys(data, isPurchaseReplace);
    }

    /**
     * 获取供应商回复进度
     */
    @PostMapping("/get/supplier/reply")
    public ResponseEntity getSupplierReply(Integer page, Integer rows, Long planId) {
        return basicPlanLogic.getSupplierReply(page, rows, planId);
    }

    /**
     * 列表
     *
     * @return
     */
    @RequestMapping("/planList")
    public ResponseEntity<List<BasicPlan>> planList() {

        return ResponseEntity.ok(basicPlanLogic.selectPlanList());
    }


    //缺料预警导出
    @RequestMapping("/downLoadRisk")
    public void downLoadRisk(HttpServletRequest request, HttpServletResponse response, String product, String supplier,
                             String isRisk, String serialNumber)
    {
        basicPlanLogic.downLoadRisk(request, response, product, supplier, isRisk, serialNumber);
    }

    /**
     * 删除标准计划预测
     */

    @PostMapping("/remove")
    public ResponseEntity remove(Long id) {
        basicPlanLogic.remove(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("downloadSapData")
    public void downloadSapData(Long planId, HttpServletRequest request, HttpServletResponse response) {
        basicPlanLogic.downloadSapData(planId, request, response);
    }
}
