package com.huaheng.pc.config.materialWarnning.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.huaheng.common.utils.DateUtils; import com.huaheng.common.utils.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.huaheng.common.utils.poi.ExcelUtil; import com.huaheng.common.utils.security.ShiroUtils; import com.huaheng.framework.web.page.PageDomain; import com.huaheng.framework.web.page.TableDataInfo; import com.huaheng.framework.web.page.TableSupport; import com.huaheng.common.utils.StringUtils; import com.huaheng.pc.config.material.domain.Material; import com.huaheng.pc.config.material.service.MaterialService; import com.huaheng.pc.config.materialWarnning.domain.MaterialSummary; import com.huaheng.pc.inventory.InventoryMaterialSummary.domain.InventoryMaterialSummary; import com.huaheng.pc.inventory.InventoryMaterialSummary.service.InventoryMaterialSummaryService; import com.huaheng.pc.inventory.inventoryDetail.domain.InventoryDetail; import com.huaheng.pc.inventory.inventoryDetail.service.InventoryDetailService; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; 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.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.huaheng.framework.aspectj.lang.annotation.Log; import com.huaheng.framework.aspectj.lang.constant.BusinessType; import com.huaheng.pc.config.materialWarnning.domain.MaterialWarning; import com.huaheng.pc.config.materialWarnning.service.IMaterialWarningService; import com.huaheng.framework.web.controller.BaseController; import com.huaheng.framework.web.domain.AjaxResult; import com.huaheng.common.support.Convert; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; /** * 物料报警 信息操作处理 * * @author huaheng * @date 2020-07-17 */ @Controller @RequestMapping("/config/materialWarning") public class MaterialWarningController extends BaseController { private String prefix = "config/materialWarning"; @Resource private IMaterialWarningService materialWarningService; @Resource private InventoryDetailService inventoryDetailService; @Resource private InventoryMaterialSummaryService inventoryMaterialSummaryService; @Resource private MaterialService materialService; @RequiresPermissions("config:materialWarning:view") @GetMapping() public String materialWarning() { return prefix + "/materialWarning"; } /** * 查询物料报警列表 */ @RequiresPermissions("config:materialWarning:list") @PostMapping("/list") @ResponseBody public TableDataInfo list(MaterialWarning materialWarning) { LambdaQueryWrapper<MaterialWarning> lambdaQueryWrapper = Wrappers.lambdaQuery(); lambdaQueryWrapper .eq(StringUtils.isNotEmpty(materialWarning.getAlarmType()), MaterialWarning::getAlarmType, materialWarning.getAlarmType()) .eq(StringUtils.isNotEmpty(materialWarning.getWarehouseCode()), MaterialWarning::getWarehouseCode, ShiroUtils.getWarehouseCode()) .eq(StringUtils.isNotEmpty(materialWarning.getCompanyCode()), MaterialWarning::getCompanyCode, materialWarning.getCompanyCode()) .eq(StringUtils.isNotEmpty(materialWarning.getMaterialCode()), MaterialWarning::getMaterialCode, materialWarning.getMaterialCode()) .like(StringUtils.isNotEmpty(materialWarning.getMaterialName()), MaterialWarning::getMaterialName, materialWarning.getMaterialName()) .eq(StringUtils.isNotEmpty(materialWarning.getMaterialSpec()), MaterialWarning::getMaterialSpec, materialWarning.getMaterialSpec()) .eq(StringUtils.isNotEmpty(materialWarning.getMaterialUnit()), MaterialWarning::getMaterialUnit, materialWarning.getMaterialUnit()) .eq(StringUtils.isNotNull(materialWarning.getMax()), MaterialWarning::getMax, materialWarning.getMax()) .eq(StringUtils.isNotNull(materialWarning.getMin()), MaterialWarning::getMin, materialWarning.getMin()) .eq(StringUtils.isNotNull(materialWarning.getUpper()), MaterialWarning::getUpper, materialWarning.getUpper()) .eq(StringUtils.isNotNull(materialWarning.getLower()), MaterialWarning::getLower, materialWarning.getLower()) .eq(StringUtils.isNotNull(materialWarning.getUserId()), MaterialWarning::getUserId, materialWarning.getUserId()) .like(StringUtils.isNotEmpty(materialWarning.getUserName()), MaterialWarning::getUserName, materialWarning.getUserName()) .eq(StringUtils.isNotEmpty(materialWarning.getEmail()), MaterialWarning::getEmail, materialWarning.getEmail()); PageDomain pageDomain = TableSupport.buildPageRequest(); Integer pageNum = pageDomain.getPageNum(); Integer pageSize = pageDomain.getPageSize(); if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) { /*使用分页查询*/ Page<MaterialWarning> page = new Page<>(pageNum, pageSize); IPage<MaterialWarning> iPage = materialWarningService.page(page, lambdaQueryWrapper); return getMpDataTable(iPage.getRecords(), iPage.getTotal()); } else { List<MaterialWarning> list = materialWarningService.list(lambdaQueryWrapper); return getDataTable(list); } } /** * 新增物料报警 */ @GetMapping("/add") public String add() { return prefix + "/add"; } /** * 新增保存物料报警 */ @RequiresPermissions("config:materialWarning:add") @Log(title = "物料报警", action = BusinessType.INSERT) @PostMapping("/add") @ResponseBody public AjaxResult addSave(MaterialWarning materialWarning) { return materialWarningService.addSave(materialWarning); } /** * 修改物料报警 */ @GetMapping("/edit/{id}") public String edit(@PathVariable("id") Integer id, ModelMap mmap) { MaterialWarning materialWarning = materialWarningService.getById(id); mmap.put("materialWarning", materialWarning); return prefix + "/edit"; } /** * 修改保存物料报警 */ @RequiresPermissions("config:materialWarning:edit") @Log(title = "物料报警", action = BusinessType.UPDATE) @PostMapping("/edit") @ResponseBody public AjaxResult editSave(MaterialWarning materialWarning) { String materialCode = materialWarning.getMaterialCode(); Material material = materialService.getMaterialByCode(materialCode); materialWarning.setMaterialName(material.getName()); materialWarning.setMaterialSpec(material.getSpec()); materialWarning.setMaterialUnit(material.getUnit()); materialWarning.setLastUpdatedBy(ShiroUtils.getLoginName()); materialWarning.setLastUpdated(DateUtils.getNowDate()); return toAjax(materialWarningService.updateById(materialWarning)); } /** * 删除物料报警 */ @RequiresPermissions("config:materialWarning:remove") @Log(title = "物料报警", action = BusinessType.DELETE) @PostMapping("/remove") @ResponseBody public AjaxResult remove(String ids) { if (StringUtils.isEmpty(ids)) { return AjaxResult.error("id不能为空"); } return toAjax(materialWarningService.removeByIds(Arrays.asList(Convert.toIntArray(ids)))); } @PostMapping("/getMaterialWarningProp") @ResponseBody public AjaxResult getMaterialWarningProp() { List<MaterialWarning> warningList = materialWarningService.list(); if(warningList == null || warningList.size() <= 0) { return AjaxResult.error("没有配置物料警告"); } int lowerNum = 0, upperNum = 0; for(MaterialWarning materialWarning : warningList) { String materialCode = materialWarning.getMaterialCode(); BigDecimal uppper = materialWarning.getUpper(); BigDecimal lower = materialWarning.getLower(); LambdaQueryWrapper<InventoryMaterialSummary> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery(); inventoryDetailLambdaQueryWrapper.eq(InventoryMaterialSummary::getMaterialCode, materialCode); List<InventoryMaterialSummary> list = inventoryMaterialSummaryService.list(inventoryDetailLambdaQueryWrapper); if (list == null) { list = Collections.emptyList(); } //筛选库存汇总数据的专用方法 List<InventoryMaterialSummary> details = inventoryMaterialSummaryService.duplicateRemoval(list); if(details == null || details.size() <= 0) { if(lower.intValue() > 0) { lowerNum++; } } else { InventoryMaterialSummary inventoryMaterialSummary = details.get(0); BigDecimal qty = inventoryMaterialSummary.getQty(); if(uppper.compareTo(qty) < 0) { upperNum++; } else if(lower.compareTo(qty) > 0) { lowerNum++; } } } MaterialSummary materialSummary = new MaterialSummary(); materialSummary.setLower(lowerNum); materialSummary.setUpper(upperNum); return AjaxResult.success(materialSummary); } @PostMapping("/getLowerWarning") @ResponseBody public AjaxResult getLowerWarning() { List<MaterialWarning> warningList = materialWarningService.list(); if(warningList == null || warningList.size() <= 0) { return AjaxResult.error("没有配置物料警告"); } List<InventoryMaterialSummary> inventoryMaterialSummaryArrayList = new ArrayList<>(); for(MaterialWarning materialWarning : warningList) { String materialCode = materialWarning.getMaterialCode(); String materialName = materialWarning.getMaterialName(); String materialUnit = materialWarning.getMaterialUnit(); BigDecimal uppper = materialWarning.getUpper(); BigDecimal lower = materialWarning.getLower(); LambdaQueryWrapper<InventoryMaterialSummary> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery(); inventoryDetailLambdaQueryWrapper.eq(InventoryMaterialSummary::getMaterialCode, materialCode); List<InventoryMaterialSummary> list = inventoryMaterialSummaryService.list(inventoryDetailLambdaQueryWrapper); if (list == null) { list = Collections.emptyList(); } //筛选库存汇总数据的专用方法 List<InventoryMaterialSummary> details = inventoryMaterialSummaryService.duplicateRemoval(list); if(details == null || details.size() <= 0) { if(lower.intValue() > 0) { InventoryMaterialSummary inventoryMaterialSummary = new InventoryMaterialSummary(); inventoryMaterialSummary.setMaterialCode(materialCode); inventoryMaterialSummary.setUserDef1(String.valueOf(lower)); inventoryMaterialSummary.setMaterialName(materialName); inventoryMaterialSummary.setMaterialUnit(materialUnit); inventoryMaterialSummaryArrayList.add(inventoryMaterialSummary); } } else { InventoryMaterialSummary inventoryMaterialSummary = details.get(0); BigDecimal qty = inventoryMaterialSummary.getQty(); if(lower.compareTo(qty) > 0) { BigDecimal def1 = lower.subtract(qty); inventoryMaterialSummary.setUserDef1(String.valueOf(def1.intValue())); inventoryMaterialSummaryArrayList.add(inventoryMaterialSummary); } } } return AjaxResult.success(inventoryMaterialSummaryArrayList); } @PostMapping("/getUpperWarning") @ResponseBody public AjaxResult getUpperWarning() { List<MaterialWarning> warningList = materialWarningService.list(); if(warningList == null || warningList.size() <= 0) { return AjaxResult.error("没有配置物料警告"); } List<InventoryMaterialSummary> inventoryMaterialSummaryArrayList = new ArrayList<>(); for(MaterialWarning materialWarning : warningList) { String materialCode = materialWarning.getMaterialCode(); BigDecimal uppper = materialWarning.getUpper(); BigDecimal lower = materialWarning.getLower(); LambdaQueryWrapper<InventoryMaterialSummary> inventoryDetailLambdaQueryWrapper = Wrappers.lambdaQuery(); inventoryDetailLambdaQueryWrapper.eq(InventoryMaterialSummary::getMaterialCode, materialCode); List<InventoryMaterialSummary> list = inventoryMaterialSummaryService.list(inventoryDetailLambdaQueryWrapper); if (list == null) { list = Collections.emptyList(); } //筛选库存汇总数据的专用方法 List<InventoryMaterialSummary> details = inventoryMaterialSummaryService.duplicateRemoval(list); if(details == null || details.size() <= 0) { } else { InventoryMaterialSummary inventoryMaterialSummary = details.get(0); BigDecimal qty = inventoryMaterialSummary.getQty(); if(uppper.compareTo(qty) < 0) { BigDecimal def1 = qty.subtract(uppper); inventoryMaterialSummary.setUserDef1(String.valueOf(def1.intValue())); inventoryMaterialSummaryArrayList.add(inventoryMaterialSummary); } } } return AjaxResult.success(inventoryMaterialSummaryArrayList); } /** * 导出模板 * * @return * @throws Exception */ @GetMapping("/importTemplate") @ResponseBody public AjaxResult importTemplate () { ExcelUtil<MaterialWarning> util = new ExcelUtil<>(MaterialWarning.class); return util.importTemplateExcel("物料预警添加"); } /** * 导入物料预警 * @param file * @return * @throws Exception */ @PostMapping("/importData") @ResponseBody public AjaxResult importData (MultipartFile file) throws Exception { ExcelUtil<MaterialWarning> util = new ExcelUtil<>(MaterialWarning.class); List<MaterialWarning> list = util.importExcel(file.getInputStream()); String operName = ShiroUtils.getLoginName(); String message = materialWarningService.insertExcelData(list, false, operName); return AjaxResult.success(message); } }