package com.malk.fenggefushi.controller;

import com.malk.fenggefushi.service.HeiHuOrderService;
import com.malk.server.common.McR;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.framework.qual.PostconditionAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/HeiHuOrder")
public class HeiHuOrderController {
    @Autowired
    private HeiHuOrderService huOrderService;

    /*=======================会用到的接口========================*/

    /*TODO：获取黑湖工单登录token*/
    @SneakyThrows
    @PostMapping("/getToken")
    McR HeiHuAccessToken(){
        log.info("------------获取到黑湖工单的token------------");
        return McR.success(huOrderService.HeiHuAccessToken());
    }

    /*TODO：【采购订单】发起宜搭审批流程*/
    @SneakyThrows
    @PostMapping("/startYidaProcurementAproval")
    McR startYidaProcurementAproval(@RequestParam String OrderNo, @RequestParam String FormUuType){
        log.info("-----------开始抓取黑湖采购工单，准备发起宜搭审批流程---------");
        return McR.success(huOrderService.startYidaProcurementAproval(OrderNo,FormUuType));
    }

    /*TODO:采购订单创建触发*/
    @SneakyThrows
    @PostMapping("/Procurement")
    McR Procurement(@RequestBody Map body){
        log.info("【接收到黑湖采购工单创建请求】");
        return McR.success(huOrderService.Procurement(body));
    }

    /*TODO:销售订单修改*/
    @SneakyThrows
    @PostMapping("/salesUpdate")
    McR salesUpdate(@RequestBody Map body){
        log.info("[接收到销售订单审批单修改],{}",body);
        return McR.success(huOrderService.salesUpdate(body));
    }

    /*todo:销售订单创建触发*/
    @SneakyThrows
    @PostMapping("/salesAproval")
    McR salesAproval(@RequestBody Map body){
        log.info("【接收到黑湖销售工单创建请求】,{}",body);
        return McR.success(huOrderService.salesAproval(body));
    }

    /*todo:销售订单不单独走审批，存入销售订单流程档案*/
    @SneakyThrows
    @PostMapping("/startYidaSalesAproval")
    McR startYidaSalesAproval(@RequestParam String OrderNo,@RequestParam String fromUuid) {
        return McR.success(huOrderService.StartYidaAproval(OrderNo,fromUuid));
    }

    /*todo:查询产品颜色接口*/
    @SneakyThrows
    @PostMapping("/getProductColor")
    McR getProductColor(@RequestParam List list){
        return McR.success(huOrderService.getProductColor(list));
    }



    /*todo:销售订单审批结束后同步黑湖工单状态*/
    @SneakyThrows
    @PostMapping("/SyncOrderStatus")
    McR SyncOrderStatus(@RequestParam String formInstanceId,@RequestParam String Approvalresult){
        log.info("-----------【开始回传销售订单状态】----------");
        return McR.success(huOrderService.SyncOrderStatus(formInstanceId,Approvalresult));
    }

    /*todo：采购订单审批结束同步黑湖工单状态*/
    @SneakyThrows
    @PostMapping("/SyncPurchaseOrderStatus")
    McR SyncPurchaseOrderStatus(@RequestParam String formInstanceId,@RequestParam String Approvalresult){
        log.info("------------【开始回传采购订单状态】--------");
        return McR.success(huOrderService.SyncPurchaseOrderStatus(formInstanceId,Approvalresult));
    }

    /*todo：定时抓取采购订单*/
    @SneakyThrows
    @PostMapping("/test12")
    @Scheduled(cron = "0 */5 * * * ?")
    McR purchaseSchedule(){
        List<Map<String, Object>> dataList = huOrderService.queryAllPurchase();

        return McR.success(huOrderService.purchaseSchedule(dataList));
    }

//    /*todo：定时抓取修改采购订单*/
//    @SneakyThrows
//    @PostMapping("/testUpdate")
//    @Scheduled(cron = "0 3,8 * * * ?")
//    McR purchaseUpdateSchedule(){
//        log.info("开始执行定时抓取修改后的采购订单数据");
//        System.out.println("开始执行定时抓取修改后的采购订单数据");
//        return McR.success(huOrderService.purchaseUpdateSchedule());
//    }

}
