package com.steerinfo.ems.tcm0317.controller;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.ems.tcm0317.model.TCm0317;
import com.steerinfo.ems.tcm0317.service.ITCm0317Service;
import com.steerinfo.ems.tmcdocdirectory.model.TMcDocdirectory;
import com.steerinfo.ems.tmcdocdirectory.service.ITMcDocdirectoryService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    ITCm0317Service tCm0317Service;
    
    @Autowired
    ITMcDocdirectoryService tMcDocdirectoryService;
    
    /** 文件保存路径  */
	public static final String FILE_DIR = "/static/";

    @ApiOperation(value="获取列表", notes="分页查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
        @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("tcm0317:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas,Integer pageNum, Integer pageSize){
    	if(parmas.containsKey("itemcode") && parmas.get("itemcode") != null && !"".equals(parmas.get("itemcode").toString())){
    		String itemcode = parmas.get("itemcode").toString();
    		TMcDocdirectory tmc = tMcDocdirectoryService.getById(itemcode);
    		if(tmc!= null && !tmc.getPath().isEmpty()){
    			parmas.put("parentcode", tmc.getPath());
            }
    	}
        PageList<TCm0317> list = tCm0317Service.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value="创建", notes="根据TCm0317对象创建")
    @ApiImplicitParam(name = "tCm0317", value = "详细实体tCm0317", required = true, dataType = "TCm0317")
    //@RequiresPermissions("tcm0317:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute TCm0317 model){
        if (model.getStarttime() == null || "".equals(model.getStarttime())) {
            model.setStarttime(DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss"));
        }
        TCm0317 tCm0317 = tCm0317Service.add(model);
        return success(tCm0317);
    }

    @ApiOperation(value="删除", notes="根据url的recordid来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("tcm0317:delete")
    @DeleteMapping(value = "/{id}")
    public RESTfulResult delete(@PathVariable String id){
    	TCm0317 tCm0317 = tCm0317Service.getById(id);
    	if(tCm0317 != null) {
    		File file = new File(FILE_DIR + tCm0317.getParentcode() + "/" + tCm0317.getFilename());
    		if (file.exists()){
    			if (file.delete()) {
    				tCm0317Service.delete(id);
    			}else{
    				return failed(null, "删除文件失败");
    			}
    		} else {
    			tCm0317Service.delete(id);
    		}
    	}else{
    		return failed(null, "文件信息不存在");
    	}
        return success();
    }

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

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

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