package com.jwipc.order.controller.pc;

import cn.hutool.core.collection.CollUtil;
import com.jwipc.order.entity.BasicPlan;
import com.jwipc.order.logic.BasicPlanLogic;
import com.jwipc.order.model.po.BasicForecastPlanReleasePO;
import com.jwipc.order.model.query.BasicPlanQuery;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.SysConfigClient;
import com.jwipc.sys.client.dto.FileResposeCDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

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

/**
 * <p>
 * 标准预测计划
 * </p>
 *
 * @Author: swq
 * @Date: 2024/3/12 14:44
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/basicPlan")
public class BasicPlanPageController {
    private final BasicPlanLogic basicPlanLogic;
    private final SysConfigClient sysConfigClient;

    @RequestMapping("/index")
    public String index(Model model) {
        FileResposeCDTO fileResposeDTO = sysConfigClient.getFileConfig("basicPlan");
        model.addAttribute("fileId", fileResposeDTO.getId());
        model.addAttribute("isSupplier", true == LoginUtil.isSupplier() ? 1 : 0);
        return MODULE_ROOT + "/delivery/basicPlan/list";
    }

    /**
     * 供应商回复
     *
     * @return 页面
     */
    @GetMapping("/supplier/replyUI")
    public String replyConfirm(Model model, Long id, @RequestParam(defaultValue = "false") boolean isPurchaseReplace) {
        BasicPlan plan = basicPlanLogic.selectById(id);
        model.addAttribute("id", id);
        model.addAttribute("plan", plan == null ? new BasicPlan() : plan);
        if (isPurchaseReplace || LoginUtil.isSupplier() == true) {
            model.addAttribute("isSupplier", true);
        } else {
            model.addAttribute("isSupplier", false);
        }
        // 供应商 不可用采购代答复功能
        if (LoginUtil.isSupplier() == true) {
            model.addAttribute("isPurchaseReplace", false);
        } else {
            model.addAttribute("isPurchaseReplace", isPurchaseReplace);
        }
        return MODULE_ROOT + "/delivery/basicPlan/supplier_reply";
    }


    /**
     * 查看
     *
     * @return 页面
     */
    @GetMapping("/show/date")
    public String showDate(Model model, Long id) {
        model.addAttribute("planId", id);
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        return MODULE_ROOT + "/delivery/basicPlan/show_reply_by_date";
    }

    /**
     * 确认回复
     *
     * @return 页面
     */
    @GetMapping("/confirm/replyUI")
    public String confirmReply(Model model, Long id) {
        model.addAttribute("id", id);
        return MODULE_ROOT + "/delivery/basicPlan/confirm_reply";
    }

    /**
     * 发布调整界面
     *
     * @return 调整
     */
    @GetMapping("/release/adjustmentUI")
    public String releaseAdjustment(Model model, Long id) {
        model.addAttribute("id", id);
        BasicPlanQuery query = new BasicPlanQuery();
        query.setPlanId(id);
        query.setUserId(LoginUtil.getUserId());
        final BasicForecastPlanReleasePO planSituation = basicPlanLogic.findPlanSituation(query);
        if (null == planSituation) {
            model.addAttribute("planTotalNum", 0);
            model.addAttribute("adTotalNum", 0);
            model.addAttribute("unAdTotalNum", 0);
        } else {
            model.addAttribute("planTotalNum", planSituation.getTotalPlanNum());
            model.addAttribute("adTotalNum", planSituation.getTotalDistributeNum());
            model.addAttribute("unAdTotalNum", planSituation.getTotalSurplusNum());
        }
        return MODULE_ROOT + "/delivery/basicPlan/plan_release_adjustment";
    }

    //导入页面
    @GetMapping("/uploadData")
    public String uploadData(Model model) {
        return MODULE_ROOT + "/delivery/basicPlan/uploadData";
    }

    /**
     * 查看回复进度
     *
     * @return 页面
     */
    @GetMapping("/look/reply/progress")
    public String lookReplyProgress(Model model, Long id) {
        model.addAttribute("planId", id);
        return MODULE_ROOT + "/delivery/basicPlan/look_reply_progress";
    }

    //导入页面
    @GetMapping("/warning")
    public String warning(Model model) {
        List<BasicPlan> list = basicPlanLogic.selectPlanList();
        model.addAttribute("planId", CollUtil.isEmpty(list) ? 0 : list.get(0).getId());
        model.addAttribute("isSupplier", LoginUtil.isSupplier());
        model.addAttribute("serialNumber", CollUtil.isEmpty(list) ? 0 : list.get(0).getSerialNumber());
        return MODULE_ROOT + "/delivery/basicPlan/warning";
    }
}
