package com.steerinfo.ems.zghbjkbzlog.controller;

import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.ems.zghbjkbzlog.model.ZghbjkbzLog;
import com.steerinfo.ems.zghbjkbzlog.service.IZghbjkbzLogService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

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

    private static final Logger LOGGER = LoggerFactory.getLogger(ZghbjkbzLogController.class);

    @Autowired
    IZghbjkbzLogService zghbjkbzLogService;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("zghbjkbz:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        if(parmas.containsKey("wrType") && parmas.get("wrType") != null && !"".equals(parmas.get("wrType").toString())){
            String wrType = parmas.get("wrType").toString();
            if(wrType.indexOf(",") == -1){
                parmas.put("wrType", "and m.wr_type = '" + wrType + "'");
            } else {
            	wrType = "and m.wr_type in ('" + wrType.replaceAll(",", "','") + "')";
                parmas.put("wrType", wrType);
            }
        }
        if(parmas.containsKey("sourceName") && parmas.get("sourceName") != null && !"".equals(parmas.get("sourceName").toString())){
            String sourceName = parmas.get("sourceName").toString();
            if(sourceName.indexOf(",") == -1){
                parmas.put("sourceName", "and m.source_Name = '" + sourceName + "'");
            } else {
            	sourceName = "and m.source_Name in ('" + sourceName.replaceAll(",", "','") + "')";
                parmas.put("sourceName", sourceName);
            }
        }
        if(parmas.containsKey("drainName") && parmas.get("drainName") != null && !"".equals(parmas.get("drainName").toString())){
            String drainName = parmas.get("drainName").toString();
            if(drainName.indexOf(",") == -1){
                parmas.put("drainName", "and m.drain_name = '" + drainName + "'");
            } else {
            	drainName = "and m.drain_name in ('" + drainName.replaceAll(",", "','") + "')";
                parmas.put("drainName", drainName);
            }
        }
        if(parmas.containsKey("location") && parmas.get("location") != null && !"".equals(parmas.get("location").toString())){
            String location = parmas.get("location").toString();
            if(location.indexOf(",") == -1){
                parmas.put("location", "and m.location = '" + location + "'");
            } else {
            	location = "and m.location in ('" + location.replaceAll(",", "','") + "')";
                parmas.put("location", location);
            }
        }
        if(parmas.containsKey("dateBegin") && parmas.get("dateBegin") != null && !"".equals(parmas.get("dateBegin").toString()) &&
        		parmas.containsKey("dateEnd") && parmas.get("dateEnd") != null && !"".equals(parmas.get("dateEnd").toString())){
            String begin = parmas.get("dateBegin").toString();
            String end = parmas.get("dateEnd").toString();
            
            parmas.put("time", "and t.TIM between to_date('" + begin + "', 'yyyy-mm-dd HH24:MI:SS') and to_date('" + end + "', 'yyyy-mm-dd HH24:MI:SS')");
        }
        PageList<ZghbjkbzLog> list = zghbjkbzLogService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="创建", notes="根据ZghbjkbzLog对象创建")
    @ApiImplicitParam(name = "zghbjkbzLog", value = "详细实体zghbjkbzLog", required = true, dataType = "ZghbjkbzLog")
    //@RequiresPermissions("zghbjkbz:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute ZghbjkbzLog model){
        ZghbjkbzLog zghbjkbzLog = zghbjkbzLogService.add(model);
        return success(zghbjkbzLog);
    }

    @ApiOperation(value="批量删除", notes="根据传过来的zghbjkbzLog数组信息来删除")
    @ApiImplicitParam(name = "zghbjkbzLog", value = "详细实体zghbjkbzLog", required = true, dataType = "ZghbjkbzLog")
    //@RequiresPermissions("zghbjkbz:delete")
    @DeleteMapping(value = "/", produces  = "application/json;charset=UTF-8")
    public RESTfulResult delete(@RequestBody ZghbjkbzLog[] model){
        if(model.length >= 1) {
            for (int i = 0; i < model.length; i++) {
            	zghbjkbzLogService.delete(model[i].getId());
            }
            return success();
        }
        else {
            return failed(null, "请选择删除数据");
        }
    }
}