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

import com.chinaitop.depot.intelligent.ventilation.model.TCtldevinfo;
import com.chinaitop.depot.intelligent.ventilation.service.AerationParameterService;
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.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/intelligents/aerationParameter")
@Api(value = "AerationParameterController", description = "通风参数值设置")
public class AerationParameterController {

    @Autowired
    private AerationParameterService aerationParameterService;

   
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    @ApiOperation(value = "查询 通风参数值列表", notes = "查询 通风参数值列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "库点编码", paramType = "query"),
            @ApiImplicitParam(name = "vcfcode", value = "仓库名称", paramType = "query")
    })
   public ResponseEntity<PageInfo<TCtldevinfo>> getList(Integer pageNum, Integer pageSize, String orgId, String vcfcode) {
        List<TCtldevinfo> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = aerationParameterService.getList(orgId,vcfcode);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TCtldevinfo> pageInfo = new PageInfo<TCtldevinfo>(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<TCtldevinfo> getById(String id) {
    	TCtldevinfo tCtldevinfo = null;
        if (StringUtils.isNotBlank(id)) {
        	tCtldevinfo = aerationParameterService.getById(id);
        } else {
            return ResponseEntity.failed("ID数据有误!");
        }
        return ResponseEntity.ok(tCtldevinfo);
    }

    @RequestMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "数据更新与添加", notes = "数据更新与添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aerationParameterJson", value = "表数据json串", paramType = "form")
    })
    public ResponseEntity save(String aerationParameterJson,Integer orgId) {
        try {
        	aerationParameterService.save(aerationParameterJson,orgId);
        } 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 = "aerationParamJson", value = "通风参数json字符串", paramType = "query")
    })
    public ResponseEntity deleteById(String id, String aerationParamJson) {
        if (StringUtils.isNotBlank(id)) {
            int count = aerationParameterService.deleteById(id, aerationParamJson);
            if (count > 0) {
                return ResponseEntity.ok();
            } else {
                return ResponseEntity.failed("删除失败!");
            }
        }
        return ResponseEntity.failed("ID数据有误!");
    }

    
    /**
     * 根据仓房编号 和设备类型获取设备名称
     * @param getBycf
     * @return
     */
    @RequestMapping(value = "/getBycf", method = RequestMethod.POST)
    @ApiOperation(value = "根据id查询信息", notes = "根据id查询信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vcfcode", value = "仓房code", paramType = "query"),
            @ApiImplicitParam(name = "vdevkindcode", value = "设备类型", paramType = "query")
    })
    public ResponseEntity<List<TCtldevinfo>> getBycf(String vcfcode,String vdevkindcode) {
    	List<TCtldevinfo> list = null;
    	Map<String, Object> modelMap = new HashMap<>();
    	if(StringUtils.isNotBlank(vdevkindcode)){
    		modelMap.put("vdevkindcode", vdevkindcode);
    	}
    	if(StringUtils.isNotBlank(vcfcode)){
    		modelMap.put("vcfcode", vcfcode);
    	}
        list = aerationParameterService.getBycf(modelMap);
        return ResponseEntity.ok(list);
    }

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

    @RequestMapping(value = "/getMap", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "获取通风类型", notes = "获取通风类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cfCode", value = "根据仓房获取设备类型", paramType = "query")
    })
    public ResponseEntity<Map<String, String>> getMap(String cfCode) {
        Map<String, String> upOrDownList = aerationParameterService.getMap(cfCode);
        return ResponseEntity.ok(upOrDownList);
    }
}
