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

import com.chinaitop.depot.intelligent.basicdata.service.WareHouseBasicInfoService;
import com.chinaitop.depot.intelligent.grainsituation.service.TemperatureRecordService;
import com.chinaitop.depot.intelligent.utils.RemoteControlUtil;
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.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * @author qingsong.han
 * @description: 查询data_kcgl_kcsw_default和basic_storehouse
 * 获取基本信息
 * @create 2018-12-17 14:01
 */
@RestController
@RequestMapping(value = "/intelligents/wareHouseBasicInfo")
@Api(value = "WareHouseBasicInfoController", description = "仓房基础信息控制类")
@SuppressWarnings("all")
public class WareHouseBasicInfoController {

    @Resource
    private WareHouseBasicInfoService wareHouseBasicInfoService;
    @Autowired
    private TemperatureRecordService temperatureRecordService;
    @Resource
    private RemoteControlUtil remoteControlUtil;

    /**
     * 根据单位查找单位下的所有仓房信息(统一对外接口)
     *
     * @param unitId 单位ID
     * @return
     */
    @RequestMapping(value = "/getWareHouseInfo", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "根据单位查找单位下的所有仓房信息(统一对外接口)", notes = "根据单位查找单位下的所有仓房信息(统一对外接口)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "unitId", value = "单位ID", paramType = "query"),
            @ApiImplicitParam(name = "libraryType", value = "仓房性质(主粮库或代储点)", paramType = "query")
    })
    public Map<String, Object> getStorehouseList(Integer unitId, String libraryType) {
        Map<String, Object> map = wareHouseBasicInfoService.getStorehouseInfo(unitId, libraryType);
        return map;
    }

    /**
     * 库点信息推送到智远
     *
     * @param orgInfoJson              推送库点信息json字符串
     * @param CUD:Create,Update,Delete 标识: 新增("I"),删除("D"),修改("U")
     * @return
     */
    @RequestMapping(value = "/saveGrainpoint", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "推送库点信息", notes = "推送库点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgInfoJson", value = "库点数据", paramType = "form"),
            @ApiImplicitParam(name = "CUD", value = "库点数据:新增(I),删除(D),修改(U)", paramType = "query")
    })
    public ResponseEntity saveGrainpoint(@RequestParam @NotNull String orgInfoJson,
                                         @RequestParam @NotNull String CUD,
                                         @RequestParam @NotNull String orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("orgInfoJson", orgInfoJson);
        requestEntity.add("CUD", CUD);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.saveGrainPoint),
                requestEntity
        );
    }

    /**
     * 接口推送同步仓房信息设置
     *
     * @param basicStorehouseJson 字符串格式json数据
     * @param del                 删除标识(值: D)
     * @return
     */
    @RequestMapping(value = "/synchronizationStoreHouse", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "推送仓房信息", notes = "推送仓房信息数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "basicStorehouseJson", value = "仓房信息数据", paramType = "form"),
            @ApiImplicitParam(name = "del", defaultValue = "D", value = "删除标识", paramType = "query")
    })
    public ResponseEntity synchronizationStoreHouse(@RequestParam @NotNull String basicStorehouseJson,
                                                    @RequestParam @NotNull String del,
                                                    @RequestParam @NotNull String orgId) {
        // 1.获取库端url
        String url = temperatureRecordService.getRemoteUrl(orgId);
        if (StringUtils.isBlank(url))
            return ResponseEntity.failed("库端硬件地址未配置!");
        // 2.封装请求数据
        MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("basicStorehouseJson", basicStorehouseJson);
        requestEntity.add("del", del);
        return remoteControlUtil.remoteScan(
                String.format("%s%s", url, RemoteControlUtil.syncStoreHouse),
                requestEntity
        );
    }

    @RequestMapping(value = "/getStorehouse", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "根据单位和仓房code查询仓房信息(统一对外接口)", notes = "根据单位和仓房code查询仓房信息(统一对外接口)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "单位编码", paramType = "query"),
            @ApiImplicitParam(name = "cfCode", value = "仓房code", paramType = "query"),
            @ApiImplicitParam(name = "libraryType", value = "仓房性质(主粮库或代储点)", paramType = "query")
    })
    public ResponseEntity getStorehouse(Integer orgId, String libraryType, String cfCode) {
        Map<String, Object> map = null;
        try {
            map = wareHouseBasicInfoService.getStorehouse(orgId, libraryType, cfCode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询仓房信息失败");
        }
        return ResponseEntity.ok(map);
    }
}
