Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / River / RiverInfoController.java
package com.newfiber.api.pc.controller.River;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newfiber.api.config.FileVisitProperty;
import com.newfiber.api.core.annotation.SysLog;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.pc.dao.RiverManage.RiverInfoDao;
import com.newfiber.api.pc.model.River.RiverInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wmj
 * @version 创建时间:2017/11/25
 * @description 河道基础信息控制器
 */
@Controller
@CrossOrigin
@RequestMapping("riverinfo")
public class RiverInfoController {

    @Autowired
    private RiverInfoDao riverInfoDao;

    /** 节点文件保存路径 */
    @Value("${xnFile.path}")
    private String xnFilePath;



    @Autowired
    private FileVisitProperty fileVisitProperty;

    @PostMapping("list")
    @ResponseBody
    @SysLog(value="河道基础信息列表",actionType="4")
    public RespBodyObj selectRiver(@RequestBody ReqBodyObj<Map<String, Object>> params){
        Integer pageSize = params.getSize();
        Integer pageNo = params.getCurrent();
        String riverName = String.valueOf(params.getData().get("riverName"));
        PageHelper.startPage(pageNo, pageSize);
        List<RiverInfo> list  = riverInfoDao.selectRiver(riverName);
        PageInfo<RiverInfo> result = new PageInfo<>();
        if (!list.isEmpty()) {
            list.forEach(e->{
                if(!StringUtils.isEmpty(e.getFilePath())){
                    e.setFilePath(fileVisitProperty.getWebFile()+e.getFilePath());
                }
            });
            result = new PageInfo<>(list);
        }
        return RespBodyObj.ok(result);
    }

    /**
     * 新建或编辑
     * @param param
     * @return
     */
    @PostMapping("addOrUpdate")
    @ResponseBody
    @SysLog(value="添加河道基础信息",actionType="1")
    public RespBodyObj addOrUpdate(@RequestBody ReqBodyObj<RiverInfo> param) {
        try {
            int i = 0;
            if (param.getData().getId() == null) {
                 i = riverInfoDao.insertSelective(param.getData());
            }else{
                i = riverInfoDao.updateByPrimaryKeySelective(param.getData());
            }
            return i > 0 ? RespBodyObj.ok("操作成功") : RespBodyObj.error("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error("操作异常");
        }
    }

    /**
     * 删除河流
     * @param params
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    @SysLog(value="删除河道基础信息",actionType="2")
    public RespBodyObj delete(@RequestBody ReqBodyObj<Map<String, Object>> params) {
        try {
            String id = String.valueOf(params.getData().get("id"));
            RiverInfo riverInfo = riverInfoDao.selectByPrimaryKey(Integer.valueOf(id));
            if(riverInfo == null){
                return RespBodyObj.error("删除失败");
            }
            if(!StringUtils.isEmpty(riverInfo.getFilePath())){
                File file = new File(xnFilePath,riverInfo.getFilePath());
                try {
                    if(file.exists()){
                        file.delete();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            Integer i = riverInfoDao.deleteByPrimaryKey(Integer.valueOf(id));
            return i > 0 ? RespBodyObj.ok("操作成功") : RespBodyObj.error("操作失败");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error("操作异常");
        }
    }

    @PostMapping(value = "/uploadFile")
    @ApiOperation("上传文件")
    @ResponseBody
    @SysLog(value="上传文件",actionType="1")
    public RespBodyObj uploadFile(@RequestParam("file") MultipartFile multipartFile){
        String filename = multipartFile.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
      /*  if(!suffix.equalsIgnoreCase(".pdf")){
            return new RespBodyObj.error(500,"必须上传pdf文件!");
        }*/
        Calendar instance = Calendar.getInstance();
        //String filePath = instance.get(Calendar.YEAR)  + File.separator +(instance.get(Calendar.MARCH) + 1) + File.separator + filename;
        StringBuffer sb = new StringBuffer();
        sb.append(instance.get(Calendar.YEAR))
                .append(File.separator)
                .append(instance.get(Calendar.MARCH) + 1).
                append(File.separator)
                .append(System.currentTimeMillis()).append(suffix);
        File file = new File(xnFilePath ,  sb.toString());
        String path = sb.toString();
        String realPath = fileVisitProperty.getWebFile()+sb.toString();
        Map<String,Object> map = new HashMap<>();
        map.put("path",path);
        map.put("realPath",realPath);
        map.put("fileName",filename);
        if(!file.getParentFile().exists()){
            file.mkdirs();
        }
        try{
            multipartFile.transferTo(file);
        }catch (IOException e){
            return RespBodyObj.error("上传失败");
        }
        return RespBodyObj.ok(map);
    }

    @PostMapping(value = "/deleteFile")
    @ApiOperation("删除文件")
    @ResponseBody
    @SysLog(value="删除文件",actionType="2")
    public RespBodyObj deleteFile(@RequestBody ReqBodyObj<Map<String, Object>> params){
        Integer id = Integer.valueOf(params.getData().get("id").toString());
        try{
            RiverInfo riverInfo = riverInfoDao.selectByPrimaryKey(id);
            if(riverInfo == null || StringUtils.isEmpty(riverInfo.getFilePath())){
                return RespBodyObj.error("删除失败");
            }
            File file = new File(xnFilePath , riverInfo.getFilePath());
            boolean delete = false;
            if(file.exists()){
                 delete = file.delete();
            }
            if(delete){
                riverInfo.setFilePath(null);
                riverInfoDao.updateByPrimaryKeyWithBLOBs(riverInfo);
                return RespBodyObj.ok("删除成功");
            }
        }catch (Exception e){
            return RespBodyObj.error("删除失败");
        }
        return RespBodyObj.ok("删除失败");
    }

    /**
     *  16条基础河
     * @return
     */
    @PostMapping(value = "/riverList")
    @ApiOperation("河湖列表")
    @ResponseBody
    @SysLog(value="河湖列表",actionType="4")
    public RespBodyObj riverList(){
        List<RiverInfo> result = riverInfoDao.selectAllRiver();
        return RespBodyObj.ok(result);
    }
}