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

import com.chinaitop.depot.intelligent.grainsituation.model.TGasConfig;
import com.chinaitop.depot.intelligent.grainsituation.service.GasConfigService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.unissoft.model.ResponseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qingsong.han
 * @description: 智能控制柜-测气配置(衡水深州)
 * @create 2021-10-15 14:28
 */
@RestController
@RequestMapping(value = "/intelligents/gasConfig")
public class GasConfigController {
    private GasConfigService gasConfigService;

    @Autowired
    public void setGasConfigController(GasConfigService gasConfigService) {
        this.gasConfigService = gasConfigService;
    }

    /**
     * 测气配置列表查询
     *
     * @param pageNum   1
     * @param pageSize  10
     * @param storeCode 仓房编码
     * @return 分页json列表
     */
    @GetMapping(value = "/getList")
    public ResponseEntity<PageInfo<TGasConfig>> getList(Integer pageNum, Integer pageSize, String storeCode) {
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            PageInfo<TGasConfig> pageInfo = gasConfigService.getList(storeCode, null);
            return ResponseEntity.ok(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
    }

    @GetMapping(value = "/getById")
    public ResponseEntity<TGasConfig> getById(String id) {
        try {
            if (StringUtils.isNotBlank(id)) {
                TGasConfig gasConfig = gasConfigService.getById(id);
                return ResponseEntity.ok(gasConfig);
            } else {
                return ResponseEntity.failed("ID数据有误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
    }

    @PostMapping(value = "/save")
    public ResponseEntity save(String gasConfigJson) {
        try {
            if (StringUtils.isNotBlank(gasConfigJson)) {
                if (gasConfigService.save(gasConfigJson) > 0) {
                    return ResponseEntity.ok();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        return ResponseEntity.failed("保存失败");
    }

    @GetMapping(value = "/deleteById")
    public ResponseEntity deleteById(String id) {
        try {
            if (StringUtils.isNotBlank(id)) {
                if (gasConfigService.deleteById(id) > 0) {
                    return ResponseEntity.ok();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        return ResponseEntity.failed("ID数据有误,删除失败!");
    }

}
