package com.zy.asrs.wcs.system.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.zy.asrs.framework.common.Cools; import com.zy.asrs.framework.common.R; import com.zy.asrs.wcs.common.annotation.OperationLog; import com.zy.asrs.wcs.common.domain.BaseParam; import com.zy.asrs.wcs.common.domain.KeyValVo; import com.zy.asrs.wcs.common.domain.PageParam; import com.zy.asrs.wcs.system.entity.Host; import com.zy.asrs.wcs.system.service.HostService; import com.zy.asrs.wcs.utils.ExcelUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.util.*; @RestController @RequestMapping("/api") public class HostController extends BaseController { @Autowired private HostService hostService; @PreAuthorize("hasAuthority('system:host:list')") @PostMapping("/host/page") public R page(@RequestBody Map map) { BaseParam baseParam = buildParam(map, BaseParam.class); PageParam pageParam = new PageParam<>(baseParam, Host.class); return R.ok().add(hostService.page(pageParam, pageParam.buildWrapper(true))); } @PreAuthorize("hasAuthority('system:host:list')") @PostMapping("/host/list") public R list(@RequestBody Map map) { return R.ok().add(hostService.list()); } @PreAuthorize("hasAuthority('system:host:list')") @GetMapping("/host/{id}") public R get(@PathVariable("id") Long id) { return R.ok().add(hostService.getById(id)); } @PreAuthorize("hasAuthority('system:host:save')") @OperationLog("添加机构") @PostMapping("/host/save") public R save(@RequestBody Host host) { host.setCreateTime(new Date()); host.setUpdateTime(new Date()); if (!hostService.save(host)) { return R.error("添加失败"); } return R.ok("添加成功"); } @PreAuthorize("hasAuthority('system:host:update')") @OperationLog("修改机构") @PostMapping("/host/update") public R update(@RequestBody Host host) { host.setUpdateTime(new Date()); if (!hostService.updateById(host)) { return R.error("修改失败"); } return R.ok("修改成功"); } @PreAuthorize("hasAuthority('system:host:remove')") @OperationLog("删除机构") @PostMapping("/host/remove/{ids}") public R remove(@PathVariable Long[] ids) { if (!hostService.removeByIds(Arrays.asList(ids))) { return R.error("删除失败"); } return R.ok("删除成功"); } @PreAuthorize("hasAuthority('system:host:list')") @PostMapping("/host/query") public R query(@RequestParam(required = false) String condition) { List vos = new ArrayList<>(); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (!Cools.isEmpty(condition)) { wrapper.like(Host::getName, condition); } hostService.page(new Page<>(1, 30), wrapper).getRecords().forEach( item -> vos.add(new KeyValVo(item.getId(), item.getName())) ); return R.ok().add(vos); } @PreAuthorize("hasAuthority('system:host:list')") @PostMapping("/host/export") public void export(@RequestBody Map map, HttpServletResponse response) throws Exception { ExcelUtil.build(ExcelUtil.create(hostService.list(), Host.class), response); } }