package com.foreverwin.mesnac.dispatch.controller;

import cn.hutool.core.date.DateUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.dispatch.dto.IssueItemDto;
import com.foreverwin.mesnac.dispatch.service.IssueItemService;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.CommonMethods;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.dispatch.service.CallItemService;
import com.foreverwin.mesnac.dispatch.model.CallItem;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Leon.L
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/Z-CALL-ITEM")
public class CallItemController {

    @Autowired
    private CallItemService callItemService;


    /**
     * 查询叫料数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/findCallItemList")
    public R findCallItemList(@RequestParam(required = false) Map paramMap){
        List<CallItem> result;
        try {
            String site = CommonMethods.getSite();
            paramMap.put("site", site);
            result = callItemService.findCallItemList(paramMap);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }


        return R.ok(result);
    }

    /**
     * 查询叫料明细数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/findCallItemDetailList")
    public R findCallItemDetailList(@RequestBody CallItem callItem){
        List<CallItem> result;
        try {
            callItem.setSite(CommonMethods.getSite());
            if (callItem.getRequireFromDate_S() != null) {
                callItem.setRequireFromDate(DateUtil.parse(callItem.getRequireFromDate_S()));
            }
            if (callItem.getRequireToDate_S() != null) {
                callItem.setRequireToDate(DateUtil.parse(callItem.getRequireToDate_S()));
            }
            if (callItem.getStartFromDate_S() != null) {
                callItem.setStartFromDate(DateUtil.parse(callItem.getStartFromDate_S()));
            }
            if (callItem.getStartToDate_S() != null) {
                callItem.setStartToDate(DateUtil.parse(callItem.getStartToDate_S()));
            }
            result = callItemService.findCallItemDetailList(callItem);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }


        return R.ok(result);
    }

    /**
     * 查询叫料明细数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/findCallItemDetailListByDispatchBo")
    public R findCallItemDetailListByDispatchBo(@RequestBody  List<String> dispatchBoList){
        List<CallItem> result;
        try {
            String site = CommonMethods.getSite();
            result = callItemService.findCallItemDetailListByDispatchBo(site, dispatchBoList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }


        return R.ok(result);
    }

    /**
     * 查询叫料明细数据
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/findCallItemListPda")
    public R findCallItemListPda(@RequestBody  CallItem callItem){
        List<CallItem> result;
        try {
            callItem.setSite(CommonMethods.getSite());
            if (callItem.getStartFromDate_S() != null) {
                callItem.setStartFromDate(DateUtil.parse(callItem.getStartFromDate_S()));
            }
            if (callItem.getStartToDate_S() != null) {
                callItem.setStartToDate(DateUtil.parse(callItem.getStartToDate_S()));
            }
            result = callItemService.findCallItemListPda(callItem);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok(result);
    }

    @ResponseBody
    @PostMapping("/callItem")
    public R callItem(@RequestBody List<CallItem> callItemList) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            if (callItemList == null || callItemList.size() <= 0) {
                throw BusinessException.build("请至少选择一笔记录");
            }
            callItemService.callItem(site, user, callItemList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok("叫料操作成功");
    }

    /**
     * 生产叫料pda
     * @param
     * @return
     */
    @ResponseBody
    @PostMapping("/callOrConfirmItemPda")
    public R callOrConfirmItemPda(@RequestBody Map<String,Object> map) {
        try {
            callItemService.callOrConfirmItemPda(map);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok("叫料成功");
    }


    @ResponseBody
    @PostMapping("/confirmReceive")
    public R confirmReceive(@RequestBody List<CallItem> callItemList) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            if (callItemList == null || callItemList.size() <= 0) {
                throw BusinessException.build("请至少选择一笔记录");
            }
            callItemService.confirmReceive(site, user, callItemList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok("确认到料操作成功");
    }

    @ResponseBody
    @PostMapping("/cancelCallItem")
    public R cancelCallItem(@RequestBody List<CallItem> callItemNoList) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            if (callItemNoList == null || callItemNoList.size() <= 0) {
                throw BusinessException.build("请至少选择一笔记录");
            }
            callItemService.cancelCallItem(site, user, callItemNoList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok("取消叫料成功");
    }

    @ResponseBody
    @PostMapping("/refreshCallItem")
    public R refreshCallItem(@RequestBody List<CallItem> callItemList) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            if (callItemList == null || callItemList.size() <= 0) {
                throw BusinessException.build("请至少选择一笔记录");
            }
            callItemService.refreshCallItem(site, user, callItemList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok("刷新叫料明细成功");
    }

    /**
     * 刷新叫料数据
     * @param callItemList
     * @return
     */
    @ResponseBody
    @PostMapping("/refreshCallItemPda")
    public R refreshCallItemPda(@RequestBody List<CallItem> callItemList) {
        try {
            callItemService.refreshCallItemPda(callItemList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok("刷新叫料明细成功");
    }

    @ResponseBody
    @PostMapping("/refreshRequiredTime")
    public R refreshRequiredTime(@RequestBody List<String> callItemNoList) {
        try {
            String site = CommonMethods.getSite();
            String user = CommonMethods.getUser();
            if (callItemNoList == null || callItemNoList.size() <= 0) {
                throw BusinessException.build("请至少选择一笔记录");
            }
            callItemService.refreshRequiredTime(site, user, callItemNoList);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok("刷新需求时间成功");
    }




    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    @ResponseBody
    @GetMapping("/findIssueItemList")
    public R findIssueItemList(String workCenter, String resource, String shopOrder, String dispatchNo, String requiredFromDate, String requiredToDate, Boolean matchResource) {
        List<CallItem> result;
        try {
            String site = CommonMethods.getSite();

            //参数拼装
            IssueItemDto issueItemDto = new IssueItemDto();
            issueItemDto.setSite(site);
            issueItemDto.setResource(resource);
            issueItemDto.setWorkCenter(workCenter);
            issueItemDto.setShopOrder(shopOrder);
            issueItemDto.setDispatchNo(dispatchNo);
            issueItemDto.setMatchResource(matchResource);
            if (StringUtil.notBlank(requiredFromDate)) {
                issueItemDto.setRequiredFromDT(LocalDate.parse(requiredFromDate, DateTimeFormatter.ISO_DATE));
            }
            if (StringUtil.notBlank(requiredToDate)) {
                issueItemDto.setRequiredToDT(LocalDate.parse(requiredToDate, DateTimeFormatter.ISO_DATE));
            }

            result = callItemService.findIssueItemList(issueItemDto);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


    /**
     *  pda查询车间需要发料的发料数据
     * @param workCenter
     * @param resource
     * @param requiredFromDate
     * @param requiredToDate
     * @param matchResource
     * @return
     */
    @ResponseBody
    @GetMapping("/findIssueItemListPda")
    public R findIssueItemListPda(String workCenter, String resource, String item,String spec,String texture,String requiredFromDate, String requiredToDate, Boolean matchResource) {
        List<CallItem> result;
        try {
            String site = CommonMethods.getSite();

            //参数拼装
            IssueItemDto issueItemDto = new IssueItemDto();
            issueItemDto.setSite(site);
            issueItemDto.setResource(resource);
            issueItemDto.setWorkCenter(workCenter);
            issueItemDto.setMatchResource(matchResource);
            issueItemDto.setItem(item);
            issueItemDto.setSpec(spec);
            issueItemDto.setTexture(texture);
            if (StringUtil.notBlank(requiredFromDate)) {
                issueItemDto.setRequiredFromDT(LocalDate.parse(requiredFromDate, DateTimeFormatter.ISO_DATE));
            }
            if (StringUtil.notBlank(requiredToDate)) {
                issueItemDto.setRequiredToDT(LocalDate.parse(requiredToDate, DateTimeFormatter.ISO_DATE));
            }

            result = callItemService.findIssueItemListPda(issueItemDto);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


}