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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinaitop.depot.intelligent.grainsituation.service.GrainCheckAnalyzeService;
import com.chinaitop.depot.intelligent.supervise.model.TDepotStyle;
import com.chinaitop.depot.intelligent.supervise.model.TDepotStyleExample;
import com.chinaitop.depot.intelligent.supervise.service.depotStyleService;
import com.chinaitop.depot.intelligent.utils.ParameterUtil;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @auther mafy
 * @createtime 2017/10/28 0028
 */
@RestController
@RequestMapping(value = "/depotStyle")
@Api(description = "库区全景类")
public class depotStyleController {

    @Resource
    private depotStyleService Service;

    /**
     * 查找所有的点图信息
     *
     * @return
     */
    @RequestMapping(value = "/getAllPointChart", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "点图信息", notes = "查找所有的点图信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "查看的类型", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "库点编码", paramType = "query")
    })
    public List<Map<String, Object>> getAllPointChart(String type, String orgId) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> newMap = new HashMap<>();
        map.put("type", type);
        map.put("orgId",orgId);
        List<Map<String, Object>> list = null;
        if (ParameterUtil.isequal(type, "1")) {//气体
            list = Service.getQT(map);
            for (int i = 0; i < list.size(); i++) {
                newMap = list.get(i);
                if (newMap.get("imgdata") != null) {
                    newMap.put("imgdata", new String((byte[]) newMap.get("imgdata")));
                    list.set(i, newMap);
                }
            }
        } else if (ParameterUtil.isequal(type, "2")) {//粮情
            list = Service.getGrainsituation(map);
            for (int i = 0; i < list.size(); i++) {
                newMap = list.get(i);
                if (newMap.get("imgdata") != null) {
                    newMap.put("imgdata", new String((byte[]) newMap.get("imgdata")));
                    list.set(i, newMap);
                }
            }
        } else if (ParameterUtil.isequal(type, "3")) {//虫害
            list = Service.getInsect(map);
            for (int i = 0; i < list.size(); i++) {
                newMap = list.get(i);
                if (newMap.get("imgdata") != null) {
                    newMap.put("imgdata", new String((byte[]) newMap.get("imgdata")));
                    list.set(i, newMap);
                }
            }
        }else if (ParameterUtil.isequal(type, "4")) {//报警
            list = Service.getWarning(map);
            for (int i = 0; i < list.size(); i++) {
                newMap = list.get(i);
                if (newMap.get("imgdata") != null) {
                    newMap.put("imgdata", new String((byte[]) newMap.get("imgdata")));
                    list.set(i, newMap);
                }
            }
        }
        return list;
    }

    /**
     * 查找单个作业点位图信息
     *
     * @return
     */
    @RequestMapping(value = "/getSinglePointChart", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "单个作业点位图信息", notes = "查找单个作业点位图信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexid", value = "仓房ID", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "库ID", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "查看的类型", paramType = "query")
    })
    public List<TDepotStyle> getSinglePointChart(String indexid, String type,Integer orgId) {
        TDepotStyleExample example = new TDepotStyleExample();
        TDepotStyleExample.Criteria criteria = example.createCriteria();
        criteria.andIndexidEqualTo(Integer.valueOf(indexid));
        criteria.andTypeEqualTo(type);
        criteria.andOrgIdEqualTo(orgId);
        List<TDepotStyle> list = Service.selectPoint(example);
        return list;
    }

    /**
     * 查找全部作业点位图信息
     *
     * @return
     */
    @RequestMapping(value = "/getAllSinglePointChart", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    @ApiOperation(value = "全部作业点位图信息", notes = "查找全部作业点位图信息")
    @ApiImplicitParams({
    	    @ApiImplicitParam(name = "orgId", value = "库ID", paramType = "query"),
            @ApiImplicitParam(name = "indexid", value = "仓房ID", paramType = "query")
    })
    public List<TDepotStyle> getAllSinglePointChart(String indexid,Integer orgId) {
        TDepotStyleExample example = new TDepotStyleExample();
        TDepotStyleExample.Criteria criteria = example.createCriteria();
        criteria.andIndexidEqualTo(Integer.valueOf(indexid));
        criteria.andTypeLike("3._");
        criteria.andOrgIdEqualTo(orgId);
        List<TDepotStyle> list = Service.selectPoint(example);
        return list;
    }

    /**
     * 新增触发区域点图数据
     *
     * @return
     */
    @RequestMapping(value = "/addAreaPoint", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "新增触发区域点图数据", notes = "新增触发区域点图数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexid", value = "仓房ID", paramType = "form"),
            @ApiImplicitParam(name = "type", value = "查看的类型", paramType = "form"),
            @ApiImplicitParam(name = "styles", value = "点位信息", paramType = "form"),
            @ApiImplicitParam(name = "imgdata", value = "触发区域图片", paramType = "form"),
            @ApiImplicitParam(name = "orgId", value = "组织机构ID", paramType = "form")
    })
    public Map<String, Object> addAreaPoint(String indexid, String type, String styles, String imgdata, Integer orgId) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            TDepotStyle depotStyle = new TDepotStyle();
            depotStyle.setIndexid(Integer.valueOf(indexid));
            depotStyle.setOrgId(orgId);
            depotStyle.setType(type);
            depotStyle.setStyles(styles);
            depotStyle.setImgdata(imgdata.getBytes());
            Service.deletePoint(depotStyle);
            Service.addPoint(depotStyle);
            modelMap.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.put("status", "error");
        }
        return modelMap;
    }

    /**
     * 新增点图数据
     *
     * @return
     */
    @RequestMapping(value = "/addPoint", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    @ApiOperation(value = "新增点图数据", notes = "新增点图数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "indexid", value = "仓房ID", paramType = "form"),
            @ApiImplicitParam(name = "pointData", value = "点位信息", paramType = "form"),
            @ApiImplicitParam(name = "orgId", value = "组织机构ID", paramType = "form")
    })
    public Map<String, Object> addPoint(String indexid, String pointData, Integer orgId) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            TDepotStyle depotStyle = new TDepotStyle();
            depotStyle.setIndexid(Integer.valueOf(indexid));
            depotStyle.setOrgId(orgId);
            JSONArray jsonArray = JSONArray.parseArray(pointData);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
                depotStyle.setType(jsonObject.get("type").toString());
                depotStyle.setStyles(jsonObject.get("styles").toString());
                Service.deletePoint(depotStyle);
                Service.addPoint(depotStyle);
            }
            modelMap.put("status", "success");
        } catch (Exception e) {
            e.printStackTrace();
            modelMap.put("status", "error");
        }
        return modelMap;
    }
}
