package com.foreverwin.mesnac.dispatch.controller;

import cn.hutool.core.date.DateUtil;
import com.foreverwin.mesnac.common.dto.SfcDispatchDto;
import com.foreverwin.mesnac.common.enums.DispatchStatusEnum;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.dispatch.service.SfcDispatchService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    public SfcDispatchService sfcDispatchService;


    /**
     * 派工生产批次清单导出
     *
     * @param workCenter
     * @param dispatchStatus
     * @param item
     * @param workOrder
     * @param shopOrder
     * @param resrce
     * @param resourceType
     * @param operation
     * @param sfc
     * @param componentDescription
     * @param turnOperation
     * @param isDispatch
     * @param startFromDate_S
     * @param startToDate_S
     * @param completeFromDate_S
     * @param completeToDate_S
     * @return
     */
    @ResponseBody
    @GetMapping("getSfcDispatch")
    public R getSfcDispatch(String workCenter, String dispatchStatus, String item, String itemNumber, String workOrder, String shopOrder, String blankingSize,
                            String resrce, String resourceType, String operation, String operationDescription,  String sfc, String component, String componentDescription, String turnOperation,
                            String isDispatch, String startFromDate_S, String startToDate_S, String completeFromDate_S, String completeToDate_S) {
        List<SfcDispatchDto> list;
        try {
            SfcDispatchDto sfcDispatchDto = new SfcDispatchDto();
            sfcDispatchDto.setSite(CommonMethods.getSite());
            sfcDispatchDto.setUser(CommonMethods.getUser());
            sfcDispatchDto.setWorkCenter(workCenter);
            sfcDispatchDto.setDispatchStatus(dispatchStatus);
            sfcDispatchDto.setItem(item);
            sfcDispatchDto.setItemNumber(itemNumber);
            sfcDispatchDto.setWorkOrder(workOrder);
            sfcDispatchDto.setShopOrder(shopOrder);
            sfcDispatchDto.setResrce(resrce);
            sfcDispatchDto.setResourceType(resourceType);
            sfcDispatchDto.setOperation(operation);
            sfcDispatchDto.setSfc(sfc);
            sfcDispatchDto.setBlankingSize(blankingSize);
            sfcDispatchDto.setComponent(component);
            sfcDispatchDto.setComponentDescription(componentDescription);
            sfcDispatchDto.setTurnOperation(turnOperation);
            sfcDispatchDto.setIsDispatch(isDispatch);
            if (startFromDate_S != null) {
                sfcDispatchDto.setStartFromDate(DateUtil.parse(startFromDate_S));
            }
            if (startToDate_S != null) {
                sfcDispatchDto.setStartToDate(DateUtil.parse(startToDate_S));
            }
            if (completeFromDate_S != null) {
                sfcDispatchDto.setCompleteFromDate(DateUtil.parse(completeFromDate_S));
            }
            if (completeToDate_S != null) {
                sfcDispatchDto.setCompleteToDate(DateUtil.parse(completeToDate_S));
            }

            list = sfcDispatchService.findSfcDispatchList(sfcDispatchDto);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(list);
    }

    /**
     * 查询派工数据
     *
     * @param sfcDispatchDto
     * @return
     */
    @ResponseBody
    @PostMapping("findSfcDispatch")
    public R findSfcDispatch(@RequestBody SfcDispatchDto sfcDispatchDto) {
        List<SfcDispatchDto> list;
        try {
            sfcDispatchDto.setSite(CommonMethods.getSite());
            sfcDispatchDto.setUser(CommonMethods.getUser());
            if (sfcDispatchDto.getStartFromDate_S() != null) {
                sfcDispatchDto.setStartFromDate(DateUtil.parse(sfcDispatchDto.getStartFromDate_S()));
            }
            if (sfcDispatchDto.getStartToDate_S() != null) {
                sfcDispatchDto.setStartToDate(DateUtil.parse(sfcDispatchDto.getStartToDate_S()));
            }
            if (sfcDispatchDto.getCompleteFromDate_S() != null) {
                sfcDispatchDto.setCompleteFromDate(DateUtil.parse(sfcDispatchDto.getCompleteFromDate_S()));
            }
            if (sfcDispatchDto.getCompleteToDate_S() != null) {
                sfcDispatchDto.setCompleteToDate(DateUtil.parse(sfcDispatchDto.getCompleteToDate_S()));
            }

            list = sfcDispatchService.findSfcDispatchList(sfcDispatchDto);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(list);
    }


    @ResponseBody
    @PostMapping("/saveDispatch")
    public R saveDispatch(@RequestBody List<SfcDispatchDto> sfcDispatchList) {
        Boolean flag;
        StringBuffer message = new StringBuffer();
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            flag = sfcDispatchService.saveDispatch(site, user, message, sfcDispatchList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }



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

    /**
     * 派工发布
     *
     * @param sfcDispatchList
     * @return
     */
    @ResponseBody
    @PostMapping("/releaseDispatch")
    public R releaseDispatch(@RequestBody List<SfcDispatchDto> sfcDispatchList) {
        Boolean flag;
        StringBuffer message = new StringBuffer();
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            flag = sfcDispatchService.releaseDispatch(site, user, message, sfcDispatchList);
        } catch (Exception e) {
            return R.failed("派工发布失败：" + e.getMessage());
        }



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

    /**
     * 返回新建状态
     *
     * @param sfcDispatchList
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/returnToNew")
    public R returnToNew(@RequestBody List<SfcDispatchDto> sfcDispatchList) {
        String user = CommonMethods.getUser();
        try {
            sfcDispatchService.returnToNew(user, sfcDispatchList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }

    /**
     * 暂挂
     *
     * @param sfcDispatchList
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/pause")
    public R pause(@RequestBody List<SfcDispatchDto> sfcDispatchList) {
        String user = CommonMethods.getUser();
        try {
            sfcDispatchService.pause(user, sfcDispatchList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }

    /**
     * 取消
     *
     * @param sfcDispatchList
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/cancel")
    public R cancel(@RequestBody List<SfcDispatchDto> sfcDispatchList) {
        String user = CommonMethods.getUser();
        try {
            sfcDispatchService.cancel(user, sfcDispatchList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok();
    }

    @PostMapping("/import")
    public R importFile(@PathParam("fileType") String fileType, @Param("site") String site, @Param("user") String user, @RequestParam("file") MultipartFile multipartFile) {
        Boolean flag;
        StringBuffer message = new StringBuffer();
        try {
            flag = sfcDispatchService.importDispatch(site, user, fileType, message, multipartFile);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }


        if (flag) {
            message.insert(0, "派工数据导入成功");
            return R.ok(message.toString());
        } else {
            message.insert(0, "派工数据导入失败：");
            return R.failed(message.toString());
        }
    }


    @PostMapping("/gantt-list")
    public R ganttList(@RequestBody SfcDispatchDto sfcDispatchDto) {
        Map<String, Object> result = new HashMap<>();

        try {
            String site = CommonMethods.getSite();
            if (StringUtil.isBlank(sfcDispatchDto.getStartFromDate_S())) {
                throw BusinessException.build("派工开始时间不能为空！");
            }
            if (StringUtil.isBlank(sfcDispatchDto.getStartToDate_S())) {
                throw BusinessException.build("派工开始时间不能为空！");
            }
            sfcDispatchDto.setSite(site);
            sfcDispatchDto.setDispatchStatus(DispatchStatusEnum.RELEASE.getCode());
            sfcDispatchDto.setStartFromDate(DateUtil.parseDate(sfcDispatchDto.getStartFromDate_S()));
            sfcDispatchDto.setStartToDate(DateUtil.parseDate(sfcDispatchDto.getStartToDate_S()));


            result = sfcDispatchService.ganttList(site, sfcDispatchDto);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


}