目录
1、模糊分页查询
2、批量删除
3、新增
4、编辑
此接口非彼接口。此接口是MVC的设计模式中的Controller层,一般我们会叫Controller层里的方法为接口。他们是负责接收前端或者其它服务的传来的请求,并对请求进行相应的处理,最终再将处理结果返回给前端或者其它服务。
1、模糊分页查询
1)Controller层
@RestController
@RequestMapping("camera")
public class CameraAlarmController {@Autowiredprivate IWarehouseOutService warehouseOutService; //注入接口/*** 获取设备相关信息* @param equipAttribute* @param equipType* @param keywords* @param pageNum* @param pageSize* @return*/@GetMapping("getOutboundEquipmentInfo")public Result getOutboundEquipmentInfo(@RequestParam(value ="operationType",required = false) Integer operationType,@RequestParam(value ="warehouseId",required = false) Integer warehouseId,@RequestParam(value ="equipAttribute",required = false) String equipAttribute,@RequestParam(value ="equipTypeId",required = false) Integer equipTypeId,@RequestParam(value = "keywords", required = false) String keywords,@RequestParam(value ="pageNum",required = false, defaultValue = "1") Integer pageNum,@RequestParam(value ="pageSize", required = false, defaultValue = "10") Integer pageSize){PageHelper.startPage(pageNum,pageSize);PageInfo<OutboundEquipmentInfoVo> pageInfo=new PageInfo<>(warehouseOutService.getOutboundEquipmentInfo(equipAttribute,equipTypeId,warehouseId,operationType,keywords));return new SuccessResult(pageInfo);}
}
在进行模糊查询操作的时候,往往有时候需要查询的参数很多,这时候也可以考虑使用实体类接收前端传参,更改如下:
@PostMapping("/getOutboundEquipmentInfo")//这里改成post请求public Result getOutboundEquipmentInfo(@RequestBody EquipInfoVo vo){PageHelper.startPage(vo.getPageNum(),vo.getPageSize());PageInfo<OutboundEquipmentInfoVo> pageInfo=new PageInfo<>(warehouseOutService.getOutboundEquipmentInfo(vo));return new SuccessResult(pageInfo);}
接收前端传参实体类代码如下:
@Data
@Accessors(chain = true)
public class EquipInfoVo {@JsonProperty(value = "operationType")private Integer operationType;@JsonProperty(value = "warehouseId")private Integer warehouseId;@JsonProperty(value = "equipAttribute")private String equipAttribute;@JsonProperty(value = "equipTypeId")private Integer equipTypeId;@JsonProperty(value = "keywords")private String keywords;@JsonProperty(value = "pageNum")private Integer pageNum=1;@JsonProperty(value = "pageSize")private Integer pageSize=10;
}
2)service层
定义service层接口(此接口就是我们Java中用intrerface关键字定义的接口了)
//这里不用加service注解
public interface IWarehouseOutService {List<OutboundEquipmentInfoVo> getOutboundEquipmentInfo(String equipAttribute,Integer equipTypeId,Integer warehouseId,Integer operationType,String keywords);}
定义service接口实现
@Service
@Slf4j
public class WarehouseOutServiceImpl implements IWarehouseOutService {@Autowiredprivate WmEquipInfoMapper wmEquipInfoMapper;@Overridepublic List<OutboundEquipmentInfoVo> getOutboundEquipmentInfo(String equipAttribute,Integer equipTypeId,Integer warehouseId,Integer operationType,String keywords) {//入库操作if (operationType==WmConstant.EQUIPMENT_INFO_INCOME){List<OutboundEquipmentInfoVo> outList=wmEquipInfoMapper.getEquipmentInformation(equipAttribute,equipTypeId,keywords);List<WmRepertoryInfoDto> allEquip = wmRepertoryInfoMapper.getAllEquip(warehouseId);outList.forEach(info->{Optional<WmRepertoryInfoDto> wmRepertoryInfoDto = allEquip.stream().filter(all -> all.getEquipId().equals(info.getId())).findFirst();if (wmRepertoryInfoDto.isPresent()){info.setInventoryQuantity(wmRepertoryInfoDto.get().getNum());info.setDamagesNum(wmRepertoryInfoDto.get().getDamagesNum());}else {info.setInventoryQuantity(0);info.setDamagesNum(0);}});return outList;}else{//出库操作List<OutboundEquipmentInfoVo> outboundEquipmentInfo = wmEquipInfoMapper.getOutboundEquipmentInfo(equipAttribute, warehouseId, equipTypeId, keywords);List<WmRelationEquipDto> relationEquipDtos=wmOutWarehouseMapper.getOutboundApproval(warehouseId);outboundEquipmentInfo.forEach(out-> relationEquipDtos.forEach(re->{if (Integer.valueOf(re.getEquipId().toString()).equals(out.getId())){Integer num=out.getInventoryQuantity()-re.getNum();out.setInventoryQuantity(num<0?0:num);}}));return outboundEquipmentInfo;}}
}
3)mapper层
//这上面不加@Mapper注解就要,在启动类上加这个注解@MapperScan("com.zcloud.dao.mapper")
public interface WmEquipInfoMapper {List<OutboundEquipmentInfoVo> getOutboundEquipmentInfo(@Param("equipAttribute") String equipAttribute,@Param("warehouseId") Integer warehouseId,@Param("equipTypeId") Integer equipTypeId,@Param("keywords") String keywords);
}
<select id="getOutboundEquipmentInfo" resultType="com.zcloud.domain.warehousemanage.vo.OutboundEquipmentInfoVo">SELECTt2.num inventoryQuantity,t2.damages_num,( SELECT `name` FROM t_dic_item WHERE type_code = "equip_attribute" AND CODE = t1.equip_attribute ) `equipAttributeName`,( SELECT `name` FROM t_dic_item WHERE type_code = "equip_unit" AND CODE = t1.equip_unit ) equipUnitName,t1.equip_name,t1.equip_brand,t1.id,t1.equip_model,t3.`name` equipTypeNameFROMt_common_wm_equip_info t1LEFT JOIN t_common_wm_repertory_info t2 ON t1.id = t2.equip_idLEFT JOIN t_common_wm_equip_type t3 ON t1.equip_type_id = t3.idWHEREt1.removed = 0AND t2.removed =0AND t3.removed=0AND t2.warehouse_id=#{warehouseId}<if test="equipAttribute!=null and equipAttribute!=''">AND t1.equip_attribute=#{equipAttribute}</if>//注意Interge类型的条件判断是否为空的时候一定不要加非空字符串判断,因为当你传的值为0的时候,mybatis会把它判断为空字符串<if test="equipTypeId!=null"> AND t1.equip_type_id=#{equipTypeId}</if><if test="keywords!=null and keywords!=''">AND (t1.equip_name like concat('%',#{keywords})OR t1.equip_model like concat('%',#{keywords})OR t1.equip_brand like concat('%',#{keywords}))</if>
</select>
2、批量删除
1) Controller层
@DeleteMapping(value = "/delete", name = "仓库管理,删除仓库信息")public Result deleteWareHouseInfo(@RequestParam(value = "ids") String ids,HttpServletRequest request) {Integer userId = (Integer) RequestUtils.getCurrentUser(request).get("userId");return new SuccessResult(wareHouseInfoService.deleteWareHouseInfo(ids,userId));}
2) service层
@Transactional //单表删除不加这个注解可以,多表关联删除一定要加
public Object deleteWareHouseInfo(Integer ids, Integer userId) {List<Integer> idList = Arrays.stream(ids.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());return warehouseInfoMapper.deleteByIds(idList,userId);}
3)mapper层
Integer deleteByIds(@Param("idList") List<Integer> idList,@Param("userId") Integer userId);
<update id="deleteByIds" parameterType="java.lang.Integer"><foreach collection="idList" item="item" separator=";">update t_warehouse_infoset removed = 1,rm_uid = #{userId},rm_time = NOW(),up_time = now()where id =#{item.id}</foreach></update>
3、新增
1) Controller层
@PostMapping(value = "/add", name = "仓库管理,新增采购")public Result addPurchaseInfo(@RequestBody WmPurchaseInfoAddVo dto, HttpServletRequest request) {Integer userId = (Integer) RequestUtils.getCurrentUser(request).get("userId");return new SuccessResult(wmPurchaseInfoService.addPurchaseInfo(dto,userId));}
2) service层
@Override@Transactionalpublic Integer addPurchaseInfo(WmPurchaseInfoAddVo dto, Integer userId) { Integer result;String orderCode = wmUtils.generateWmCode(WmConstant.PURCHASE_CODE_PREFIX, 1);dto.setOrderCode(orderCode).setCrUid(userId).setUpUid(userId);//插入采购基本信息,返回id//需要注意的是,如果待插入数据的表未设置主键自增,则在这里需要设置主键的值//而这里是设置了主键自增,所以就需要在mapper层配置sql语句的时候,设置返回自增的主键值purchaseInfoMapper.insertSelective(dto);List<WmRelationEquipDto> wmRelationEquipDtos = dto.getRelationEquip();//设置关联设备的,关联id,以及关联类型wmRelationEquipDtos.forEach(e -> {e.setRelationId(dto.getId()).setType(WmConstant.PURCHASE_RELATION).setStockPendingNum(e.getNum());});//插入关联设备信息result = relationEquipMapper.insertBatch(wmRelationEquipDtos);return result >= 1 ? 1 : 0;}
3) mapper层
单个新增
<insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" keyColumn="id"parameterType="vip.dtcloud.domain.warehousemanage.vo.WmPurchaseInfoAddVo">insert into t_common_wm_purchase_info<trim prefix="(" suffix=")" suffixOverrides=","><if test="addVo.orderCode != null">order_code,</if><if test="addVo.orderName != null">order_name,</if><if test="addVo.purchaseMethod != null">purchase_method,</if><if test="addVo.purchaseManager != null">purchase_manager,</if><if test="addVo.orderDate != null">order_date,</if><if test="addVo.contractId != null">contract_id,</if><if test="addVo.biddingDocumentIds != null">bidding_document_ids,</if><if test="addVo.purchaseContractIds != null">purchase_contract_ids,</if><if test="addVo.meetingSummaryIds != null">meeting_summary_ids,</if><if test="addVo.otherFilesIds != null">other_files_ids,</if><if test="addVo.remark != null">remark,</if>up_time,cr_time,<if test="addVo.upUid != null">up_uid,</if><if test="addVo.crUid != null">cr_uid,</if></trim><trim prefix="values (" suffix=")" suffixOverrides=","><if test="addVo.orderCode != null">#{addVo.orderCode,jdbcType=VARCHAR},</if><if test="addVo.orderName != null">#{addVo.orderName,jdbcType=VARCHAR},</if><if test="addVo.purchaseMethod != null">#{addVo.purchaseMethod,jdbcType=VARCHAR},</if><if test="addVo.purchaseManager != null">#{addVo.purchaseManager,jdbcType=VARCHAR},</if><if test="addVo.orderDate != null">#{addVo.orderDate,jdbcType=TIMESTAMP},</if><if test="addVo.contractId != null">#{addVo.contractId,jdbcType=INTEGER},</if><if test="addVo.biddingDocumentIds != null">#{addVo.biddingDocumentIds,jdbcType=VARCHAR},</if><if test="addVo.purchaseContractIds != null">#{addVo.purchaseContractIds,jdbcType=VARCHAR},</if><if test="addVo.meetingSummaryIds != null">#{addVo.meetingSummaryIds,jdbcType=VARCHAR},</if><if test="addVo.otherFilesIds != null">#{addVo.otherFilesIds,jdbcType=VARCHAR},</if><if test="addVo.remark != null">#{addVo.remark,jdbcType=VARCHAR},</if>now(),now(),<if test="addVo.upUid != null">#{addVo.upUid,jdbcType=INTEGER},</if><if test="addVo.crUid != null">#{addVo.crUid,jdbcType=INTEGER},</if></trim></insert>
批量新增
<insert id="insertBatch">insert into t_common_wm_relation_equip (relation_id, type,equip_id, equip_serial, equip_code,num, stock_pending_num, up_time)values<foreach collection="dtoList" item="item" separator=",">( #{item.relationId,jdbcType=INTEGER}, #{item.type,jdbcType=INTEGER},#{item.equipId,jdbcType=BIGINT}, #{item.equipSerial,jdbcType=VARCHAR}, #{item.equipCode,jdbcType=VARCHAR},#{item.num,jdbcType=INTEGER}, #{item.stockPendingNum,jdbcType=INTEGER}, now())</foreach></insert>
注意 : 做批量插入的时候,插入的数量不能太多,否则会因为sql语句过长而出现无法执行的问题。一般超过5000条的话就可以使用分页去插入了。比如,可以改成如下这样的插入:
List<List<EquipRealVariableDto>> lists = splitList(wmRelationEquipDtos, 5000);lists.forEach(e -> {result += relationEquipMapper.insertBatch(e);});
splitList方法代码如下:
private static <T> List<List<T>> splitList(List<T> list, int splitCount) {int length = list.size();long totalLength = (long)length + (long)splitCount - 1L;long num = totalLength / (long)splitCount;List<List<T>> newList = new ArrayList();for(int i = 0; (long)i < num; ++i) {int fromIndex = i * splitCount;int toIndex = (i + 1) * splitCount < length ? (i + 1) * splitCount : length;newList.add(list.subList(fromIndex, toIndex));}return newList;}
4、编辑
1) Controller层
@PutMapping(value = "/edit", name = "仓库管理,修改仓库信息")public Result editWareHouseInfo(@RequestBody WmWarehouseInfoDto dto,HttpServletRequest request) {Integer userId = (Integer) RequestUtils.getCurrentUser(request).get("userId");return new SuccessResult(wareHouseInfoService.editWareHouseInfo(dto,userId));}
2) service层
@Overridepublic Integer editWareHouseInfo(WmWarehouseInfoDto dto, Integer userId) {dto.setUpUid(userId);return warehouseInfoMapper.updateByPrimaryKeySelective(dto);}
3) mapper层
<update id="updateByPrimaryKeySelective" parameterType="vip.dtcloud.domain.warehousemanage.dto.WmWarehouseInfoDto">update t_common_wm_warehouse_info<set><if test="warehouseName != null">warehouse_name = #{warehouseName,jdbcType=VARCHAR},</if><if test="warehouseLocation != null">warehouse_location = #{warehouseLocation,jdbcType=VARCHAR},</if><if test="warehouseManager != null">warehouse_manager = #{warehouseManager,jdbcType=VARCHAR},</if><if test="phone != null">phone = #{phone,jdbcType=VARCHAR},</if><if test="departmentId != null">department_id = #{departmentId,jdbcType=VARCHAR},</if><if test="warehouseStatus != null">warehouse_status = #{warehouseStatus,jdbcType=INTEGER},</if>up_time = now(),<if test="upUid != null">up_uid = #{upUid,jdbcType=INTEGER},</if></set>where id = #{id,jdbcType=INTEGER}</update>