package com.prolog.eis.controller.page;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.prolog.eis.dto.outbound.PagePriorityDto;
import com.prolog.eis.dto.page.*;
import com.prolog.eis.service.page.PageService;
import com.prolog.eis.utils.EasyExcelUtil;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.fx.inf.core.InterfaceType;
import com.prolog.fx.inf.core.annotation.InterfaceMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@RestController
@Api(tags = "UI接口服务")
@RequestMapping(value = "api/v1/page")
public class PageController {

    private final PageService pageService;

    @PostMapping(value = "/assemblyState")
    @ApiOperation(value = "自动下发状态", notes = "自动下发状态")
    public RestMessage assemblyPackageState(@RequestBody JSONObject req) {
        try {
            String type = req.getStr("type");
            if (StrUtil.isBlank(type)) {
                return RestMessage.error("查询状态不能为空");
            }
            return RestMessage.doSuccess(pageService.assemblyState(type));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "自动下发状态查询失败" + e.getMessage(), null);
        }
    }
    @PostMapping(value = "/itemList")
    @ApiOperation(value = "商品列表", notes = "商品列表")
    public RestMessage itemList(@RequestBody ItemReqDto req) {
        try {
            return RestMessage.doSuccess(pageService.itemList(req));
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/outboundOrderList")
    @ApiOperation(value = "出库单列表", notes = "出库单列表")
    public RestMessage<Page<OutboundOrderRespDto>> outboundOrderList(@RequestBody PickingOrderReqDto req) {
        try {
            List<OutboundOrderRespDto> list = pageService.outboundOrderList(req);
            return RestMessage.doSuccess(PageUtils.getPage(list));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/outboundOrderDetailList")
    @ApiOperation(value = "出库单详情", notes = "出库单详情")
    public RestMessage<List<OutboundOrderDetailRespDto>> outboundOrderDetailList(@RequestBody JSONObject req) {
        try {
            Integer id = req.getInt("id");
            if (Objects.isNull(id)) {
                return RestMessage.error("出库单id不能为空");
            }
            return RestMessage.doSuccess(pageService.orderDetailList(id));
        } catch (Exception e) {
            e.printStackTrace();
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/updateAssemblyState")
    @ApiOperation(value = "组单开关修改", notes = "组单开关修改")
    public RestMessage<String> updateTaskType(@RequestBody AssemblyStateDto req) throws Exception {
        try {
            pageService.updateTaskType(req);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败", null);
        }
    }


    @PostMapping(value = "/orderDistribute")
    @ApiOperation(value = "订单下发", notes = "出库订单管理--订单下发")
    public RestMessage<String> groupOrderDistribute(@RequestBody List<Integer> ids) {
        try {
            pageService.groupOrderDistribute(ids);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败:"+e.getMessage(), null);
        }
    }


    @PostMapping(value = "/orderStockWarn")
    @ApiOperation(value = "库存预警", notes = "库存预警")
    public RestMessage<Page<OrderStockWarnRespDto>> orderStockWarn(@RequestBody OrderStockWarnReqDto req) {
        try {
            PageUtils.startPage(req.getPageNum(), req.getPageSize());
            List<OrderStockWarnRespDto> list = pageService.orderStockWarn(req);
            return RestMessage.newInstance(true, "200", "查询成功", PageUtils.getPage(list));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败:"+e.getMessage(), null);
        }
    }

    @PostMapping(value = "/stockList")
    @ApiOperation(value = "库存查询", notes = "库存查询")
    public RestMessage<Page<StockRespDto>> stockList(@RequestBody StockReqDto req) {
        try {
            PageUtils.startPage(req.getPageNum(), req.getPageSize());
            List<StockRespDto> list = pageService.stockList(req);
            return RestMessage.newInstance(true, "200", "查询成功", PageUtils.getPage(list));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败:"+e.getMessage(), null);
        }
    }

    @GetMapping(value = "/stockExport")
    @ApiOperation(value = "库存导出", notes = "库存导出")
    public void stockList(HttpServletResponse response, HttpServletRequest request) {
        try {
            String ids = request.getParameter("ids");
            StockReqDto req = StockReqDto.builder().build();
            if (StrUtil.isNotBlank(ids)) {
                req.setIds( Arrays.asList(ids.split(",")) .stream().map(Integer::parseInt).collect(Collectors.toList()));
            }
            List<StockRespDto> result = pageService.stockList(req);
            EasyExcelUtil.writeEasyExcel(response, StockRespDto.class, result, "库存");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @PostMapping(value = "/inboundOrderList")
    @ApiOperation(value = "入库单列表", notes = "入库单列表")
    public RestMessage<Page<InboundOrderRespDto>> inboundOrderList(@RequestBody InboundOrderReqDto req) {
        try {
            List<InboundOrderRespDto> list = pageService.inboundOrderList(req);
            return RestMessage.doSuccess(PageUtils.getPage(list));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }
    @GetMapping(value = "/inboundOrderDetail")
    @ApiOperation(value = "入库单详情", notes = "入库单详情")
    public RestMessage<List<InboundOrderDetailRespDto>> inboundOrderDetail(@RequestParam("id") Integer id) {
        try {
            List<InboundOrderDetailRespDto> list = pageService.inboundOrderDetail(id);
            return RestMessage.doSuccess(list);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }
    /**
     * @param priorityList
     * @return
     */
    @ApiOperation(value = "更新优先级", notes = "更新优先级")
    @RequestMapping(method = RequestMethod.POST, value = "/priority")
    @InterfaceMethod(key = "更新优先级", name = "更新优先级", type = InterfaceType.Receive)
    public RestMessage updatePriority(@RequestBody @Valid List<PagePriorityDto> priorityList) {
        try {
             pageService.updatePriority(priorityList);
            return RestMessage.success("操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "库存锁定界面查询", notes = "库存锁定界面查询")
    @RequestMapping(method = RequestMethod.POST, value = "/lockStockPage")
    @InterfaceMethod(key = "库存锁定界面查询", name = "库存锁定界面查询", type = InterfaceType.Receive)
    public RestMessage<Page<LockStockRespDto>> lockStockPage(@RequestBody LockStockReqDto req) {
        try {
            Page<LockStockRespDto> result = pageService.lockStockListPage(req);
            return RestMessage.success("操作成功", result);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "库存锁定详情查询", notes = "库存锁定详情查询")
    @RequestMapping(method = RequestMethod.POST, value = "/lockContainerStockDetail")
    @InterfaceMethod(key = "库存锁定详情查询", name = "库存锁定详情查询", type = InterfaceType.Receive)
    public RestMessage<LockStockRespDto> lockContainerStockDetail(@RequestParam("containerNo") String containerNo) {
        try {
            LockStockRespDto result = pageService.lockContainerStockDetail(containerNo);
            return RestMessage.success("操作成功", result);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "容器库存全部锁定", notes = "容器库存全部锁定")
    @RequestMapping(method = RequestMethod.POST, value = "/containerListLock")
    @InterfaceMethod(key = "容器库存全部锁定", name = "容器库存全部锁定", type = InterfaceType.Receive)
    public RestMessage<Page<LockStockRespDto>> containerListLock(@RequestBody List<String> containerSubNos) {
        try {
            pageService.lockContainerStocks(containerSubNos);
            return RestMessage.success("操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "容器库存部分锁定", notes = "容器库存部分锁定")
    @RequestMapping(method = RequestMethod.POST, value = "/containerLock")
    @InterfaceMethod(key = "容器库存部分锁定", name = "容器库存部分锁定", type = InterfaceType.Receive)
    public RestMessage<Page<LockStockRespDto>> containerLock(@RequestParam("containerNo") String containerNo,@RequestParam("lockQty") BigDecimal lockQty) {
        try {
            pageService.lockContainerStock(containerNo,lockQty);
            return RestMessage.success("操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @ApiOperation(value = "容器库存解锁", notes = "容器库存解锁")
    @RequestMapping(method = RequestMethod.POST, value = "/containerUnLock")
    @InterfaceMethod(key = "容器库存解锁", name = "容器库存解锁", type = InterfaceType.Receive)
    public RestMessage<Page<LockStockRespDto>> containerLock(@RequestBody List<String> containerSubNos) {
        try {
            pageService.unLockContainerStock(containerSubNos);
            return RestMessage.success("操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }


    /*@PostMapping(value = "/orderNum")
    @ApiOperation(value = "出库单总数量/待领取数量/已完成数量", notes = "出库单总数量/待领取数量/已完成数量")
    public RestMessage<DemonstrateTaskDto> orderNum() {
        try {
            return RestMessage.doSuccess(pageService.orderNum());
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }


    @PostMapping(value = "/pickingOrderList")
    @ApiOperation(value = "组单列表", notes = "组单列表")
    public RestMessage<Page<PickingOrderRespDto>> pickingOrderList(@RequestBody PickingOrderReqDto req) {
        try {
            PageUtils.startPage(req.getPageNum(), req.getPageSize());
            List<PickingOrderRespDto> list = pageService.pickingOrderList(req);
            return RestMessage.doSuccess(PageUtils.getPage(list));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }*/



   /* @PostMapping(value = "/pickOutboundOrderList")
    @ApiOperation(value = "组任务包出库单列表", notes = "组单出库单列表")
    public RestMessage<List<PickingOutboundOrderRespDto>> pickOutboundOrderList(@RequestBody JSONObject req) {
        try {
            return RestMessage.doSuccess(pageService.pickOutboundOrderList(req.getInt("pickId")));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }
*/
   /* @PostMapping(value = "/pickOutboundOrderDetailList")
    @ApiOperation(value = "组任务包出库单详情", notes = "组单出库单详情")
    public RestMessage<List<PickOutboundOrderDetailRespDto>> pickOutboundOrderDetailList(@Validated @RequestBody OutboundOrderDetailReqDto req) {
        try {
            return RestMessage.doSuccess(pageService.pickOutboundOrderDetailList(req));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }*/

    /*@PostMapping(value = "/pickOrderLock")
    @ApiOperation(value = "组任务包暂停继续", notes = "组单暂停继续")
    public RestMessage pickOrderLock(@Validated @RequestBody PickOrderLockReqDto req) {
        try {
            pageService.pickOrderLock(req);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }*/

    /*@PostMapping(value = "/pickOrderActive")
    @ApiOperation(value = "组任务包激活领取", notes = "组单激活领取")
    public RestMessage pickOrderActive(@RequestBody List<Integer> ids) {
        try {
            pageService.pickOrderActive(ids);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }*/

    /*@PostMapping(value = "/outboundOrderCancel")
    @ApiOperation(value = "组任务包出库单取消", notes = "出库单取消")
    public RestMessage outboundOrderCancel(@RequestBody List<Integer> ids) {
        try {
            pageService.outboundOrderCancel(ids);
            return RestMessage.doSuccess(null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }*/



}
