package com.phm.manage.controller; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletResponse; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import org.apache.commons.lang3.ObjectUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; 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.RestController; import org.springframework.web.multipart.MultipartFile; import com.phm.common.annotation.Log; import com.phm.common.core.controller.BaseController; import com.phm.common.core.domain.AjaxResult; import com.phm.common.core.page.TableDataInfo; import com.phm.common.enums.BusinessType; import com.phm.common.exception.GlobalException; import com.phm.common.utils.bean.BeanUtils; import com.phm.common.utils.poi.ExcelUtil; import com.phm.manage.domain.OrderConfig; import com.phm.manage.domain.common.CommonResult; import com.phm.manage.domain.common.OrderXmlVO; import com.phm.manage.service.IOrderConfigService; import com.phm.manage.util.JaxbUtil; import cn.hutool.core.bean.BeanUtil; /** * 指令配置Controller * * @author phm * @date 2023-10-07 */ @RestController @RequestMapping("/manage/orderConfig") public class OrderConfigController extends BaseController { @Autowired private IOrderConfigService orderConfigService; /** * 查询指令配置列表 */ @PreAuthorize("@ss.hasPermi('manage:orderConfig:list')") @GetMapping("/list") public TableDataInfo list(OrderConfig orderConfig) { startPage(); List list = orderConfigService.selectOrderConfigList(orderConfig); return getDataTable(list); } /** * 导出指令配置列表 */ @PreAuthorize("@ss.hasPermi('manage:orderConfig:export')") @Log(title = "指令配置", businessType = BusinessType.EXPORT) @PostMapping("/export") public void export(HttpServletResponse response, OrderConfig orderConfig) { List list = orderConfigService.selectOrderConfigList(orderConfig); ExcelUtil util = new ExcelUtil(OrderConfig.class); util.exportExcel(response, list, "指令配置数据"); } /** * 获取指令配置详细信息 */ @PreAuthorize("@ss.hasPermi('manage:orderConfig:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return success(orderConfigService.selectOrderConfigById(id)); } @PreAuthorize("@ss.hasPermi('manage:orderConfig:query')") @GetMapping(value = "/xml/{id}") public CommonResult getInfoXml(@PathVariable("id") Long id) throws Exception { OrderConfig orderConfig = orderConfigService.selectOrderConfigById(id); OrderXmlVO xmlVO = new OrderXmlVO(); BeanUtil.copyProperties(orderConfig, xmlVO); return CommonResult.success(JaxbUtil.convertToXml(xmlVO)); } /** * 新增指令配置 */ @PreAuthorize("@ss.hasPermi('manage:orderConfig:add')") @Log(title = "指令配置", businessType = BusinessType.INSERT) @PostMapping public CommonResult add(@RequestBody OrderConfig orderConfig) { OrderConfig order = orderConfigService.selectOrderConfigByCode(orderConfig.getOrderCode()); if (ObjectUtils.isNotEmpty(order)) { return CommonResult.error("指令编码已存在,请重新输入!"); } return CommonResult.success(orderConfigService.insertOrderConfig(orderConfig)); } /** * 修改指令配置 */ @PreAuthorize("@ss.hasPermi('manage:orderConfig:edit')") @Log(title = "指令配置", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody OrderConfig orderConfig) { return toAjax(orderConfigService.updateOrderConfig(orderConfig)); } /** * 删除指令配置 */ @PreAuthorize("@ss.hasPermi('manage:orderConfig:remove')") @Log(title = "指令配置", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(orderConfigService.deleteOrderConfigByIds(ids)); } /** * 导入xml * * @param file file * @param updateSupport updateSupport * @return res */ @PostMapping(value = "/import/xml") public CommonResult importData(MultipartFile file, boolean updateSupport) { OrderXmlVO orderXmlVO = new OrderXmlVO(); OrderConfig orderConfig = new OrderConfig(); try { JAXBContext jaxbContext = JAXBContext.newInstance(OrderXmlVO.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); orderXmlVO = (OrderXmlVO)jaxbUnmarshaller.unmarshal(file.getInputStream()); } catch (JAXBException | IOException exception) { logger.error("XML解析错误,信息:{}", exception.getMessage()); throw new GlobalException("XML解析错误,请检查XMl格式"); } BeanUtils.copyBeanProp(orderConfig, orderXmlVO); OrderConfig order = orderConfigService.selectOrderConfigByCode(orderConfig.getOrderCode()); if (ObjectUtils.isNotEmpty(order)) { return CommonResult.error("指令编码已存在,请重新输入!"); } return CommonResult.success(orderConfigService.insertOrderConfig(orderConfig)); } }