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.mesnac.dispatch.service.impl.ShopOrderReleaseServiceImpl;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
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;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        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);
    }

    /**
     * 工单整理- 修改工单BOM组件的数量
     *
     * @param shopOrderList
     * @return
     */
    @ResponseBody
    @PostMapping("shopOrderBomComponentUpdate")
    public R shopOrderBomComponentUpdate(@RequestBody List<ShopOrderRelease> shopOrderList) {

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

        return R.ok();
    }

    /**
     * 工单整理- 下达
     *
     * @param shopOrderList
     * @return
     */
    @ResponseBody
    @PostMapping("shopOrderRelease")
    public R shopOrderRelease(@RequestBody List<ShopOrderRelease> shopOrderList) {
        Boolean flag = true;
        StringBuffer message = new StringBuffer();
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        if (shopOrderList == null || shopOrderList.size() <= 0) {
            return R.failed("请至少选择一笔需要下达的工单");
//            throw new BaseException("请至少选择一笔需要下达的工单");
        }

            for (ShopOrderRelease shopOrderReleaseModel : shopOrderList) {
                try {
                    shopOrderReleaseService.shopOrderRelease(site, user,message, shopOrderReleaseModel);
                }catch (Exception e) {
                    flag = false;
                    message.append(e.getMessage());
                }
            }


        if (flag){
            message.insert(0, "工单下达成功");
            return R.ok(message.toString());
        }else {
            message.insert(0, "工单下达失败：");
            return R.failed(message.toString());
        }

    }


    /**
     * 工单整理- 刷新派工单
     *
     * @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();
    }
}
