package com.prolog.eis.controller.pc;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.prolog.eis.dto.page.*;
import com.prolog.eis.model.container.StationCfg;
import com.prolog.eis.service.page.PcService;
import com.prolog.eis.dto.outbound.WaveBindConditionDto;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.framework.utils.AssertUtils;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * @desc:
 * @author: zhangJie
 * @date: 2023/9/13 14:32
 */

@Slf4j
@RestController
@Validated
@RequestMapping("/api/v1/pc")
@RequiredArgsConstructor
@Api(tags = "pc接口服务")
public class PcController {

    private final PcService pcService;

    @GetMapping(value = "/waveBindCondition")
    @ApiOperation(value = "波次绑定情况", notes = "波次绑定情况")
    public RestMessage<List<WaveBindConditionDto>> waveBindCondition() {
        try {
            return RestMessage.newInstance(true, "200", "查询成功", pcService.waveBindCondition());
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/allotmentStationList")
    @ApiOperation(value = "供包口列表", notes = "供包口列表")
    @InterfaceMethod(key = "allotmentStationList", name = "供包口列表", type = InterfaceType.Receive)
    public RestMessage<List<String>> allotmentStationList() {
        try {
            return RestMessage.newInstance(true, "200", "查询成功", pcService.allotmentStationList());
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/checkAllotment")
    @ApiOperation(value = "供包站台校验", notes = "供包站台校验")
    @InterfaceMethod(key = "checkAllotment", name = "供包站台校验", type = InterfaceType.Receive)
    public RestMessage checkAllotment(@RequestParam("stationNo") String stationNo) {
        try {
            return RestMessage.newInstance(true, "200", "查询成功", pcService.checkAllotment(stationNo));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/cleanAskUser")
    @ApiOperation(value = "清除索取人", notes = "清除索取人")
    @InterfaceMethod(key = "cleanAskUser", name = "清除索取人", type = InterfaceType.Receive)
    public RestMessage cleanAskUser(@RequestParam("code") String code) {
        try {
            pcService.cleanAskUser(code);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/scanCodeGetItem")
    @ApiOperation(value = "供包站台扫码获取物料信息", notes = "供包站台扫码获取物料信息")
    @InterfaceMethod(key = "scanCodeGetItem", name = "供包站台扫码获取物料信息", type = InterfaceType.Receive)
    public RestMessage<List<AllotmentItemInfoDto>> scanCodeGetItem(@RequestParam("code") String code) {
        try {
            return RestMessage.newInstance(true, "200", "获取成功", pcService.scanCodeGetItem(code));
        } catch (Exception e) {
            log.error("供包站台扫码获取物料信息错误:{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "获取失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/allotmentOrder")
    @ApiOperation(value = "供包站台索取商品任务", notes = "供包站台索取商品任务")
    @InterfaceMethod(key = "allotmentOrder", name = "供包站台索取商品任务", type = InterfaceType.Receive)
    public RestMessage<List<AllotmentOrderDto>> allotmentOrder(@RequestBody AllotmentOrderReqDto req) {
        try {
            return RestMessage.newInstance(true, "200", "索取成功", pcService.allotmentOrder(req.getItemId(), req.getLotId(), req.getOutboundId(), req.getOutboundDetailId(), req.getUserId(), req.getCode()));
        } catch (Exception e) {
            log.error("供包站台索取商品任务错误:{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "索取失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/allotmentOrderShow")
    @ApiOperation(value = "供包站台查看商品任务", notes = "供包站台查看商品任务")
    @InterfaceMethod(key = "allotmentOrderShow", name = "供包站台查看商品任务", type = InterfaceType.Receive)
    public RestMessage<List<AllotmentOrderDto>> allotmentOrderShow(@RequestParam("itemId") String itemId) {
        try {
            return RestMessage.newInstance(true, "200", "查询成功", pcService.allotmentOrderShow(itemId));
        } catch (Exception e) {
            log.error("供包站台查看商品任务错误:{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/allotmentConfirmCheck")
    @ApiOperation(value = "复核确认校验药监码", notes = "复核确认校验药监码")
    @InterfaceMethod(key = "allotmentConfirmCheck", name = "复核确认校验药监码", type = InterfaceType.Receive)
    public RestMessage allotmentConfirmCheck(@RequestBody @Valid AllotmentConfirm req) {
        try {
            return RestMessage.newInstance(true, "200", "校验成功", pcService.allotmentConfirmCheck(req));
        } catch (Exception e) {
            log.error("复核校验错误:{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "校验失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/allotmentConfirm")
    @ApiOperation(value = "复核确认", notes = "复核确认")
    @InterfaceMethod(key = "allotmentConfirm", name = "复核确认", type = InterfaceType.Receive)
    public RestMessage allotmentConfirm(@RequestBody @Valid AllotmentConfirm req) {
        try {
            pcService.allotmentConfirm(req);
            return RestMessage.newInstance(true, "200", "复核成功", null);
        } catch (Exception e) {
            log.error("复核确认错误:{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "复核失败" + e.getMessage(), null);
        }
    }


    @PostMapping(value = "/noteConfirm")
    @ApiOperation(value = "票据提交", notes = "票据提交")
    @InterfaceMethod(key = "NoteConfirm", name = "票据提交", type = InterfaceType.Receive)
    public RestMessage noteConfirm(@RequestBody @Valid NoteConfirm req) {
        try {
            pcService.noteConfirm(req);
            return RestMessage.newInstance(true, "200", "查询成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }


    @PostMapping(value = "/mergeContainerList")
    @ApiOperation(value = "合托列表", notes = "合托列表")
    @InterfaceMethod(key = "mergeContainerList", name = "合托列表", type = InterfaceType.Receive)
    public RestMessage<Page<MergeContainerRespDto>> mergeContainerList(@RequestBody MergeContainerReqDto req) {
        try {
            List<MergeContainerRespDto> list = pcService.mergeContainerList(req);
            return RestMessage.newInstance(true, "200", "查询成功", PageUtils.getPage(list));
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/mergeContainerDetail")
    @ApiOperation(value = "合托详情", notes = "合托详情")
    @InterfaceMethod(key = "mergeContainerDetail", name = "合托详情", type = InterfaceType.Receive)
    public RestMessage<List<MergeContainerDetailRespDto>> mergeContainerDetail(@RequestParam("id") String id) {
        try {
            List<MergeContainerDetailRespDto> list = pcService.mergeContainerDetail(id);
            return RestMessage.newInstance(true, "200", "查询成功", list);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/mergeContainerReq")
    @ApiOperation(value = "合托发起", notes = "合托发起")
    @InterfaceMethod(key = "mergeContainerReq", name = "合托发起", type = InterfaceType.Receive)
    public RestMessage mergeContainerReq(@RequestBody JSONObject req) {
        try {
            String ids = req.getStr("ids");
            AssertUtils.isNotTrue(StrUtil.isBlank(ids), "请选择需要合托的容器");
            AssertUtils.isNotTrue(StrUtil.split(ids, ",").length != 2, "请选择2条需要合托的容器");
            pcService.mergeContainerReq(ids);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/pdStationList")
    @ApiOperation(value = "拆零盘点养护站台", notes = "拆零盘点养护站台")
    @InterfaceMethod(key = "pdStationList", name = "拆零盘点养护站台", type = InterfaceType.Receive)
    public RestMessage<List<StationCfg>> pdStationList() {
        try {
            return RestMessage.newInstance(true, "200", "操作成功", pcService.pdStationList());
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @GetMapping(value = "/pdStationCheck")
    @ApiOperation(value = "拆零盘点养护站台校验", notes = "拆零盘点养护站台校验")
    @InterfaceMethod(key = "pdStationCheck", name = "拆零盘点养护站台校验", type = InterfaceType.Receive)
    public RestMessage<List<StationCfg>> pdStationCheck(@RequestParam("stationNo") String stationNo) {
        try {
            AssertUtils.isNotNull(stationNo, "站台编号不能为空");
            pcService.pdStationCheck(stationNo);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/getPdTask")
    @ApiOperation(value = "拆零盘点养护任务领取", notes = "拆零盘点养护任务领取")
    @InterfaceMethod(key = "getPdTask", name = "拆零盘点养护任务领取", type = InterfaceType.Receive)
    public RestMessage<PdTaskGetRespDto> getPdTask(@RequestBody @Valid PdTaskGetReqDto req) {
        try {
            return RestMessage.newInstance(true, "200", "操作成功", pcService.getPdTask(req.getStationNo(), req.getTaskType()));
        } catch (Exception e) {
            log.error("拆零盘点养护任务领取失败：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/pdTaskConfirm")
    @ApiOperation(value = "拆零盘点养护确认", notes = "拆零盘点养护确认")
    @InterfaceMethod(key = "pdTaskConfirm", name = "拆零盘点养护确认", type = InterfaceType.Receive)
    public RestMessage<PdTaskGetRespDto> pdTaskConfirm(@RequestBody @Valid PdTaskConfirmReqDto req) {
        try {
            return RestMessage.newInstance(true, "200", "操作成功", pcService.pdTaskConfirm(req));
        } catch (Exception e) {
            log.error("拆零盘点养护确认失败：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/pdTaskContainerReturn")
    @ApiOperation(value = "拆零盘点养护容器归还", notes = "拆零盘点养护容器归还")
    @InterfaceMethod(key = "pdTaskContainerReturn", name = "拆零盘点养护容器归还", type = InterfaceType.Receive)
    public RestMessage pdTaskContainerReturn(@RequestBody JSONObject req) {
        try {
            String containerNo = req.getStr("containerNo");
            String stationNo = req.getStr("stationNo");
            AssertUtils.isNotTrue(StrUtil.isBlank(containerNo) || StrUtil.isBlank(stationNo), "站台和容器不能为空");
            pcService.pdTaskContainerReturn(containerNo, stationNo);
            return RestMessage.newInstance(true, "200", "操作成功", null);
        } catch (Exception e) {
            log.error("拆零盘点养护容器归还失败：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "操作失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/allotmentTaskRecordList")
    @ApiOperation(value = "供包记录列表", notes = "供包记录列表")
    @InterfaceMethod(key = "allotmentTaskRecordList", name = "供包记录列表", type = InterfaceType.Receive)
    public RestMessage<Page<AllotmentTaskRecordResultDto>> allotmentTaskRecordList(@RequestBody AllotmentTaskRecordReqDto req) {
        try {
            List<AllotmentTaskRecordResultDto> list = pcService.allotmentTaskRecordList(req);
            return RestMessage.newInstance(true, "200", "查询成功", PageUtils.getPage(list));
        } catch (Exception e) {
            log.error("供包记录列表查询失败：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "供包记录列表查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/allotmentTaskRecordDetail")
    @ApiOperation(value = "供包记录详情", notes = "供包记录详情")
    @InterfaceMethod(key = "allotmentTaskRecordDetail", name = "供包记录详情", type = InterfaceType.Receive)
    public RestMessage<List<AllotmentTaskRecordDetailResultDto>> allotmentTaskRecordDetail(@RequestBody JSONObject req) {
        try {
            Integer id = req.getInt("id");
            if (Objects.isNull(id)) {
                return RestMessage.newInstance(false, "500", "id不能为空", null);
            }
            List<AllotmentTaskRecordDetailResultDto> list = pcService.allotmentTaskRecordDetail(id);
            return RestMessage.newInstance(true, "200", "查询成功", list);
        } catch (Exception e) {
            log.error("供包记录详情查询失败：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "供包记录详情查询失败" + e.getMessage(), null);
        }
    }

    @PostMapping(value = "/allotmentTaskRecordSave")
    @ApiOperation(value = "供包记录重新推送", notes = "供包记录重新推送")
    @InterfaceMethod(key = "allotmentTaskRecordSave", name = "供包记录重新推送", type = InterfaceType.Receive)
    public RestMessage allotmentTaskRecordSave(@RequestBody JSONObject req) {
        try {
            Integer id = req.getInt("id");
            if (Objects.isNull(id)) {
                return RestMessage.newInstance(false, "500", "id不能为空", null);
            }
            pcService.allotmentTaskRecordSave(id);
            return RestMessage.newInstance(true, "200", "重新推送成功", null);
        } catch (Exception e) {
            log.error("供包记录重新推送失败：{}", e.getMessage(), e);
            return RestMessage.newInstance(false, "500", "供包记录重新推送失败" + e.getMessage(), null);
        }
    }
}
