package com.malk.junengtai.controller;

import com.malk.junengtai.service.PurchaseService;
import com.malk.server.common.McException;
import com.malk.server.common.McR;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/Purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /*todo:手动同步档案表*/
    @SneakyThrows
    @PostMapping("/synchronizedArchiveTable")
    McR synchronizedArchiveTable(@RequestParam String name,@RequestParam String date){
        log.info("开始同步,{}",name);
        purchaseService.synchronizedArchiveTable(name,date);
        log.info("同步完成");
        return McR.success();
    }

    /*todo：组件信息手动同步接口*/
    @SneakyThrows
    @PostMapping("/componentInfo")
    McR ComponentInformation(@RequestParam String date){
        log.info("----------开始全量同步组件信息----------");
        purchaseService.ComponentInformation(date);
        log.info("-----------组件信息已同步完成-----------");
        return McR.success();
    }

    /*todo:供应商信息同步接口*/
    @SneakyThrows
    @PostMapping("/supplierInfo")
    McR supplierInformation(@RequestParam String dateString1){
        log.info("---------开始全量同步供应商信息---------");
        purchaseService.supplierInformation(dateString1);
        log.info("---------供应商信息同步完成-------------");
        return McR.success();
    }

    /*todo:查询库存信息接口*/
    @SneakyThrows
    @PostMapping("/queryInventory")
    McR queryInventory(@RequestBody Map<String,String> body){
        return McR.success(purchaseService.queryInventory(body));
    }

    /*todo:创建采购订单接口*/
    @SneakyThrows
    @PostMapping("/purchaseOrder")
    McR purchaseOrder(HttpServletRequest request){
        Map<String, ?> data = UtilServlet.getParamMap(request);
        log.info("采购订单, {}", data);
        McException.assertParamException_Null(data, "instanceId");
        return purchaseService.purchaseOrder(UtilMap.getString(data,"instanceId"));
    }

    /*todo:拣货单接口*/
    @SneakyThrows
    @PostMapping("/pickingList")
    McR pickingList(@RequestBody Map<String,String> body){
        log.info("开始查询拣货单明细");
        return McR.success(purchaseService.pickingList(body));
    }

}
