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

import com.chinaitop.depot.intelligent.basicdata.model.TLqinfo;
import com.chinaitop.depot.intelligent.basicdata.service.GrainService;
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/grain")
@Api(value = "GrainController", description = "粮情信息控制类")
@SuppressWarnings("all")
public class GrainController {

    @Resource
    private GrainService grainService;

    @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"),
            @ApiImplicitParam(name = "orgId", value = "库点编码", paramType = "query")
    })
    public ResponseEntity<PageInfo<TLqinfo>> getList(Integer pageNum, Integer pageSize, String vCfCode,String orgId) {
        List<TLqinfo> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = grainService.getList(vCfCode,orgId);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TLqinfo> pageInfo = new PageInfo<>(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<TLqinfo> getById(String id) {
        TLqinfo tLqinfo = null;
        if (StringUtils.isNotBlank(id)) {
            tLqinfo = grainService.getById(id);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(tLqinfo);
    }

    @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 {
            grainService.save(grainJson);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        return ResponseEntity.ok();
    }

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

    @RequestMapping(value = "/getBycfCode", method = RequestMethod.GET)
    @ApiOperation(value = "校验是否重复 信息列表", notes = "校验是否重复 信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vdevcode", value = "设备编码", paramType = "query"),
            @ApiImplicitParam(name = "vdevname", value = "设备名称", paramType = "query")
    })
    public ResponseEntity getBycfCode(String vdevcode, String vdevname) {
        boolean bycfCode = grainService.getBycfCode(vdevcode, vdevname);
        return ResponseEntity.ok(bycfCode);
    }

    @RequestMapping(value = "/synchronizationAll", method = RequestMethod.GET)
    @ApiOperation(value = "与智远同步粮油信息基础数据", notes = "与智远同步粮油信息基础数据")
    @ApiImplicitParam(name = "orgId", value = "库房编码", paramType = "query")
    public ResponseEntity synchronizationAll(String orgId) {
        if (StringUtils.isBlank(orgId)){
            return ResponseEntity.ok("库房编码不能为空！");
        }
        String msg = null;
        try {
            msg = grainService.synchronizationAll(orgId);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok("同步失败！");
        }
        return ResponseEntity.ok(msg);
    }
}
