package com.sy.coinage.notify.controller;

import com.sy.coinage.core.annotation.LoginNotRequired;
import com.sy.coinage.notify.req.ErrorEventReq;
import com.sy.coinage.util.printer.RfidInfoDTO;
import com.sy.coinage.util.printer.ZplImgConvertPrinter;
import com.sy.coinage.websocket.WebSocketServer;
import com.sy.coinage.workshop.model.BaseInfo;
import com.sy.coinage.workshop.service.IBaseInfoService;
import com.sy.coinage.workshop.vo.ViewInfoVO;
import com.zebra.sdk.comm.ConnectionException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * -其他系统回调接口
 */
@Api(tags = "其他系统回调接口")
@RestController
@RequestMapping(value = "/notify")
public class EventController {

    @Autowired
    private IBaseInfoService baseInfoService;

    @ApiOperation(value = "接收rfid扫描事件接口")
    @PostMapping("/errorEvent")
    @LoginNotRequired
    public Object errorEvent(@RequestBody @Valid ErrorEventReq req) {
        BaseInfo info = this.baseInfoService.getByRfid(req.getRfid());
        ViewInfoVO vo = new ViewInfoVO();
        BeanUtils.copyProperties(info, vo);
        vo.setStatus(-1);
        vo.setErrType(req.getErrType());
        vo.setInOrOut(req.getInOrOut());
        vo.setOperateTime(req.getOpTime());
        vo.setCreateBy("");
        vo.setChecker("");
        WebSocketServer.sendMessageClient(req.getWareId(), vo);
        return "";
    }

    @ApiOperation(value = "打印")
    @PostMapping(value = "/print")
    @LoginNotRequired
    public Object print(@RequestBody @Valid RfidInfoDTO req) throws ConnectionException {
        ZplImgConvertPrinter.printZpl(req);
        return "";
    }

}
