package com.steerinfo.ems.tcm0316.controller;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.tcm0316.model.TCm0316;
import com.steerinfo.ems.tcm0316.service.ITCm0316Service;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

@RestController
@RequestMapping("/${api.version}/tcm0316s")
public class TCm0316Controller extends BaseRESTfulController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TCm0316Controller.class);
    @Autowired
    ITCm0316Service tCm0316Service;

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("trmunit:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TCm0316> list = tCm0316Service.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("tpmcaseact:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
        PageList<TCm0316> list = tCm0316Service.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }
    
    @ApiOperation(value="创建", notes="根据tCm0316对象创建")
    @ApiImplicitParam(name = "tCm0316", value = "详细实体tCm0316", required = true, dataType = "TCm0316")
    //@RequiresPermissions("trmunit:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute TCm0316 model){
        if(tCm0316Service.getById(model.getPowermonitorcode()) != null){
            return failed(null, "id已存在，不能新增");
        }
        else{
            if(model.getCreatyear() == null || "".equals(model.getCreatyear())){
                model.setCreatyear(DateUtils.getCurrentTime("yyyy"));
            }
            TCm0316 tCm0316 = tCm0316Service.add(model);
            return success(tCm0316);
        }
    }

    @ApiOperation(value="获取详细信息", notes="根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("trmunit:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id){
        TCm0316 tCm0316 = tCm0316Service.getById(id);
        return success(tCm0316);
    }

    @ApiOperation(value="更新详细信息", notes="根据url的id来指定更新对象，并根据传过来的tCm0316信息来更新详细信息")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
        @ApiImplicitParam(name = "tCm0316", value = "详细实体tCm0316", required = true, dataType = "TCm0316")
    })
    //@RequiresPermissions("trmunit:update")
    @PutMapping(value = "/{id}", produces  = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody TCm0316 model){
        LOGGER.info("动力监测点表修改前的数据是" + tCm0316Service.getById(id).toString());
        model.setPowermonitorcode(id);
        TCm0316 tCm0316 = tCm0316Service.modify(model);
        LOGGER.info("动力监测点表修改后的数据是" + tCm0316.toString());
        return success(tCm0316);
    }

    @ApiOperation(value="删除", notes="根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("trmunit:delete")
    @DeleteMapping(value = "/{id}")
    public RESTfulResult delete(@PathVariable String id){
        String[] array = id.split(",");
        if(array.length == 1){
            LOGGER.info("动力监测点表要删除的数据是" + tCm0316Service.getById(id).toString());
            tCm0316Service.delete(id);
            return success();
        }
        else if(array.length > 1) {
            for (int i = 0; i < array.length; i++) {
                LOGGER.info("动力监测点表要删除的第" + (i + 1) + "条数据是" + tCm0316Service.getById(array[i]).toString());
                tCm0316Service.delete(array[i]);
            }
            LOGGER.info("动力监测点表要删除数据共" + array.length + "条。");
            return success();
        }
        else {
            return failed(null, "请输入要删除的id");
        }
    }

    @ApiOperation(value="监测点下拉框", notes="查询id和name")
    @GetMapping(value = "/getidandname/")
    public RESTfulResult getNameAndId(){
        List<TCm0316> tCm0316 = tCm0316Service.getNameAndId();
        return success(tCm0316);
    }
}
