package com.prolog.eis.controller.pda;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.prolog.eis.constants.cfg.LocationCfgConstants;
import com.prolog.eis.dto.contaienr.ContainerCleanDto;
import com.prolog.eis.dto.contaienr.EmptyContainerInboundDto;
import com.prolog.eis.dto.contaienr.EmptyContainerOutboundDto;
import com.prolog.eis.dto.inbound.InboundCarryTaskDto;
import com.prolog.eis.dto.outbound.SpecifyContainerCarryDto;
import com.prolog.eis.dto.page.PdaStockRespDto;
import com.prolog.eis.dto.page.PickingConfirmReqDto;
import com.prolog.eis.dto.page.PickingDetailDto;
import com.prolog.eis.dto.page.PickingListDto;
import com.prolog.eis.model.container.LocationCfg;
import com.prolog.eis.model.container.StationCfg;
import com.prolog.eis.service.container.ContainerInfoService;
import com.prolog.eis.service.container.ContainerTaskService;
import com.prolog.eis.service.container.LocationCfgService;
import com.prolog.eis.service.container.StationCfgService;
import com.prolog.eis.service.inbound.BizInboundTaskService;
import com.prolog.eis.service.outbound.BizOutboundTaskService;
import com.prolog.eis.service.page.PdaService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.AssertUtils;
import com.prolog.framework.utils.MapUtils;
import com.prolog.framework.utils.StringUtils;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/api/v1/pda")
@RequiredArgsConstructor
@Api(tags = "PDA接口服务")
public class PdaController {

    @Autowired
    private PdaService pdaService;
    @Autowired
    private BizOutboundTaskService outboundTaskService;
    @Autowired
    private BizInboundTaskService inboundTaskService;
    @Autowired
    private StationCfgService stationCfgService;
    @Autowired
    private ContainerInfoService containerInfoService;
    @Autowired
    private ContainerTaskService containerTaskService;
    @Autowired
    private LocationCfgService locationCfgService;

    @ApiOperation(value = "指定托盘搬运", notes = "指定托盘搬运")
    @RequestMapping(method = RequestMethod.POST, value = "/specifyContainerCarry")
    @InterfaceMethod(key = "specifyContainerCarry", name = "指定托盘搬运(WMS->EIS)", type = InterfaceType.Receive)
    public RestMessage specifyContainerCarry(@Valid @RequestBody SpecifyContainerCarryDto specifyContainerCarryDto) {
        try {
            outboundTaskService.specifyContainerCarry(specifyContainerCarryDto);
            return RestMessage.success("请求成功", specifyContainerCarryDto);
        } catch (Exception e) {
            log.error("指定托盘搬运异常:{}", e);
            return RestMessage.error("请求异常" + e.getMessage());
        }

    }

    @ApiOperation(value = "点位释放", notes = "点位释放")
    @RequestMapping(method = RequestMethod.POST, value = "/cleanContainerItem")
    @InterfaceMethod(key = "cleanContainerItem", name = "点位释放(WMS->EIS)", type = InterfaceType.Receive)
    public RestMessage cleanContainerItem(@Valid @RequestBody ContainerCleanDto containerCleanDto) {
        try {
            pdaService.cleanContainerItem(containerCleanDto);
            return RestMessage.success("请求成功", null);
        } catch (Exception e) {
            log.error("点位释放异常:{}", e);
            return RestMessage.error("请求异常" + e.getMessage());
        }

    }

    @ApiOperation(value = "空托入库", notes = "空托入库")
    @RequestMapping(method = RequestMethod.POST, value = "/emptyContainerInbound")
    @InterfaceMethod(key = "emptyContainerInbound", name = "RF空托入库", type = InterfaceType.Receive)
    public RestMessage emptyContainerInbound(@Valid @RequestBody EmptyContainerInboundDto emptyContainerInboundDto) {
        try {
            inboundTaskService.emptyContainerInbound(emptyContainerInboundDto);
            return RestMessage.newInstance(true, "请求成功");
        } catch (Exception e) {
            log.error("空托入库异常：{}", e);
            return RestMessage.newInstance(false, "请求异常" + e.getMessage());
        }
    }

