package com.consum.base.controller; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import org.apache.commons.compress.utils.Lists; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.CollectionUtils; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import com.alibaba.excel.EasyExcelFactory; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelDataConvertException; import com.consum.base.BaseController; import com.consum.base.core.type.StatesType; import com.consum.base.core.utils.CommonUtil; import com.consum.base.core.utils.MapUtils; import com.consum.base.core.utils.MapperUtil; import com.consum.base.pojo.BaseGoodsTemplateParam; import com.consum.base.pojo.excel.ImportGoodsInfoTemplate; import com.consum.base.pojo.response.GoodsTemplateVO; import com.consum.base.service.BaseCategoryService; import com.consum.base.service.BaseGoodsTemplateService; import com.consum.base.service.BaseWarehouseService; import com.consum.base.service.FinSysTenantService; import com.consum.base.service.LWhGoodsService; import com.consum.model.po.BaseCategory; import com.consum.model.po.BaseGoodsModels; import com.consum.model.po.BaseGoodsTemplate; import com.consum.model.po.BaseWarehouse; import com.consum.model.po.FinSysTenant; import com.consum.model.po.FinSysTenantUser; import com.consum.model.po.SDictData; import com.consum.model.vo.BaseGoodsTemplateVo; import com.walker.db.page.GenericPager; import com.walker.infrastructure.utils.StringUtils; import com.walker.web.ResponseValue; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; /** * @Description 物品模板 * @Author 卢庆阳 * @Date 2023/10/24 */ @Api(value = "物品模板", tags = "物品模板") @RestController @RequestMapping("/pc/base/goods/template") public class BaseGoodsTemplateController extends BaseController { @Autowired private BaseGoodsTemplateService baseGoodsTemplateService; @Autowired private LWhGoodsService lWhGoodsService; @Autowired private BaseWarehouseService baseWarehouseService; @Autowired private FinSysTenantService finSysTenantService; @Autowired private BaseCategoryService baseCategoryService; /** * @Description 新增物品模板 * @Author 卢庆阳 * @Date 2023/10/24 */ @PostMapping("/add") public ResponseValue add() { BaseGoodsTemplateParam param = CommonUtil.getObjFromReqBody(BaseGoodsTemplateParam.class); BaseGoodsTemplateParam param2 = new BaseGoodsTemplateParam(); CommonUtil.copyProperties(param, param2); param = param2; FinSysTenantUser sysInfo = this.getSysInfo(); if (param.getCategoryId() == null) { return ResponseValue.error("登录用户信息不存在"); } if (param.getCategoryId() == null) { return ResponseValue.error("分类为空"); } if (StringUtils.isEmpty(param.getGoodsName())) { return ResponseValue.error("物品名称为空"); } // 判断同一分类下的物品名称是否重复 BaseGoodsTemplate goodsTemplate = this.baseGoodsTemplateService.getByGoodsNameAndCategoryId(param.getGoodsName(), param.getCategoryId()); if (goodsTemplate != null) { return ResponseValue.error("物品名称已存在"); } int result = this.baseGoodsTemplateService.add(param, sysInfo); if (result > 0) { return ResponseValue.success(1); } return ResponseValue.error("新增失败!"); } /** * @Description 物品模板列表查询 * @Author 卢庆阳 * @Date 2023/10/24 */ @GetMapping("/list") public ResponseValue queryList() { BaseGoodsTemplateParam param = CommonUtil.getObjFromReq(BaseGoodsTemplateParam.class); BaseGoodsTemplateParam param2 = new BaseGoodsTemplateParam(); CommonUtil.copyProperties(param, param2); param = param2; FinSysTenantUser sysInfo = this.getSysInfo(); if (sysInfo == null) { return ResponseValue.error("登录用户信息不存在"); } param.setAgencyId(Long.valueOf(sysInfo.getTenantId())); GenericPager pager = this.baseGoodsTemplateService.queryList(param); return ResponseValue.success(pager); } /** * 根据物品id查询物品详情 * * @author 卢庆阳 * @date 2023/9/26 */ @GetMapping("/detail") public ResponseValue getById(Long id) { if (id == null) { return ResponseValue.error("物品id为空"); } BaseGoodsTemplateVo vo = this.baseGoodsTemplateService.getById(id); if (vo == null) { return ResponseValue.error("查询失败!"); } return ResponseValue.success("查询成功!", vo); } /** * @Description 编辑 * @Author 卢庆阳 * @Date 2023/10/24 */ @PostMapping("/edit") public ResponseValue edit() { BaseGoodsTemplateParam param = CommonUtil.getObjFromReqBody(BaseGoodsTemplateParam.class); BaseGoodsTemplateParam param2 = new BaseGoodsTemplateParam(); CommonUtil.copyProperties(param, param2); param = param2; if (StringUtils.isEmpty(param.getGoodsName())) { return ResponseValue.error("物品名称为空"); } List models = param.getModels(); if (CollectionUtils.isEmpty(models)) { return ResponseValue.error("物品型号为空"); } int num = this.baseGoodsTemplateService.updateBaseGoodsTemplate(param); return num > 0 ? ResponseValue.success(1) : ResponseValue.error("编辑失败!"); } /** * 修改状态 * * @author 卢庆阳 * @date 2023/10/25 *

* 物品的禁用或删除,不影响已经采购入过库的物品信息。 *

*/ @PostMapping("/updStatus") public ResponseValue updateStatus() { BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class); BaseGoodsTemplate param2 = new BaseGoodsTemplate(); CommonUtil.copyProperties(goodsTemplate, param2); goodsTemplate = param2; if (goodsTemplate.getId() == null || goodsTemplate.getStates() == null) { return ResponseValue.error("参数错误"); } int num = this.baseGoodsTemplateService.updateStatus(goodsTemplate); return num > 0 ? ResponseValue.success(1) : ResponseValue.error("修改失败!"); } /** * @Description 根据物品id删除物品 * @Author 卢庆阳 * @Date 2023/10/25 *

* 物品的禁用或删除,不影响已经采购入过库的物品信息。 *

*/ @DeleteMapping("/del") public ResponseValue updateById() { BaseGoodsTemplate goodsTemplate = CommonUtil.getObjFromReqBody(BaseGoodsTemplate.class); BaseGoodsTemplate param2 = new BaseGoodsTemplate(); CommonUtil.copyProperties(goodsTemplate, param2); goodsTemplate = param2; if (goodsTemplate.getId() == null) { return ResponseValue.error("物品id为空"); } int num = this.baseGoodsTemplateService.updateById(goodsTemplate, this.getCurrentUser()); return num > 0 ? ResponseValue.success(1) : ResponseValue.error("删除失败!"); } /** * @Description 查询仓库类型(数据字典) * @Author 卢庆阳 * @Date 2023/10/30 */ @GetMapping("/select/classificationCode") public ResponseValue queryClassificationCode() { List list = this.baseGoodsTemplateService.queryClassificationCode(); return ResponseValue.success(list); } /** * @Description 根据分类id查询物品模板 * @Author 卢庆阳 * @Date 2023/10/30 */ @GetMapping("/selectByCategoryId") public ResponseValue queryByCategoryId(Long categoryId) { List list = this.baseGoodsTemplateService.queryByCategoryId(categoryId); return ResponseValue.success(list); } @GetMapping("/query/goodsTemplate") public ResponseValue queryGoodsTemplateByCategoryId(Long agencyId, Long categoryId) { // 不限制机构 List list = baseGoodsTemplateService.queryGoodsTemplateByCategoryId(null, categoryId, StatesType.NORMAL.getValue(), null); if (list == null) { return ResponseValue.error("查询失败!"); } return ResponseValue.success("查询成功!", list); } @ApiOperation(value = "调拨查询机构下所有仓库下的分类模板信息", notes = "调拨查询机构下所有仓库下的分类模板信息") @ApiImplicitParams({ @ApiImplicitParam(name = "agencyId", value = "父级机构id", required = true, dataType = "java.lang.Long", paramType = "query"), @ApiImplicitParam(name = "categoryId", value = "分类id", required = true, dataType = "Long", paramType = "query")}) @GetMapping("/query/warehouse/goods") public ResponseValue queryWarehouseGoods(Long agencyId, Long categoryId) { // 调拨查询所有仓库物品模板信息 Map map = new HashMap<>(); map.put("categoryId", categoryId); List baseWarehouseList = baseWarehouseService.getBaseWareHouseList(agencyId, StatesType.NORMAL.getValue()); if (CollectionUtils.isEmpty(baseWarehouseList)) { return ResponseValue.error("机构无仓库!"); } List warehouseIdList = baseWarehouseList.stream().map(BaseWarehouse::getId).collect(Collectors.toList()); map.put("warehouseIdList", warehouseIdList); String sql = "SELECT DISTINCT bgt.id,bgt.GOODS_NAME, CLASSIFICATION type FROM l_wh_goods g LEFT JOIN base_goods_template bgt ON g.BASE_GOODS_TEMPLATE_ID = bgt.id " + "WHERE WAREHOUSE_TYPE = 0 " + "AND WAREHOUSE_ID in (:warehouseIdList) " + "AND CATEGORY_ID = :categoryId " // 1:集采,2:自采 + "AND BUY_TYPE =1"; List resultList = Lists.newArrayList(); List> goodsTemplateList = lWhGoodsService.select(sql, map, new MapperUtil()); goodsTemplateList.forEach(item -> { GoodsTemplateVO goodsTemplateVO = MapUtils.convertMapToObj(item, GoodsTemplateVO.class); resultList.add(goodsTemplateVO); }); return ResponseValue.success("查询成功!", resultList); } @PostMapping("/import") public ResponseValue upload(MultipartFile file) throws IOException { String originalFilename = file.getOriginalFilename(); if (!".xls".endsWith(originalFilename)) { return ResponseValue.error("文件格式有误!"); } FinSysTenantUser sysInfo = this.getSysInfo(); if (sysInfo == null) { return ResponseValue.error("当前登录用户为空"); } EasyExcelFactory.read(file.getInputStream(), ImportGoodsInfoTemplate.class, new AnalysisEventListener() { List list = Lists.newArrayList(); @Override public void invoke(ImportGoodsInfoTemplate data, AnalysisContext analysisContext) { String categoryOne = data.getCategoryOne(); String categoryTwo = data.getCategoryTwo(); String categoryThree = data.getCategoryThree(); String goodsName = data.getGoodsName(); String goodModelName = data.getGoodModelName(); String unit = data.getUnit(); String type = data.getType(); String agencyName = data.getAgencyName(); BaseCategory baseCategory = baseCategoryService.getByCategoryByName(categoryThree); Long categoryId = baseCategory.getId(); Optional optional = list.stream().filter(item -> item.getCategoryId().equals(categoryId)).findFirst(); if (optional.isPresent()) { BaseGoodsModels baseGoodsModels = new BaseGoodsModels(); baseGoodsModels.setModelName(goodModelName); baseGoodsModels.setUnit(unit); optional.get().getModels().add(baseGoodsModels); } else { BaseGoodsTemplateParam baseGoodsTemplate = new BaseGoodsTemplateParam(); baseGoodsTemplate.setCategoryId(categoryId); baseGoodsTemplate.setGoodsName(goodsName); baseGoodsTemplate.setStates(1); FinSysTenant finSysTenant = finSysTenantService.selectByName(agencyName); baseGoodsTemplate.setAgencyId(finSysTenant.getId()); baseGoodsTemplate.setAgencyName(agencyName); List models = new ArrayList<>(); BaseGoodsModels baseGoodsModels = new BaseGoodsModels(); baseGoodsModels.setModelName(goodModelName); baseGoodsModels.setUnit(unit); models.add(baseGoodsModels); baseGoodsTemplate.setModels(models); list.add(baseGoodsTemplate); } } @Override public void doAfterAllAnalysed(AnalysisContext analysisContext) { for (BaseGoodsTemplateParam baseGoodsTemplate : list) { baseGoodsTemplateService.add(baseGoodsTemplate, null); } } @Override public void onException(Exception exception, AnalysisContext analysisContext) throws Exception { if (exception instanceof ExcelDataConvertException) { ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception; Integer row = excelDataConvertException.getRowIndex() + 1; Integer column = excelDataConvertException.getColumnIndex() + 1; throw new RuntimeException("第" + row + "行,第" + column + "列解析异常,请正确填写"); } else { throw new RuntimeException(exception.getMessage()); } } }).sheet(0).doRead(); return ResponseValue.success("导入成功!"); } }