package com.steerinfo.ems.dispatchenergylog.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.steerinfo.ems.dispatchenergylog.model.DispatchEnergyLog;
import com.steerinfo.ems.dispatchenergylog.service.IDispatchEnergyLogService;
import com.steerinfo.ems.dispatchenergylogact.model.DispatchEnergyLogAct;
import com.steerinfo.ems.dispatchenergylogact.service.IDispatchEnergyLogActService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.math.BigDecimal;

/**
 * DispatchEnergyLog RESTful接口：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-08-08 11:44
 * 类描述
 * 修订历史：
 * 日期：2020-08-08
 * 作者：generator
 * 参考：
 * 描述：DispatchEnergyLog RESTful接口
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@RestController
@RequestMapping("/${api.version}/dispatchenergylogs")
public class DispatchEnergyLogController extends BaseRESTfulController {

    @Autowired
    IDispatchEnergyLogService dispatchEnergyLogService;
    
    @Autowired
    IDispatchEnergyLogActService dispatchEnergyLogActService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("dispatchenergylog:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<DispatchEnergyLog> list = dispatchEnergyLogService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="获取列表", notes="分页模糊查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("dispatchenergylog:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<DispatchEnergyLog> list = dispatchEnergyLogService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据DispatchEnergyLog对象创建")
    @ApiImplicitParam(name = "dispatchEnergyLog", value = "详细实体dispatchEnergyLog", required = true, dataType = "DispatchEnergyLog")
    //@RequiresPermissions("dispatchenergylog:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@RequestBody Map<String,Object> maps){
    	if(maps.get("clock") == null){
    		return failed(null, "请传入日志日期");
    	}
    	String clock = maps.get("clock").toString();
    	String dLog = maps.get("dLog") == null ? "" : maps.get("dLog").toString(); // 重要记事
    	String orderLog = maps.get("orderLog") == null ? "" : maps.get("orderLog").toString(); // 上级命令
    	
    	DispatchEnergyLog t = dispatchEnergyLogService.getById(clock);
    	DispatchEnergyLog model = new DispatchEnergyLog();
    	model.setClock(clock);
    	model.setDlog(dLog);
    	model.setOrderLog(orderLog);
    	if(t!=null){
    		dispatchEnergyLogService.modify(model);
    	}else{
    		dispatchEnergyLogService.add(model);
    	}
    	// 日志详情
    	JSONArray ja = new JSONArray();
    	if(maps.get("data")!=null){
    		ja = (JSONArray)maps.get("data");
    		HashMap<String, Object> params = new HashMap<>();
        	params.put("clock", clock);
        	dispatchEnergyLogActService.deleteByParams(params);
        	if (ja != null && !ja.isEmpty()) {
        		for (int i = 0; i < ja.size(); i++) {
        			JSONObject jo = (JSONObject) ja.get(i);
        			DispatchEnergyLogAct da = new DispatchEnergyLogAct();
        			da.setClock(clock);
        			da.setTim(jo.getString("tim"));
        			da.setActLog(jo.getString("actLog"));
        			da.setDataLog(jo.getString("dataLog"));
        			dispatchEnergyLogActService.add(da);
        		}
        	}
    	}
        return success();
    }

    @ApiOperation(value="获取详细信息", notes="根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("dispatchenergylog:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id){
        DispatchEnergyLog dispatchEnergyLog = dispatchEnergyLogService.getById(id);
        String orderLog = "";
        String dLog = "";
        List<DispatchEnergyLogAct> list = new ArrayList<DispatchEnergyLogAct>();
        if(dispatchEnergyLog !=null) {
        	dLog = dispatchEnergyLog.getDlog();
        	orderLog = dispatchEnergyLog.getOrderLog();
        	HashMap<String, Object> params = new HashMap<>();
        	params.put("clock", dispatchEnergyLog.getClock());
        	list = dispatchEnergyLogActService.query(params);
        }
        Map<String, Object> m = new HashMap<String, Object>();
    	m.put("orderLog", orderLog);
    	m.put("dLog", dLog);
    	m.put("list", list);
        return success(m);
    }

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的dispatchEnergyLog信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
        @ApiImplicitParam(name = "dispatchEnergyLog", value = "详细实体dispatchEnergyLog", required = true, dataType = "DispatchEnergyLog")
    })
    //@RequiresPermissions("dispatchenergylog:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody DispatchEnergyLog model){
        model.setId(id);
        DispatchEnergyLog dispatchEnergyLog = dispatchEnergyLogService.modify(model);
        return success(dispatchEnergyLog);
    }

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("dispatchenergylog:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id){
    	List<String> list = Arrays.asList(id.split(","));
    	if(ListUtils.isNotEmpty(list)) {
	    	List<String> ids = ListUtils.convertList(list);
			  dispatchEnergyLogService.delete(ids);
    	}
      return success();
    }
}
