package com.foreverwin.mesnac.dispatch.controller;

import cn.hutool.core.date.DateUtil;
import com.foreverwin.mesnac.dispatch.model.ShopOrderRelease;
import com.foreverwin.mesnac.dispatch.service.ShopOrderReleaseService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * ClassName: ShopOrderReleaseController
 *
 * @author Leon.L
 *
 * Copyright (C) 2021-2022 上海昊声电子信息技术有限公司.
 *
 * All Rights Reserved
 *
 * http://www.foreverwin.com.cn
 *
 * Create Time: 2021/06/02
 */
@RestController
@RequestMapping("/shopOrderRelease")
public class ShopOrderReleaseController {

    @Autowired
    private ShopOrderReleaseService shopOrderReleaseService;

    @ResponseBody
    @PostMapping("queryShopOrder")
    public R queryShopOrder(@RequestBody ShopOrderRelease shopOrderRelease) {
        List<ShopOrderRelease> list = null;
        try {
            if (shopOrderRelease == null) {
                shopOrderRelease = new ShopOrderRelease();
            }
            shopOrderRelease.setSite(CommonMethods.getSite());

            if (shopOrderRelease.getStartFromDate_S() != null) {
                shopOrderRelease.setStartFromDate(DateUtil.parse(shopOrderRelease.getStartFromDate_S()));
            }
            if (shopOrderRelease.getStartToDate_S() != null) {
                shopOrderRelease.setStartToDate(DateUtil.parse(shopOrderRelease.getStartToDate_S()));
            }
            if (shopOrderRelease.getCompleteFromDate_S() != null) {
                shopOrderRelease.setCompleteFromDate(DateUtil.parse(shopOrderRelease.getCompleteFromDate_S()));
            }
            if (shopOrderRelease.getCompleteToDate_S() != null) {
                shopOrderRelease.setCompleteToDate(DateUtil.parse(shopOrderRelease.getCompleteToDate_S()));
            }

            list = shopOrderReleaseService.findShopOrderList(shopOrderRelease);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(list);
    }

    /**
     * 工单整理- 下达
     *
     * @param shopOrderList
     * @return
     */
    @ResponseBody
    @PostMapping("shopOrderRelease")
    public R shopOrderRelease(@RequestBody List<ShopOrderRelease> shopOrderList) {

        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            shopOrderReleaseService.shopOrderRelease(site, user, shopOrderList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }


    /**
     * 工单整理- 刷新派工单
     *
     * @param shopOrderList
     * @return
     */
    @ResponseBody
    @PostMapping("dispatchRefresh")
    public R dispatchRefresh(@RequestBody List<ShopOrderRelease> shopOrderList) {

        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            shopOrderReleaseService.dispatchRefresh(site, user, shopOrderList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }
}
