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

import com.chinaitop.depot.intelligent.basicdata.model.TWarningThreshold;
import com.chinaitop.depot.intelligent.basicdata.model.TWarningThresholdList;
import com.chinaitop.depot.intelligent.basicdata.service.WarningThresholdService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
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.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/intelligents/WarningThreshold")
@Api(value = "WarningThresholdController", description = "预警阈值控制类")
@SuppressWarnings("all")
public class WarningThresholdController {

    @Autowired
    private WarningThresholdService warningThresholdService;

    @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<TWarningThresholdList>> getList(Integer pageNum, Integer pageSize, String vCfCode) {
        List<TWarningThresholdList> list = null;
        try {
            if (null != pageNum && null != pageSize) {
                PageHelper.startPage(pageNum, pageSize);
            }
            list = warningThresholdService.getList(vCfCode);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        PageInfo<TWarningThresholdList> pageInfo = new PageInfo<TWarningThresholdList>(list);
        return ResponseEntity.ok(pageInfo);
    }

    @RequestMapping(value = "/getByCfCode", method = RequestMethod.GET)
    @ApiOperation(value = "根据仓房code查询 预警阈值", notes = "根据仓房code查询 预警阈值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "vCfCode", value = "仓库名称", paramType = "query")
    })
    public ResponseEntity<List<TWarningThreshold>> getByCfCode(String vCfCode, Integer wState) {
        List<TWarningThreshold> bycfCodeList = null;
        try {
            bycfCodeList = warningThresholdService.getByCfCode(vCfCode, wState);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseEntity.failed(e.getMessage());
        }
        return ResponseEntity.ok(bycfCodeList);
    }

    @RequestMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "数据更新与添加", notes = "数据更新与添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "thresholdListJson", value = "表数据", paramType = "form"),
            @ApiImplicitParam(name = "deleteRowId", value = "删除一条数据id", paramType = "query")
    })
    public ResponseEntity save(String thresholdListJson, String deleteRowId) {
        // deleteRowId 为删除一条数据,再保存时才确定删除.
        if (StringUtils.isNotBlank(deleteRowId)) {
            // 调用删除方法
            this.deleteById(deleteRowId);
        }
        try {
            // JSON字符串转对象
            if (thresholdListJson != null) {
                ObjectMapper mapper = new ObjectMapper();
                mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
                List<TWarningThreshold> thresholdList = mapper.readValue(thresholdListJson, new TypeReference<List<TWarningThreshold>>() {
                });
                warningThresholdService.saveAndUpdate(thresholdList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed(e.getMessage());
        }
        return ResponseEntity.ok();
    }

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

    /**
     * 根据id删除列表并删除详情数据
     *
     * @param id
     * @param vCfCode
     * @return
     */
    @RequestMapping(value = "/deleteThreshold", method = RequestMethod.POST)
    @ApiOperation(value = "根据id删除列表及详情多条信息", notes = "根据id删除列表及详情多条信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "列表id", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓房编码", paramType = "query"),
            @ApiImplicitParam(name = "thresholdSetListJson", value = "json对象", paramType = "query")
    })
    public ResponseEntity deleteThreshold(String id, String vCfCode, String thresholdSetListJson) {
        if (StringUtils.isNotBlank(id) && StringUtils.isNotBlank(vCfCode)) {
            int count = warningThresholdService.deleteThreshold(id, vCfCode, thresholdSetListJson);
            if (count > 0) {
                ResponseEntity.ok();
                return ResponseEntity.ok();
            } else {
                return ResponseEntity.failed("删除失败!");
            }
        }
        return ResponseEntity.failed("ID或仓房编码有误!");
    }

    @RequestMapping(value = "/getByOrgIdAndCfCode", method = RequestMethod.GET)
    @ApiOperation(value = "根据仓房code和库点code查询 测温预警阈值", notes = "根据仓房code和库点code查询 测温预警阈值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "库点编码", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓库编码", paramType = "query")
    })
    public ResponseEntity getByOrgIdAndCfCode(String orgId, String vCfCode) {
        HashMap<String, String> byOrgIdAndCfCode = null;
        try {
            byOrgIdAndCfCode = warningThresholdService.getByOrgIdAndCfCode(orgId, vCfCode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询赋值失败");
        }
        return ResponseEntity.ok(byOrgIdAndCfCode);
    }

    /**
     * 根据orgId或仓房code查询测温预警,报警阀值.
     * @param orgId
     * @param vCfCode
     * @return
     */
    @RequestMapping(value = "/getValueByOrgIdOrCfCode", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "根据仓房code和库点code查询 测温预警阈值", notes = "根据仓房code和库点code查询 测温预警阈值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orgId", value = "库点编码", paramType = "query"),
            @ApiImplicitParam(name = "vCfCode", value = "仓库编码", paramType = "query")
    })
    public ResponseEntity getValueByOrgIdOrCfCode(String orgId, String vCfCode) {
        Map<String, Map<String, Object>> listInObjectStorCode = null;
        try {
            listInObjectStorCode = warningThresholdService.getValueByOrgIdOrCfCode(orgId, vCfCode);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("查询赋值失败");
        }
        return ResponseEntity.ok(listInObjectStorCode);
    }

}
