package com.foreverwin.mesnac.dispatch.controller;

import cn.hutool.core.date.DateUtil;
import com.foreverwin.mesnac.common.dto.SfcDispatchDto;
import com.foreverwin.mesnac.dispatch.model.ShopOrderRelease;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.dispatch.model.SfcDispatch;
import com.foreverwin.mesnac.dispatch.service.SfcDispatchService;
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.List;

/**
 *
 * 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 workOrder, String shopOrder,
                            String resrce, String resourceType, String operation, String sfc, String componentDescription, String turnOperation,
                            String isDispatch, String startFromDate_S, String startToDate_S, String completeFromDate_S, String completeToDate_S) {
        List<ShopOrderRelease> list;
        try {
            ShopOrderRelease shopOrderRelease = new ShopOrderRelease();
            shopOrderRelease.setSite(CommonMethods.getSite());
            shopOrderRelease.setWorkCenter(workCenter);
            shopOrderRelease.setDispatchStatus(dispatchStatus);
            shopOrderRelease.setItem(item);
            shopOrderRelease.setWorkOrder(workOrder);
            shopOrderRelease.setShopOrder(shopOrder);
            shopOrderRelease.setResrce(resrce);
            shopOrderRelease.setResourceType(resourceType);
            shopOrderRelease.setOperation(operation);
            shopOrderRelease.setSfc(sfc);
            shopOrderRelease.setComponentDescription(componentDescription);
            shopOrderRelease.setTurnOperation(turnOperation);
            shopOrderRelease.setIsDispatch(isDispatch);
            if (startFromDate_S != null) {
                shopOrderRelease.setStartFromDate(DateUtil.parse(startFromDate_S));
            }
            if (startToDate_S != null) {
                shopOrderRelease.setStartToDate(DateUtil.parse(startToDate_S));
            }
            if (completeFromDate_S != null) {
                shopOrderRelease.setCompleteFromDate(DateUtil.parse(completeFromDate_S));
            }
            if (completeToDate_S != null) {
                shopOrderRelease.setCompleteToDate(DateUtil.parse(completeToDate_S));
            }

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

        return R.ok(list);
    }

    /**
     * 查询派工数据
     *
     * @param shopOrderRelease
     * @return
     */
    @ResponseBody
    @PostMapping("findSfcDispatch")
    public R findSfcDispatch(@RequestBody ShopOrderRelease shopOrderRelease) {
        List<ShopOrderRelease> list;
        try {
            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 = sfcDispatchService.findSfcDispatchList(shopOrderRelease);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(list);
    }


    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SfcDispatch> frontPage, SfcDispatch sfcDispatch){
        IPage result;
        QueryWrapper<SfcDispatch> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(sfcDispatch);
        sfcDispatch.setSite(CommonMethods.getSite());
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SfcDispatch::getHandle, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getSite, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getShopOrder, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getSfc, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getBlankingSize, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getDispatchSeq, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getDispatchNo, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getDispatchStatus, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getDrawingsNo, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getDrawingsRevision, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getIsLock, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getRouterBo, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getStepId, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getOperation, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getResourceType, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getWorkCenter, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getResrce, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getEmployee, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getTurnOperation, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getIsDispatch, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getIsImport, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getRemark, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getCreateUser, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getModifyUser, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getOther1, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getOther2, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getOther3, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getOther4, frontPage.getGlobalQuery())
                .or().like(SfcDispatch::getOther5, frontPage.getGlobalQuery())
    );
        }
        result = sfcDispatchService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }


    @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());
        }
    }
}