package com.foreverwin.mesnac.dispatch.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.common.dto.SfcDispatchDto;
import com.foreverwin.mesnac.common.enums.DispatchStatusEnum;
import com.foreverwin.mesnac.dispatch.model.SfcDispatch;
import com.foreverwin.mesnac.dispatch.service.SfcDispatchService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
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 paramMap
     * @return
     */
    @ResponseBody
    @GetMapping("findSfcDispatch")
    public R findSfcDispatch(@RequestParam(required = false) Map paramMap) {
        List<SfcDispatchDto> list;
        try {
            paramMap.put("site", CommonMethods.getSite());

            list = sfcDispatchService.findSfcDispatchList(paramMap);
        } 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());
        }
    }

    @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
                    .or().like(SfcDispatch::getShopOrder, frontPage.getGlobalQuery())
                    .or().like(SfcDispatch::getSfc, frontPage.getGlobalQuery())
                    .or().like(SfcDispatch::getDispatchNo, frontPage.getGlobalQuery())
                    .or().like(SfcDispatch::getDispatchStatus, frontPage.getGlobalQuery())
            );
        }
        result = sfcDispatchService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }


    @GetMapping("/gantt-list")
    public R ganttList(@RequestParam(required = false) Map paramMap) {
        Map<String, Object> result = new HashMap<>();

        try {
            String site = CommonMethods.getSite();
            paramMap.put("site", site);
            paramMap.put("status", "('RELEASE','START','COMPLETE')");


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

        return R.ok(result);
    }
}