package com.malk.kuaikeli.controller;

import com.alibaba.fastjson.JSON;
import com.malk.kuaikeli.service.KKLService;
import com.malk.server.common.McR;
import com.malk.utils.UtilServlet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 错误抛出与拦截详见 CatchException
 */
@Slf4j
@RestController
@RequestMapping()
public class KKLController {

    @Autowired
    private KKLService kklService;

    /**
     * [动态] 发起单据, 分供应商进行流程推送
     */
    @PostMapping("supplier")
    McR supplier(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("supplier, {}", JSON.toJSONString(data));

        kklService.matchSupplier(data);
        return McR.success();
    }

    /**
     * 菜单计划, 编辑校验
     * -
     * 前端提交动作: 校验是否审批通过, 是否当日之前计划, 是否当日且在16点及以后时间, 均不允许提交
     * 后端校验返回、加任何产品都要走审批，减少20%以内直接过，每一个原材料
     */
    @PostMapping("validate")
    McR validate(@RequestBody Map data) {

        log.info("validate, {}", JSON.toJSONString(data));

        kklService.validateApprove(data);
        return McR.success();
    }

    /**
     * 定价单: 项目点 & 供应商大类, 数据匹配
     */
    @PostMapping("service")
    McR service(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("service, {}", JSON.toJSONString(data));

        kklService.serviceRate(data);
        return McR.success();
    }

    /**
     * 物料档案: 价格调整
     */
    @PostMapping("adjust")
    McR adjust(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("adjust, {}", JSON.toJSONString(data));

        kklService.adjustCost(data);
        return McR.success();
    }

    /**
     * 供应商原材料档案: 新增匹配
     */
    @PostMapping("addition")
    McR addition(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("addition, {}", JSON.toJSONString(data));

        kklService.adjustCost(data);
        return McR.success();
    }


    /**
     * 周日同步更新价格
     */
    @PostMapping("sync")
    McR sync() {

        log.info("sync price");

        kklService.syncPrice();
        return McR.success();
    }

    @PostMapping("test")
    McR test(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);

        log.info("xxxx, {}", data);
        return McR.success();
    }

}
