package com.chinaitop.depot.intelligent.basicdata.controller;

import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfo;
import com.chinaitop.depot.intelligent.basicdata.service.GaseousService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 多通道气体液位信息设置
 */
@RestController
@RequestMapping(value = "/intelligents/gaseous")
@Api(value = "GaseousController", description = "多通道气体设备控制类")
@SuppressWarnings("all")
public class GaseousController {

    @Resource
    private GaseousService gaseousService;

    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "查询多通道气体设备信息", notes = "查询多通道气体设备信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓库名称", paramType = "query")
    })
    public ResponseEntity<PageInfo<TDcsqtinfo>> getList(Integer pageNum, Integer pageSize, String vCfCode) {
        List<TDcsqtinfo> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = gaseousService.getList(vCfCode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("多通道气体列表加载失败");
        }
        PageInfo<TDcsqtinfo> pageInfo = new PageInfo<TDcsqtinfo>(list);
        return ResponseEntity.ok(pageInfo);
    }

    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    @ApiOperation(value = "根据多通道气体id查询", notes = "根据多通道气体id查询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "多通道气体id", paramType = "query")
    })
    public ResponseEntity<TDcsqtinfo> getById(String id) {
        TDcsqtinfo tDcsqtinfo = null;
        if (StringUtils.isNotBlank(id)) {
            tDcsqtinfo = gaseousService.getById(id);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(tDcsqtinfo);
    }

    @RequestMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "数据更新与添加", notes = "数据更新与添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grainJson", value = "表数据json串", paramType = "form")
    })
    public ResponseEntity save(String grainJson) {
        try {
            gaseousService.save(grainJson);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("保存失败");
        }
        return ResponseEntity.ok();
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query"),
            @ApiImplicitParam(name = "dcsQtInfoInfoJson", value = "json字符串", paramType = "query")
    })
    public ResponseEntity deleteById(String id, String dcsQtInfoInfoJson) {
        if (StringUtils.isNotBlank(id)) {
            int count = gaseousService.deleteById(id, dcsQtInfoInfoJson);
            if (count > 0) {
                return ResponseEntity.ok();
            } else {
                return ResponseEntity.failed("删除失败!");
            }
        }
        return ResponseEntity.failed("ID数据有误!");
    }

    /**
     * 硬件厂商同步基础数据
     *
     * @param orgId
     * @return
     */
    @RequestMapping(value = "/synchronizationAll", method = RequestMethod.GET)
    @ApiOperation(value = "同步多通道气体基础数据", notes = "对接多通道气体设置基础数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "组织机构编码", paramType = "query"),
            @ApiImplicitParam(name = "vUpdatePerson", value = "操作人", paramType = "query")
    })
    public ResponseEntity synchronizationAll(String orgId, String vUpdatePerson) {
        if (StringUtils.isBlank(orgId)) {
            return ResponseEntity.ok("库房编码不能为空！");
        }
        String msg = null;
        try {
            msg = gaseousService.synchronizationAll(orgId, vUpdatePerson);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok("同步失败！");
        }
        return ResponseEntity.ok(msg);
    }

}