    @PostMapping(value = "/emptyContainerOutBound")
    @ApiOperation(value = "空托出库", notes = "空托出库")
    public RestMessage<String> emptyContainerOutBound(
            @Valid @RequestBody EmptyContainerOutboundDto emptyContainerOutDto) {
        try {
            outboundTaskService.emptyContainerOutBound(emptyContainerOutDto.getOutBoundNum(), emptyContainerOutDto.getTargetArea(),1);
            return RestMessage.newInstance(true, "200", "操作成功", "");
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/emptyContainerOutBoundArea")
    @ApiOperation(value = "空托出库目标区域列表", notes = "空托出库目标区域列表")
    public RestMessage<List<LocationCfg>> emptyContainerOutBoundArea() {
        try {
            return RestMessage.newInstance(true, "200", "操作成功", locationCfgService.findByMap(MapUtils.put("type", LocationCfgConstants.JHQ).getMap()));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/emptyContainerNum")
    @ApiOperation(value = "空托数量查询", notes = "空托数量查询")
    public RestMessage<Long> emptyContainerNum() {
        try {
            return RestMessage.newInstance(true, "200", "操作成功", outboundTaskService.emptyContainerNum());
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "站台查询", notes = "站台查询")
    @PostMapping(value = "/queryStation")
    public RestMessage<List<StationCfg>> queryStation(@RequestBody StationCfg stationCfg) {
        List<StationCfg> stationCfgs = null;
        try {
            stationCfgs = stationCfgService.findByCriteria(Criteria.forClass(StationCfg.class));
            return RestMessage.newInstance(true, "请求成功", stationCfgs);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "200", "查询发生异常!", null);
        }
    }

    @PostMapping(value = "/updateStation")
    @ApiOperation(value = "站台编辑", notes = "站台编辑")
    public RestMessage<String> updateStation(@RequestBody StationCfg stationCfg) {
        try {
            stationCfgService.updateStation(stationCfg);
            return RestMessage.newInstance(true, "修改成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", "修改失败" + e.getMessage(), null);
        }
    }
    @PostMapping(value = "/pickingList")
    @ApiOperation(value = "拣选列表", notes = "拣选列表")
    public RestMessage<List<PickingListDto>> pickingList(@RequestBody JSONObject req) {
        try {
            String containerNo = req.getStr("containerNo");
            if (StrUtil.isBlank(containerNo)) {
                return RestMessage.error("容器编号不能为空");
            }
            return RestMessage.newInstance(true, "查询成功", containerTaskService.pickingList(containerNo));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/pickingDetail")
    @ApiOperation(value = "拣选详情", notes = "拣选详情")
    public RestMessage<List<PickingDetailDto>> pickingDetail(@RequestBody JSONObject req) {
        try {
            Integer id = req.getInt("id");
            if (Objects.isNull(id)) {
                return RestMessage.error("任务id不能为空");
            }
            return RestMessage.newInstance(true, "查询成功",  containerTaskService.pickingDetail(id));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/pickingConfirm")
    @ApiOperation(value = "拣选确认", notes = "拣选确认")
    public RestMessage pickingConfirm(@RequestBody @Valid PickingConfirmReqDto req) {
        try {
            containerTaskService.pickingConfirm(req);
            return RestMessage.newInstance(true, "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", "操作失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/stockList")
    @ApiOperation(value = "容器库存", notes = "容器库存")
    public RestMessage<List<PdaStockRespDto>> stockList(@RequestBody JSONObject req) {
        try {
            String containerNo = req.getStr("containerNo");
            if (StrUtil.isBlank(containerNo)) {
                return RestMessage.error("容器编号不能为空");
            }
            List<PdaStockRespDto> list = containerInfoService.stockList(containerNo);
            return RestMessage.newInstance(true, "查询成功", list);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "202", "查询失败" + e.getMessage(), null);
        }
    }





    @PostMapping(value = "/updateDirectionUrl")
    @ApiOperation(value = "出入口方向切换", notes = "出入口方向切换")
    @InterfaceMethod(key = "updateDirectionUrl", name = "出入口方向切换", type = InterfaceType.Receive)
    public RestMessage<Long> updateDirectionUrl(@RequestBody String json) {
        try {
            com.alibaba.fastjson.JSONObject typeJson = com.alibaba.fastjson.JSONObject.parseObject(json);
            String type = typeJson.getString("id");
            AssertUtils.isNotTrue(StringUtils.isEmpty(type),"参数不能为空");
            locationCfgService.updateDirectionUrl(type);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/wmsCarryTaskScan")
    @ApiOperation(value = "rf搬运任务创建扫码", notes = "rf搬运任务创建扫码")
    @InterfaceMethod(key = "wmsCarryTaskScan", name = "rf搬运任务创建扫码", type = InterfaceType.Receive)
    public RestMessage<InboundCarryTaskDto> wmsCarryTaskScan(@RequestParam("containerNo") String containerNo,@RequestParam("locationNo") String locationNo) {
        try {
            if (StrUtil.isBlank(containerNo)) {
                return RestMessage.newInstance(false, "500", "容器号不能为空", null);
            }
            return RestMessage.newInstance(true, "200", "查询成功", inboundTaskService.wmsCarryTaskScan(containerNo, locationNo));
        } catch (Exception e) {
            log.error("rf搬运任务创建扫码错误：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/wmsCarryTaskTargetList")
    @ApiOperation(value = "rf搬运任务任务目标区域列表", notes = "rf搬运任务任务目标区域列表")
    @InterfaceMethod(key = "wmsCarryTaskTargetList", name = "rf搬运任务任务目标区域列表", type = InterfaceType.Receive)
    public RestMessage<List<Map<String, Object>>> wmsCarryTaskTargetList(@RequestParam("type") Integer type) {
        try {
            return RestMessage.newInstance(true, "200", "查询成功", pdaService.wmsCarryTaskTargetList(type));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/wmsCarryTaskConfirm")
    @ApiOperation(value = "rf搬运任务创建确认", notes = "rf搬运任务创建确认")
    @InterfaceMethod(key = "wmsCarryTaskConfirm", name = "rf搬运任务创建确认", type = InterfaceType.Receive)
    public RestMessage wmsCarryTaskConfirm(@RequestBody InboundCarryTaskDto req) {
        try {
            inboundTaskService.inboundTaskConfirm(req);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            log.error("rf搬运任务创建确认错误：{}",e);
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }


}
