|
|
|
package cn.redrock.assetmanage.controller;
|
|
|
|
|
|
|
|
import cn.redrock.assetmanage.auth.Admin;
|
|
|
|
import cn.redrock.assetmanage.auth.CurrentUser;
|
|
|
|
import cn.redrock.assetmanage.common.Constants;
|
|
|
|
import cn.redrock.assetmanage.common.ResponseCode;
|
|
|
|
import cn.redrock.assetmanage.common.Result;
|
|
|
|
import cn.redrock.assetmanage.dto.Material;
|
|
|
|
import cn.redrock.assetmanage.entity.AmMaterial;
|
|
|
|
import cn.redrock.assetmanage.entity.AmMaterialLog;
|
|
|
|
import cn.redrock.assetmanage.entity.AmUser;
|
|
|
|
import cn.redrock.assetmanage.service.MaterialLogService;
|
|
|
|
import cn.redrock.assetmanage.service.MaterialService;
|
|
|
|
import cn.redrock.assetmanage.utils.DateUtil;
|
|
|
|
import cn.redrock.assetmanage.utils.ExcelUtils;
|
|
|
|
import cn.redrock.assetmanage.utils.StringUtil;
|
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.format.annotation.DateTimeFormat;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.validation.Valid;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ Author : lihua
|
|
|
|
* @ Date :
|
|
|
|
* @ Description : 2019/7/23 16:55
|
|
|
|
* @ Modified by :
|
|
|
|
* @ Version : 1.0
|
|
|
|
*/
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/material")
|
|
|
|
public class MaterialController {
|
|
|
|
@Autowired
|
|
|
|
private MaterialService materialService;
|
|
|
|
@Autowired
|
|
|
|
private MaterialLogService materialLogService;
|
|
|
|
|
|
|
|
private AmMaterialLog buildLog(Integer userId, Constants.Operate operate, Constants.Status status, Material material){
|
|
|
|
if(StringUtil.isNotBlank(material.getCode()) && material.getNum() != null){
|
|
|
|
AmMaterialLog materialLog = new AmMaterialLog();
|
|
|
|
materialLog.setCode(material.getCode());
|
|
|
|
materialLog.setOperate(operate.getOperate());
|
|
|
|
materialLog.setCreateTime(new Date());
|
|
|
|
materialLog.setNum(material.getNum());
|
|
|
|
materialLog.setStatus(status.getStatus());
|
|
|
|
materialLog.setDescription(material.getDescription());
|
|
|
|
materialLog.setUserId(userId);
|
|
|
|
return materialLog;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<AmMaterialLog> buildBatchLog(Integer userId,Constants.Operate operate, Constants.Status status,List<Material> materials){
|
|
|
|
List<AmMaterialLog> materialLogs = new ArrayList<>();
|
|
|
|
for(Material material : materials){
|
|
|
|
AmMaterialLog log = buildLog(userId,operate,status,material);
|
|
|
|
if(log != null){
|
|
|
|
materialLogs.add(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return materialLogs;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Admin
|
|
|
|
@PostMapping("/add")
|
|
|
|
public Result addMaterial(@CurrentUser AmUser user,@Valid @RequestBody Material material) {
|
|
|
|
if(!materialService.add(material)){
|
|
|
|
materialLogService.writeLog(buildLog(user.getId(), Constants.Operate.IN, Constants.Status.FAILED,material));
|
|
|
|
return Result.error(ResponseCode.ASSET_EXISTED);
|
|
|
|
}
|
|
|
|
materialLogService.writeLog(buildLog(user.getId(),Constants.Operate.IN,Constants.Status.NO_AUDIT,material));
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Admin
|
|
|
|
@PostMapping("/batch/add")
|
|
|
|
public Result batchAddMaterial(@CurrentUser AmUser user,@RequestParam("file") MultipartFile file,@RequestParam(name = "isOverride",required = false) Boolean isOverride) {
|
|
|
|
List<Material> list = ExcelUtils.readExcel(file,Material.class);
|
|
|
|
if(list == null || list.isEmpty()){
|
|
|
|
return Result.error(ResponseCode.EXCEL_PARSE_ERROR);
|
|
|
|
}
|
|
|
|
boolean ret;
|
|
|
|
if(isOverride != null && isOverride){
|
|
|
|
ret = materialService.batchAdd(list,true);
|
|
|
|
}else{
|
|
|
|
ret = materialService.batchAdd(list,false);
|
|
|
|
}
|
|
|
|
if(!ret){
|
|
|
|
materialLogService.batchWriteLog(buildBatchLog(user.getId(),Constants.Operate.IN, Constants.Status.FAILED,list));
|
|
|
|
return Result.error(ResponseCode.ASSET_BATCH_ADD_FAIL);
|
|
|
|
}
|
|
|
|
materialLogService.batchWriteLog(buildBatchLog(user.getId(),Constants.Operate.IN,Constants.Status.NO_AUDIT,list));
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/sub")
|
|
|
|
public Result subMaterial(@CurrentUser AmUser user,@RequestBody Material material) {
|
|
|
|
if(user.getIsAdmin() == 1){
|
|
|
|
if(!materialService.sub(material)){
|
|
|
|
materialLogService.writeLog(buildLog(user.getId(), Constants.Operate.OUT, Constants.Status.FAILED,material));
|
|
|
|
return Result.error(ResponseCode.ASSET_SUB_FAIL);
|
|
|
|
}else{
|
|
|
|
materialLogService.writeLog(buildLog(user.getId(), Constants.Operate.OUT, Constants.Status.SUCCESS,material));
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
materialLogService.writeLog(buildLog(user.getId(),Constants.Operate.OUT,Constants.Status.NO_AUDIT,material));
|
|
|
|
}
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/batch/sub")
|
|
|
|
public Result batchSubMaterial(@CurrentUser AmUser user,@RequestParam("file") MultipartFile file) {
|
|
|
|
List<Material> list = ExcelUtils.readExcel(file,Material.class);
|
|
|
|
if(list == null || list.isEmpty()){
|
|
|
|
return Result.error(ResponseCode.EXCEL_PARSE_ERROR);
|
|
|
|
}
|
|
|
|
List<Material> successMaterial = new ArrayList<>();
|
|
|
|
List<Material> failMaterial = new ArrayList<>();
|
|
|
|
if(user.getIsAdmin() == 1){
|
|
|
|
materialService.batchSub(list,successMaterial,failMaterial);
|
|
|
|
if(!successMaterial.isEmpty() && failMaterial.isEmpty()){
|
|
|
|
materialLogService.batchWriteLog(buildBatchLog(user.getId(),Constants.Operate.OUT, Constants.Status.SUCCESS,list));
|
|
|
|
}else{
|
|
|
|
materialLogService.batchWriteLog(buildBatchLog(user.getId(),Constants.Operate.OUT, Constants.Status.FAILED,failMaterial));
|
|
|
|
materialLogService.batchWriteLog(buildBatchLog(user.getId(),Constants.Operate.OUT, Constants.Status.SUCCESS,successMaterial));
|
|
|
|
return Result.ok(failMaterial);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
materialLogService.batchWriteLog(buildBatchLog(user.getId(),Constants.Operate.OUT, Constants.Status.NO_AUDIT,list));
|
|
|
|
}
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Admin
|
|
|
|
@PostMapping("/audit")
|
|
|
|
public Result auditMaterial(@Valid @RequestBody List<Material> list) {
|
|
|
|
List<Material> successMaterial = new ArrayList<>();
|
|
|
|
List<Material> failMaterial = new ArrayList<>();
|
|
|
|
materialService.batchSub(list,successMaterial,failMaterial);
|
|
|
|
if(failMaterial.isEmpty()){
|
|
|
|
materialLogService.updateLog(list,Constants.Status.SUCCESS.getStatus());
|
|
|
|
}else{
|
|
|
|
materialLogService.updateLog(successMaterial,Constants.Status.SUCCESS.getStatus());
|
|
|
|
materialLogService.updateLog(failMaterial,Constants.Status.FAILED.getStatus());
|
|
|
|
return Result.ok(failMaterial);
|
|
|
|
}
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Admin
|
|
|
|
@PostMapping("/edit")
|
|
|
|
public Result editMaterial(@Valid @RequestBody Material material) {
|
|
|
|
if(!materialService.update(material)){
|
|
|
|
return Result.error(ResponseCode.ASSET_NO_EXISTED);
|
|
|
|
}
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Admin
|
|
|
|
@DeleteMapping("/delete/{id}")
|
|
|
|
public Result deleteMaterial(@PathVariable("id") Integer id) {
|
|
|
|
if(!materialService.delete(id)){
|
|
|
|
return Result.error(ResponseCode.ASSET_NO_EXISTED);
|
|
|
|
}
|
|
|
|
return Result.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/search")
|
|
|
|
public Result searchMaterial(String field,String content,Integer pageNum, Integer pageSize) {
|
|
|
|
return Result.ok(PageInfo.of(materialService.search(field,content,pageNum,pageSize)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/export")
|
|
|
|
public void exportMaterial(String field, String content, HttpServletResponse response) {
|
|
|
|
List<AmMaterial> list = materialService.search(field,content,null,null);
|
|
|
|
String time = DateUtil.dateToStr(new Date(),"yyyyMMddHHmmss");
|
|
|
|
String fileName = "物料_"+time+".xlsx";
|
|
|
|
ExcelUtils.buildExcelDocument(fileName,ExcelUtils.writeExcel(list,AmMaterial.class),response);
|
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("/export/fail")
|
|
|
|
public void exportFailMaterial(@RequestBody List<AmMaterial> list, HttpServletResponse response) {
|
|
|
|
String time = DateUtil.dateToStr(new Date(),"yyyyMMddHHmmss");
|
|
|
|
String fileName = "出库失败物料_"+time+".xlsx";
|
|
|
|
ExcelUtils.buildExcelDocument(fileName,ExcelUtils.writeExcel(list,AmMaterial.class),response);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/log")
|
|
|
|
public Result queryMaterialLog(@RequestParam Integer operate,@RequestParam Integer status,
|
|
|
|
@RequestParam(required = false) String field,
|
|
|
|
@RequestParam(required = false) String content,
|
|
|
|
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTm,
|
|
|
|
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTm,
|
|
|
|
Integer pageNum, Integer pageSize) {
|
|
|
|
return Result.ok(materialLogService.queryLog(operate,status,field,content,startTm,endTm,pageNum,pageSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
@GetMapping("/warn")
|
|
|
|
public Result queryMaterialWarn(Integer pageNum, Integer pageSize) {
|
|
|
|
return Result.ok(materialService.warn(pageNum,pageSize));
|
|
|
|
}
|
|
|
|
}
|