SysCustomFieldController.java 10.9 KB
package com.skua.modules.system.controller;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.skua.core.api.vo.Result;
import com.skua.core.aspect.annotation.AutoLog;
import com.skua.core.query.QueryGenerator;
import com.skua.modules.system.entity.SysCustomField;
import com.skua.modules.system.entity.SysCustomTable;
import com.skua.modules.system.service.ISysCustomFieldService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

 /**
  * 字段维护
  * @author liyy
  *
  */
@Slf4j
@Api(tags="字段维护")
@ApiIgnore
@RestController
@RequestMapping("/sys/sysCustomField")
public class SysCustomFieldController {
	@Autowired
	private ISysCustomFieldService sysCustomFieldService;
	
	/**
	  * 分页列表查询
	 * @param sysCustomField
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "字段维护-分页列表查询")
	@ApiOperation(value="字段维护-分页列表查询", notes="字段维护-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysCustomField>> queryPageList(SysCustomField sysCustomField,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<SysCustomField>> result = new Result<IPage<SysCustomField>>();
		QueryWrapper<SysCustomField> queryWrapper = QueryGenerator.initQueryWrapper(sysCustomField, req.getParameterMap());
		queryWrapper.orderByAsc("field_sort");
		Page<SysCustomField> page = new Page<SysCustomField>(pageNo, pageSize);
		IPage<SysCustomField> pageList = sysCustomFieldService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	@AutoLog(value = "字段维护-分页列表查询-对客户开放")
	@ApiOperation(value="字段维护-分页列表查询-对客户开放", notes="字段维护-分页列表查询-对客户开放")
	@GetMapping(value = "/showList")
	public Result<IPage<SysCustomField>> queryPageShowList(SysCustomField sysCustomField,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<SysCustomField>> result = new Result<IPage<SysCustomField>>();
		QueryWrapper<SysCustomField> queryWrapper = QueryGenerator.initQueryWrapper(sysCustomField, req.getParameterMap());
		queryWrapper.eq("is_open", "1");
		queryWrapper.orderByAsc("field_sort","field_name").orderByAsc("field_sort");
		Page<SysCustomField> page = new Page<SysCustomField>(pageNo, pageSize);
		IPage<SysCustomField> pageList = sysCustomFieldService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	@AutoLog(value = "字段维护-分页列表查询-表头维护")
	@ApiOperation(value="字段维护-分页列表查询-表头维护", notes="字段维护-分页列表查询-表头维护")
	@GetMapping(value = "/showHeadList")
	public Result<IPage<SysCustomField>> queryPageShowHeadList(SysCustomField sysCustomField,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		Result<IPage<SysCustomField>> result = new Result<IPage<SysCustomField>>();
		QueryWrapper<SysCustomField> queryWrapper = QueryGenerator.initQueryWrapper(sysCustomField, req.getParameterMap());
		queryWrapper.orderByAsc("field_sort","field_name").orderByAsc("field_sort");
		Page<SysCustomField> page = new Page<SysCustomField>(pageNo, pageSize);
		IPage<SysCustomField> pageList = sysCustomFieldService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	@AutoLog(value = "字段维护-无分页列表")
	@ApiOperation(value="字段维护-无分页列表", notes="字段维护-无分页列表")
	@GetMapping(value = "/listNoPage")
	public Result<List<SysCustomField>> queryList(SysCustomField sysCustomField,HttpServletRequest req) {
		Result<List<SysCustomField>> result = new Result<List<SysCustomField>>();
		QueryWrapper<SysCustomField> queryWrapper = QueryGenerator.initQueryWrapper(sysCustomField, req.getParameterMap());
		List<SysCustomField> list = sysCustomFieldService.list(queryWrapper);
		result.setSuccess(true);
		result.setResult(list);
		return result;
	}
	
	@AutoLog(value = "字段维护-获取列表页表头")
	@ApiOperation(value="字段维护-获取列表页表头", notes="字段维护-获取列表页表头")
	@GetMapping(value = "/listHead")
	public Result<List<SysCustomField>> queryListPageHeadField(SysCustomTable sysCustomTable,HttpServletRequest req) {
		Result<List<SysCustomField>> result = new Result<List<SysCustomField>>();
		List<SysCustomField> list = sysCustomFieldService.queryListPageHeadField(sysCustomTable);
		result.setSuccess(true);
		result.setResult(list);
		return result;
	}
	
	@AutoLog(value = "字段维护-获取表单页字段")
	@ApiOperation(value="字段维护-获取表单页字段", notes="字段维护-获取表单页字段")
	@GetMapping(value = "/listModel") 
	public Result<Map<String,List<SysCustomField>>> queryModelPageField(SysCustomTable sysCustomTable,HttpServletRequest req) {
		Result<Map<String,List<SysCustomField>>> result = new Result<Map<String,List<SysCustomField>>>();
		List<SysCustomField> list = sysCustomFieldService.queryModelPageField(sysCustomTable);
		//对列表进行分组
		List<SysCustomField> infoList = new ArrayList<SysCustomField>();
		List<SysCustomField> codeList = new ArrayList<SysCustomField>();
		List<SysCustomField> installList = new ArrayList<SysCustomField>();
		List<SysCustomField> fileList = new ArrayList<SysCustomField>();
		for(SysCustomField sysCustomField : list) {
			String fieldGroup = sysCustomField.getFieldGroup();
			if("1".equals(fieldGroup)) {
				infoList.add(sysCustomField);
			}else if("2".equals(fieldGroup)) {
				codeList.add(sysCustomField);
			}else if("3".equals(fieldGroup)) {
				installList.add(sysCustomField);
			}else if("4".equals(fieldGroup)) {
				fileList.add(sysCustomField);
			}
		}
		Map<String,List<SysCustomField>> map = new HashMap<String,List<SysCustomField>>();
		map.put("info", infoList);
		map.put("code", codeList);
		map.put("install", installList);
		map.put("file", fileList);
		result.setSuccess(true);
		result.setResult(map);
		return result;
	}
	
	/**
	  *   添加
	 * @param sysCustomField
	 * @return
	 */
	@AutoLog(value = "字段维护-添加")
	@ApiOperation(value="字段维护-添加", notes="字段维护-添加")
	@PostMapping(value = "/add")
	public Result<SysCustomField> add(@RequestBody SysCustomField sysCustomField) {
		Result<SysCustomField> result = new Result<SysCustomField>();
		try {
			sysCustomFieldService.save(sysCustomField);
			result.success("添加成功!");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}
	/**
	 * 表字段同步,不改变已经配置的字段,新增未配置字段
	 * @param sysCustomField
	 * @return
	 */
	@AutoLog(value = "字段维护-表字段同步")
	@ApiOperation(value="字段维护-表字段同步", notes="字段维护-表字段同步")
	@PostMapping(value = "/sync")
	public Result<SysCustomTable> sync(@RequestBody SysCustomTable sysCustomTable) {
		Result<SysCustomTable> result = new Result<SysCustomTable>();
		try {
			sysCustomFieldService.syncSysCustomField(sysCustomTable);
			result.success("字段同步成功!");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("字段同步失败");
		}
		return result;
	}
	
	/**
	  *  编辑
	 * @param sysCustomField
	 * @return
	 */
	@AutoLog(value = "字段维护-编辑")
	@ApiOperation(value="字段维护-编辑", notes="字段维护-编辑")
	@PutMapping(value = "/edit")
	public Result<SysCustomField> edit(@RequestBody SysCustomField sysCustomField) {
		Result<SysCustomField> result = new Result<SysCustomField>();
		SysCustomField sysCustomFieldEntity = sysCustomFieldService.getById(sysCustomField.getId());
		if(sysCustomFieldEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = sysCustomFieldService.updateById(sysCustomField);
			//TODO 返回false说明什么?
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *   通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "字段维护-通过id删除")
	@ApiOperation(value="字段维护-通过id删除", notes="字段维护-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try {
			sysCustomFieldService.removeById(id);
		} catch (Exception e) {
			log.error("删除失败",e.getMessage());
			return Result.error("删除失败!");
		}
		return Result.ok("删除成功!");
	}
	
	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "字段维护-批量删除")
	@ApiOperation(value="字段维护-批量删除", notes="字段维护-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<SysCustomField> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<SysCustomField> result = new Result<SysCustomField>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别!");
		}else {
			this.sysCustomFieldService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@AutoLog(value = "字段维护-通过id查询")
	@ApiOperation(value="字段维护-通过id查询", notes="字段维护-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysCustomField> queryById(@RequestParam(name="id",required=true) String id) {
		Result<SysCustomField> result = new Result<SysCustomField>();
		SysCustomField sysCustomField = sysCustomFieldService.getById(id);
		if(sysCustomField==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(sysCustomField);
			result.setSuccess(true);
		}
		return result;
	}

}