package com.jwipc.order.api.controller;

import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.finance.logic.PaymentPlanLogic;
import com.jwipc.order.api.dto.CreateShortagePlanDTO;
import com.jwipc.order.api.logic.CreateShortagePlanLogic;
import com.jwipc.order.logic.ShortagePlanHanaLogic;
import com.jwipc.util.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author: swq
 * @Date: 2024/4/3 11:20
 */
@RestController
@RequestMapping("/sap/api/")
@RequiredArgsConstructor
@Slf4j
public class CreateShortagePlanApiController {
    private final CreateShortagePlanLogic createShortagePlanLogic;
    private final ShortagePlanHanaLogic shortagePlanHanaLogic;
    private final PaymentPlanLogic paymentPlanLogic;

    @PostMapping("createShortagePlan")
    public ApiResult createBasicPlan(@RequestBody CreateShortagePlanDTO createBasicPlanDTO) {
        return createShortagePlanLogic.createShortagePlan(createBasicPlanDTO);
    }

    @PostMapping("createShortageHanaPlan")
    public ApiResult createShortageHanaPlan() {
        log.info("开始生成" +  new Date() + "的欠料计划数量");
        shortagePlanHanaLogic.initShortagePlanHana();
        log.info("生成成功");
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

    @PostMapping("createPaymentPlan")
    public ApiResult createPaymentPlan() {
        Calendar cl = Calendar.getInstance();
        String paymentExpireStr = DateUtils.formatYYYYMMDDDash(cl.getTime());
        String year = paymentExpireStr.split("-")[0];
        String month = paymentExpireStr.split("-")[1];
        paymentPlanLogic.addNewPaymentPlan(year, month, false);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }
}
