package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi.ford;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ford.IFordFetchWebService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.mes.bean.ford.MesFordFetchParameter;
import cn.estsh.i3plus.pojo.mes.bean.ford.MesFordJsaSortInfoWithFetchWeb;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "福特web抓单")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/fetch-web")
@Slf4j
public class MesFordFetchWebController {

    @Autowired
    private IFordFetchWebService fordFetchWebService;

    @PostMapping(value = "/param/save")
    @ApiOperation(value = "保存抓单参数")
    public ResultBean saveParams(@RequestBody MesFordFetchParameter params) {
        try {
            fordFetchWebService.insertParams(params);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("保存成功!");
    }

    @PostMapping(value = "/param/update")
    @ApiOperation(value = "更新抓单参数")
    public ResultBean updateParams(@RequestBody MesFordFetchParameter params) {
        try {
            fordFetchWebService.updateParams(params);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("更新成功!");
    }

    @GetMapping(value = "/param/del/{id}/{userName}")
    @ApiOperation(value = "删除抓单参数")
    public ResultBean updateParams(@PathVariable Long id, @PathVariable String userName) {
        try {
            fordFetchWebService.deleteParams(id, userName);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("删除成功!");
    }

    @GetMapping(value = "/param/query/{id}")
    @ApiOperation(value = "查询抓单参数")
    public ResultBean queryParams(@PathVariable Long id) {
        MesFordFetchParameter parameter;
        try {
            parameter = fordFetchWebService.queryParams(id);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功!").setResultObject(parameter);
    }


    @GetMapping(value = "/param/query/list/{organizeCode}")
    @ApiOperation(value = "查询抓单参数")
    public ResultBean queryParamList(@PathVariable String organizeCode) {
        List<MesFordFetchParameter> mesFordFetchParameters;
        try {
            mesFordFetchParameters = fordFetchWebService.queryParams(organizeCode);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功!").setResultList(mesFordFetchParameters);
    }

    @GetMapping(value = "/param/data/{id}/{organizeCode}")
    @ApiOperation(value = "定时抓单")
    public ResultBean queryData(@PathVariable Long id,@PathVariable String organizeCode) {
        List<MesFordJsaSortInfoWithFetchWeb> mesFordJsaSortInfoWithFetchWebs;
        try {
            mesFordJsaSortInfoWithFetchWebs = fordFetchWebService.doFetchData(id, organizeCode);
        } catch (Exception e) {
            return ResultBean.fail(e.getMessage());
        }
        return ResultBean.success("查询成功").setResultList(mesFordJsaSortInfoWithFetchWebs);
    }


}
