package com.uas.eis.controller;

import com.uas.eis.entity.ErrorMessage;
import com.uas.eis.entity.ErrorMsg;
import com.uas.eis.entity.StepWork.TestReq;
import com.uas.eis.exception.ApiStepWorkException;
import com.uas.eis.exception.SystemException;
import com.uas.eis.sdk.entity.ApiResult;
import com.uas.eis.sdk.entity.StepWorkApiResult;
import com.uas.eis.sdk.resp.ApiResponse;
import com.uas.eis.sdk.resp.StepWorkApiResponse;
import com.uas.eis.service.StepWorkService;
import com.uas.eis.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xiaost
 * @date 2025/8/26 17:51
 * 按照文档：MES工序作业对接接口说明文档 要求编写api 接口请求名字，输入，输出参数
 **/
@RestController
public class StepWorkController {
   @Autowired
   private StepWorkService stepWorkService;

    /**
     * 登录获取token
     * 租户ID	tenantId	String	是	租户ID
     * 账号	username	String	是	账号
     * 密码	password	String	是	md5(password)
     * @return
     */
    @RequestMapping(value="/blade-auth/oauth/token", method = RequestMethod.POST)
    public StepWorkApiResult<Map<String,Object>> getToken(HttpServletRequest request,String username,String password){
        Map<String,Object> map = new HashMap<>();
        if(!StringUtil.hasText(username) || !StringUtil.hasText(password) ){
            return StepWorkApiResponse.failRsp(400,"用户名和密码必填!");
        }
        map.put("accessToken",stepWorkService.login(username, password));
        map.put("tokenType","bearer");
        //refresh_token
        return StepWorkApiResponse.successRsp(map);
    }

    /**
     * 成品测试
     * @param request
     * @param data
     * @return
     */
    @RequestMapping(value="/mes-product/public/station/center/test", method = RequestMethod.POST)
    public StepWorkApiResult<Map<String,Object>> test(HttpServletRequest request, @RequestBody String data){
        if(!StringUtil.hasText(data) || !StringUtil.hasText(data) ){
            throw new ApiStepWorkException(new StepWorkApiResult(ErrorMessage.BUSINESS_DATAILLEGAL));
        }
        return stepWorkService.test(request,data);
    }

    /**
     * 成品过站
     * @param request
     * @param data
     * {
     *  "workOrderNo":"WO20220826000002SHD",  //工单号
     * "stationCode":"GW--001",  //工位编码
     * "routeCode":"22b443b4fa379a546330f4e984cc6ed1", //工序编号
     * "sequenceNumber":"[P]2022083426046[B]" //产品SN
     * }
     * @return
     */
    @RequestMapping(value="/mes-product/public/station/center/over/station", method = RequestMethod.POST)
    public StepWorkApiResult<Map<String,Object>> station(HttpServletRequest request, @RequestBody String data){
        if(!StringUtil.hasText(data) || !StringUtil.hasText(data) ){
            throw new ApiStepWorkException(new StepWorkApiResult(ErrorMessage.BUSINESS_DATAILLEGAL));
        }
        return stepWorkService.station(request,data);
    }
}
