java+postgresql+swagger-多表关联insert操作(六)

入参为json,然后根据需要对多张表进行操作:

入参格式:

{"custstoreName":"swagger-测试经销商01","customerName":"swagger-测试客户01","propertyNo":"swaggertest01","propertyName":"swagger-测试资产01","proName":"四川省","cityName":"攀枝花市","propertyType":"1","deviceNumber":"swaggerdevice01","fileName":"swagger-测试资产01附件01","fileUrl":"https://blog.youkuaiyun.com/huangsu_123/?type=lately"}

分别往四张表进行写数判断:

tb_custstoreinfo、tb_propertyinfo、tb_propertyfileinfo、tb_propertydeviceinfo

1、实体:

表实体:

package com.example.springbootmybatisplus.entity.property;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;


@ApiModel(description = "设备租赁_资产管理信息表")
@Data
@TableName(value = "tb_propertyinfo")
public class TbPropertyInfo implements Serializable {
    /**
     * 主键(pp)
     */
    @TableId(value = "id", type = IdType.INPUT)
    @ApiModelProperty(value = "主键(pp)")
    private String id;

    /**
     * 客户Id
     */
    @TableField(value = "custstoreid")
    @ApiModelProperty(value = "客户Id")
    private String custstoreId;

    /**
     * 承租人名字
     */
    @TableField(value = "lessee")
    @ApiModelProperty(value = "承租人名字")
    private String lessee;

    /**
     * 申请编号
     */
    @TableField(value = "appno")
    @ApiModelProperty(value = "申请编号")
    private String appNo;

    /**
     * 资产编号
     */
    @TableField(value = "propertyno")
    @ApiModelProperty(value = "资产编号")
    private String propertyNo;

    /**
     * 资产名称
     */
    @TableField(value = "propertyname")
    @ApiModelProperty(value = "资产名称")
    private String propertyName;

    /**
     * 资产所属省份code
     */
    @TableField(value = "procode")
    @ApiModelProperty(value = "资产所属省份code")
    private String proCode;

    /**
     * 资产所属省份
     */
    @TableField(value = "proname")
    @ApiModelProperty(value = "资产所属省份")
    private String proName;

    /**
     * 资产所属城市code
     */
    @TableField(value = "citycode")
    @ApiModelProperty(value = "资产所属城市code")
    private String cityCode;

    /**
     * 资产所属城市名称
     */
    @TableField(value = "cityname")
    @ApiModelProperty(value = "资产所属城市名称")
    private String cityName;

    /**
     * 资产品牌
     */
    @TableField(value = "propertybrand")
    @ApiModelProperty(value = "资产品牌")
    private String propertyBrand;

    /**
     * 资产类型
     */
    @TableField(value = "propertytype")
    @ApiModelProperty(value = "资产类型(1、光学机械;2、包装机械;3、木工机械;4、印染机械;5、纺织机械;6、印刷机械;7、金属切削;8、塑胶成型;9、辅助设备,10、工程机械)")
    private Short propertyType;

    /**
     * 铭牌型号
     */
    @TableField(value = "nameplatemodel")
    @ApiModelProperty(value = "铭牌型号")
    private String namePlateModel;

    /**
     * 备注json
     */
    @TableField(value = "jsonstring")
    @ApiModelProperty(value = "备注json")
    private String jsonString;

    /**
     * 创建时间(录入时间)
     */
    @TableField(value = "createdat")
    @ApiModelProperty(value = "创建时间(录入时间)")
    private Date createdAt;

    /**
     * 创建操作人
     */
    @TableField(value = "createdbyid")
    @ApiModelProperty(value = "创建操作人")
    private String createdById;

    /**
     * 更新时间
     */
    @TableField(value = "updatedat")
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 更新操作人
     */
    @TableField(value = "updatedbyid")
    @ApiModelProperty(value = "更新操作人")
    private String updatedById;

    /**
     * 删除时间
     */
    @TableField(value = "deletedat")
    @ApiModelProperty(value = "删除时间")
    private Date deletedAt;

    /**
     * 删除操作人
     */
    @TableField(value = "deletedbyid")
    @ApiModelProperty(value = "删除操作人")
    private String deletedById;

    /**
     * 是否删除(f否t是)
     */
    @TableField(value = "deleted")
    @ApiModelProperty(value = "是否删除(f否t是)")
    private Boolean deleted;

    /**
     * 承租人id(tb_propertylesseeinfo.id)
     */
    @TableField(value = "lesseeid")
    @ApiModelProperty(value = "承租人id(tb_propertylesseeinfo.id)")
    private String lesseeId;

    /**
     * 合同编号
     */
    @TableField(value = "contractno")
    @ApiModelProperty(value = "合同编号")
    private String contractNo;

    /**
     * 设备唯一标识
     */
    @TableField(value = "uniquedeviceidentifier")
    @ApiModelProperty(value = "设备唯一标识")
    private String uniqueDeviceIdentifier;

    /**
     * 资产型号
     */
    @TableField(value = "propertymold")
    @ApiModelProperty(value = "资产型号")
    private String propertyMold;

    /**
     * 门店id
     */
    @TableField(value = "dealerid")
    @ApiModelProperty(value = "门店id")
    private String dealerId;

    /**
     * 安装录入人
     */
    @TableField(value = "installinputperson")
    @ApiModelProperty(value = "安装录入人")
    private String installInputPerson;

    /**
     * 统一社会信用代码(冗余字段方便查询,承租人信息)
     */
    @TableField(value = "socialcreditcode")
    @ApiModelProperty(value = "统一社会信用代码(冗余字段方便查询,承租人信息)")
    private String socialCreditCode;

    /**
     * 修改人
     */
    @TableField(value = "updatebyname")
    @ApiModelProperty(value = "修改人")
    private String updateByName;

    /**
     * 虚拟设备号
     */
    @TableField(value = "virtualdeviceno")
    @ApiModelProperty(value = "虚拟设备号")
    private String virtualDeviceNo;

    /**
     * 监控状态(1正常,2重点关注,3已处置)
     */
    @TableField(value = "monitorstatus")
    @ApiModelProperty(value = "监控状态(1正常,2重点关注,3已处置)")
    private Short monitorStatus;
}
package com.example.springbootmybatisplus.entity.property;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

@ApiModel(description = "设备租赁_资产附件信息表")
@Data
@TableName(value = "tb_propertyfileinfo")
public class TbPropertyFileInfo {
    /**
     * 主键自增
     */
    @TableId(value = "id", type = IdType.AUTO)
    @ApiModelProperty(value = "主键自增")
    private Long id;

    /**
     * 资产id(tb_propertyinfo.id)
     */
    @TableField(value = "propertyid")
    @ApiModelProperty(value = "资产id(tb_propertyinfo.id)")
    private String propertyId;

    /**
     * 文件名
     */
    @TableField(value = "filename")
    @ApiModelProperty(value = "文件名")
    private String fileName;

    /**
     * 文件路径
     */
    @TableField(value = "fileurl")
    @ApiModelProperty(value = "文件路径")
    private String fileUrl;

    /**
     * 文件备注
     */
    @TableField(value = "fileremark")
    @ApiModelProperty(value = "文件备注")
    private String fileRemark;

    /**
     * 文件类型
     */
    @TableField(value = "filetype")
    @ApiModelProperty(value = "文件类型")
    private String fileType;

    /**
     * 备注json
     */
    @TableField(value = "jsonstring")
    @ApiModelProperty(value = "备注json")
    private String jsonString;

    /**
     * 创建时间
     */
    @TableField(value = "createdat")
    @ApiModelProperty(value = "创建时间")
    private Date createdAt;

    /**
     * 创建操作人
     */
    @TableField(value = "createdbyid")
    @ApiModelProperty(value = "创建操作人")
    private String createdById;

    /**
     * 更新时间
     */
    @TableField(value = "updatedat")
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 更新操作人
     */
    @TableField(value = "updatedbyid")
    @ApiModelProperty(value = "更新操作人")
    private String updatedById;

    /**
     * 删除时间
     */
    @TableField(value = "deletedat")
    @ApiModelProperty(value = "删除时间")
    private Date deletedAt;

    /**
     * 删除操作人
     */
    @TableField(value = "deletedbyid")
    @ApiModelProperty(value = "删除操作人")
    private String deletedById;

    /**
     * 是否删除(f否t是)
     */
    @TableField(value = "deleted")
    @ApiModelProperty(value = "是否删除(f否t是)")
    private Boolean deleted;
}
package com.example.springbootmybatisplus.entity.property;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

@ApiModel(description = "设备租赁_设备信息表")
@Data
@TableName(value = "tb_propertydeviceinfo")
public class TbPropertyDeviceInfo implements Serializable {
    /**
     * 主键(pd)
     */
    @TableId(value = "id", type = IdType.INPUT)
    @ApiModelProperty(value = "主键(pd)")
    private String id;

    /**
     * 资产id(tb_propertyinfo.id)
     */
    @TableField(value = "propertyid")
    @ApiModelProperty(value = "资产id(tb_propertyinfo.id)")
    private String propertyId;

    /**
     * 设备号
     */
    @TableField(value = "devicenumber")
    @ApiModelProperty(value = "设备号")
    private String deviceNumber;

    /**
     * 设备状态:1在线、0离线、2未启用
     */
    @TableField(value = "devicestate")
    @ApiModelProperty(value = "设备状态:1在线、0离线、2未启用")
    private Short deviceState;

    /**
     * 设备电量
     */
    @TableField(value = "batterypct")
    @ApiModelProperty(value = "设备电量")
    private BigDecimal batterYpct;

    /**
     * 定位纬度
     */
    @TableField(value = "devicelat")
    @ApiModelProperty(value = "定位纬度")
    private BigDecimal deviceLat;

    /**
     * 定位经度
     */
    @TableField(value = "devicelng")
    @ApiModelProperty(value = "定位经度")
    private BigDecimal deviceLng;

    /**
     * 最后定位时间
     */
    @TableField(value = "locationtime")
    @ApiModelProperty(value = "最后定位时间")
    private Date locationTime;

    /**
     * 最后检测时间
     */
    @TableField(value = "lasttime")
    @ApiModelProperty(value = "最后检测时间")
    private Date lastTime;

    /**
     * 绑定时间
     */
    @TableField(value = "bindingtime")
    @ApiModelProperty(value = "绑定时间")
    private Date bindingTime;

    /**
     * 最新报警时间
     */
    @TableField(value = "alarmtime")
    @ApiModelProperty(value = "最新报警时间")
    private Date alarmTime;

    /**
     * 设备状态更新时间
     */
    @TableField(value = "stateupdatedat")
    @ApiModelProperty(value = "设备状态更新时间")
    private Date stateUpdatedAt;

    /**
     * 备注json
     */
    @TableField(value = "jsonstring")
    @ApiModelProperty(value = "备注json")
    private String jsonString;

    /**
     * 创建时间
     */
    @TableField(value = "createdat")
    @ApiModelProperty(value = "创建时间")
    private Date createdAt;

    /**
     * 创建操作人
     */
    @TableField(value = "createdbyid")
    @ApiModelProperty(value = "创建操作人")
    private String createdById;

    /**
     * 更新时间
     */
    @TableField(value = "updatedat")
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 更新操作人
     */
    @TableField(value = "updatedbyid")
    @ApiModelProperty(value = "更新操作人")
    private String updatedById;

    /**
     * 删除时间
     */
    @TableField(value = "deletedat")
    @ApiModelProperty(value = "删除时间")
    private Date deletedAt;

    /**
     * 删除操作人
     */
    @TableField(value = "deletedbyid")
    @ApiModelProperty(value = "删除操作人")
    private String deletedById;

    /**
     * 是否删除(f否t是)
     */
    @TableField(value = "deleted")
    @ApiModelProperty(value = "是否删除(f否t是)")
    private Boolean deleted;

    /**
     * 最新报警名称
     */
    @TableField(value = "alarmname")
    @ApiModelProperty(value = "最新报警名称")
    private String alarmName;

    /**
     * 定位方式
     */
    @TableField(value = "locationtype")
    @ApiModelProperty(value = "定位方式")
    private String locationType;

    /**
     * 设备类型:0有线设备;1 无线设备,2全部,3设备手环
     */
    @TableField(value = "iswireless")
    @ApiModelProperty(value = "设备类型:0有线设备;1 无线设备,2全部,3设备手环")
    private Short isWireless;

    /**
     * 设备型号(产品型号)
     */
    @TableField(value = "devicemodel")
    @ApiModelProperty(value = "设备型号(产品型号)")
    private String deviceModel;

    /**
     * 首次定位点和报警定位点二者之间差值(m)
     */
    @TableField(value = "distance")
    @ApiModelProperty(value = "首次定位点和报警定位点二者之间差值(m)")
    private BigDecimal distance;
}
package com.example.springbootmybatisplus.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName(value = "tb_district")
@ApiModel(value = "省市区经纬度")
public class TbDistrict implements Serializable {
    /**
     * 主键(自增)
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 省份code
     */
    @TableField(value = "procode")
    @ApiModelProperty(value = "资产编号")
    private String proCode;

    /**
     * 省份名称
     */
    @TableField(value = "proname")
    @ApiModelProperty(value = "省份名称")
    private String proName;

    /**
     * 城市code
     */
    @TableField(value = "citycode")
    @ApiModelProperty(value = "城市code")
    private String cityCode;

    /**
     * 城市名称
     */
    @TableField(value = "cityname")
    @ApiModelProperty(value = "城市名称")
    private String cityName;

    /**
     * 原始纬度
     */
    @TableField(value = "lat")
    @ApiModelProperty(value = "原始纬度")
    private String lat;

    /**
     * 原始经度
     */
    @TableField(value = "lng")
    @ApiModelProperty(value = "原始经度")
    private String lng;

    /**
     * 备注json
     */
    @TableField(value = "jsonstring")
    @ApiModelProperty(value = "备注json")
    private String jsonString;

    /**
     * 创建时间(录入时间)
     */
    @TableField(value = "createdat")
    @ApiModelProperty(value = "创建时间(录入时间)")
    private Date createdAt;

    /**
     * 创建操作人
     */
    @TableField(value = "createdbyid")
    @ApiModelProperty(value = "创建操作人")
    private String createdById;

    /**
     * 更新时间
     */
    @TableField(value = "updatedat")
    @ApiModelProperty(value = "更新时间")
    private Date updatedAt;

    /**
     * 更新操作人
     */
    @TableField(value = "updatedbyid")
    @ApiModelProperty(value = "更新操作人")
    private String updatedById;

    /**
     * 删除时间
     */
    @TableField(value = "deletedat")
    @ApiModelProperty(value = "删除时间")
    private Date deletedAt;

    /**
     * 删除操作人
     */
    @TableField(value = "deletedbyid")
    @ApiModelProperty(value = "删除操作人")
    private String deletedById;

    /**
     * 是否删除(f否t是)
     */
    @TableField(value = "deleted")
    @ApiModelProperty(value = "是否删除(f否t是)")
    private Boolean deleted;
}
package com.example.springbootmybatisplus.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName(value = "tb_custstoreinfo")
public class TbCustStoreInfo implements Serializable {
    /**
     * 主键(DL)
     */
    @TableId(value = "id", type = IdType.INPUT)
    private String id;

    /**
     * 客户Id
     */
    @TableField(value = "customerid")
    private String customerId;

    /**
     * 上级经销商Id
     */
    @TableField(value = "parentId")
    private String parentId;

    /**
     * 经销商编号
     */
    @TableField(value = "code")
    private String code;

    /**
     * 经销商名称
     */
    @TableField(value = "\"name\"")
    private String name;

    /**
     * 经销商等级
     */
    @TableField(value = "levelCode")
    private short levelCode;

    /**
     * 城市code
     */
    @TableField(value = "cityCode")
    private String cityCode;

    /**
     * 城市名称
     */
    @TableField(value = "cityName")
    private String cityName;

    /**
     * 省份Code
     */
    @TableField(value = "proCode")
    private String proCode;

    /**
     * 省份名称
     */
    @TableField(value = "proName")
    private String proName;

    /**
     * 备注json
     */
    @TableField(value = "jsonString")
    private Object jsonString;

    /**
     * 创建时间
     */
    @TableField(value = "createdAt")
    private Date createdAt;

    /**
     * 创建操作人
     */
    @TableField(value = "createdById")
    private String createdById;

    /**
     * 更新时间
     */
    @TableField(value = "updatedAt")
    private Date updatedAt;

    /**
     * 更新操作人
     */
    @TableField(value = "updatedById")
    private String updatedById;

    /**
     * 删除时间
     */
    @TableField(value = "deletedAt")
    private Date deletedAt;

    /**
     * 删除操作人
     */
    @TableField(value = "deletedById")
    private String deletedById;

    /**
     * 是否删除(f否t是)
     */
    @TableField(value = "deleted")
    private Boolean deleted;

    /**
     * 客户经销商id
     */
    @TableField(value = "customerDealerId")
    private String customerdealerid;
    /**
     * 类型(1客户,2经销商)
     */
    @TableField(value = "type")
    private int type;
    /**
     * 机构结算单位类型:0 非结算单位;1 结算单位
     */
    @TableField(value = "settletype")
    private int settleType;

    /**
     * 曾经是否有结算属性:0 无; 1 有
     */
    @TableField(value = "isoncesettle")
    private int isOncesettle;
    /**
     * Key编码
     */
    @TableField(value = "keycode")
    private String keyCode;
    /**
     * 融资经理
     */
    @TableField(value = "financemanager")
    private String financeManager;

    /**
     * 新增时间
     */
    @TableField(value = "inserttime")
    private Date insertTime;

    /**
     * 部门Id
     */
    @TableField(value = "departmentid")
    private String departmentId;

    /**
     * 供应商Id
     */
    @TableField(value = "supplierid")
    private String supplierId;

    /**
     * 经销商来源
     */
    @TableField(value = "incuststore")
    private String inCuststore;
    /**
     * 经销商具体地址
     */
    @TableField(value = "custstoreAddress")
    private String custstoreAddress;
}

入参接收实体:

package com.example.springbootmybatisplus.dto;

import lombok.Data;

@Data
public class PropertyDto {
    private String custstoreName;
    private String customerName;
    private String propertyNo;
    private String propertyName;
    private String proName;
    private String cityName;
    private Short propertyType;
    private String deviceNumber;
    private String fileName;
    private String fileUrl;
}

其他实体:

package com.example.springbootmybatisplus.dto;

import lombok.Data;

@Data
public class DistrictDto {
    private String proCode;
    private String proName;
    private String cityCode;
    private String cityName;
    private String lat;
    private String lng;
}
package com.example.springbootmybatisplus.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

import java.io.Serializable;
@Data
public class TbCustStore implements Serializable {
    /**
     * 主键(DL)
     */
    @TableId(value = "id", type = IdType.INPUT)
    private String id;

    /**
     * 客户Id
     */
    @TableField(value = "customerid")
    private String customerId;

    /**
     * 上级经销商Id
     */
    @TableField(value = "parentId")
    private String parentId;

    /**
     * 经销商编号
     */
    @TableField(value = "code")
    private String code;

    /**
     * 经销商名称
     */
    @TableField(value = "\"name\"")
    private String name;

    /**
     * 经销商等级
     */
    @TableField(value = "levelCode")
    private Short levelCode;
}

2、Mapper:

package com.example.springbootmybatisplus.mapper.property;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.entity.property.TbPropertyInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TbPropertyInfoMapper extends BaseMapper<TbPropertyInfo> {
    @Select("select Id as propertyid from tb_propertyinfo where propertyno = #{propertyNo} and deleted=false ")
    String getPropertyId(@Param("propertyNo") String propertyNo);

    @Select("select 'PP'||nextval('pp')")
    String getPropertyInfoId();
}
package com.example.springbootmybatisplus.mapper.property;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.entity.property.TbPropertyFileInfo;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TbPropertyFileInfoMapper extends BaseMapper<TbPropertyFileInfo> {
}
package com.example.springbootmybatisplus.mapper.property;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.entity.property.TbPropertyDeviceInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TbPropertyDeviceInfoMapper extends BaseMapper<TbPropertyDeviceInfo> {
    @Select("select 'PD'||nextval('pd')")
    String getPropertyDeviceId();

    @Select("select * from tb_propertydeviceinfo where deleted =false and devicenumber =#{inDeviceNumber} limit 1")
    TbPropertyDeviceInfo getPropertyDevice(@Param("inDeviceNumber") String inDeviceNumber);
}
package com.example.springbootmybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.dto.DistrictDto;
import com.example.springbootmybatisplus.entity.TbDistrict;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface TbDistrictMapper extends BaseMapper<TbDistrict> {
    DistrictDto selectProCodeAndCityCode(@Param("proName") String proName,@Param("cityName") String cityName);
}
package com.example.springbootmybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.entity.TbCustStore;
import com.example.springbootmybatisplus.entity.TbCustStoreInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TbCustStoreInfoMapper extends BaseMapper<TbCustStoreInfo> {
    @Select("select id,customerid ,parentid ,code ,\"name\" ,levelcode  from tb_custstoreinfo where deleted =false and code like #{inCode}; ")
    List<TbCustStoreInfo> selectTbCustStoreInfo1(@Param("inCode") String inCode);

    @Select("select id,customerid ,parentid ,code ,name ,levelcode from tb_custstoreinfo where deleted =false and code like #{inCode}; ")
    List<TbCustStoreInfo> selectTbCustStoreInfo2(@Param("inCode") String inCode);

    @Select("select id,customerid ,parentid ,code ,name ,levelcode from tb_custstoreinfo where deleted =false and name=#{inCuststoreName} order by levelcode asc limit 1")
    TbCustStore selectTbCustStoreInfo3(@Param("inCuststoreName") String inCuststoreName);

    @Select("select fn_getcuststorecode(#{inParentId})")
    String getCustStoreCode(@Param("inParentId") String inParentId);
}
package com.example.springbootmybatisplus.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.entity.TbCustStore;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface TbCustStoreMapper extends BaseMapper<TbCustStore> {
}
package com.example.springbootmybatisplus.mapper.property;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootmybatisplus.dto.PropertyDto;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface PropertyMapper extends BaseMapper<PropertyDto> {
}

3、Service:

package com.example.springbootmybatisplus.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springbootmybatisplus.dto.DistrictDto;
import com.example.springbootmybatisplus.dto.PropertyDto;
import com.example.springbootmybatisplus.entity.TbCustStore;
import com.example.springbootmybatisplus.entity.TbCustStoreInfo;
import com.example.springbootmybatisplus.entity.property.TbPropertyDeviceInfo;
import com.example.springbootmybatisplus.entity.property.TbPropertyFileInfo;
import com.example.springbootmybatisplus.entity.property.TbPropertyInfo;
import com.example.springbootmybatisplus.mapper.TbCustStoreInfoMapper;
import com.example.springbootmybatisplus.mapper.TbDistrictMapper;
import com.example.springbootmybatisplus.mapper.property.TbPropertyDeviceInfoMapper;
import com.example.springbootmybatisplus.mapper.property.TbPropertyFileInfoMapper;
import com.example.springbootmybatisplus.mapper.property.TbPropertyInfoMapper;
import com.example.springbootmybatisplus.mapper.property.ViPropertyDeviceMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;
import java.util.UUID;

@Service
public class TbPropertyService {
    private static final Logger log = LoggerFactory.getLogger(TbDevicestatusService.class);

    @Autowired
    private TbPropertyInfoMapper tbPropertyInfoMapper;

    @Autowired
    private TbPropertyFileInfoMapper tbPropertyFileInfoMapper;

    @Autowired
    private TbPropertyDeviceInfoMapper tbPropertyDeviceInfoMapper;

    @Autowired
    private ViPropertyDeviceMapper viPropertyDeviceMapper;

    @Autowired
    private TbDistrictMapper tbDistrictMapper;

    @Autowired
    private TbCustStoreInfoMapper tbCustStoreInfoMapper;

    public void addProperty(String msg) {
        try {
            PropertyDto propertyDto = JSON.parseObject(msg, PropertyDto.class);
            if (Objects.isNull(propertyDto)) {
                log.info("资产信息为空");
                return;
            }

            TbCustStore existCustomerId = tbCustStoreInfoMapper.selectTbCustStoreInfo3(propertyDto.getCustomerName());
            if (Objects.isNull(existCustomerId)) {
                log.info("该客户在库中不存在,需要新建");
                TbCustStoreInfo tbCustStoreInfo = insertTbCustomerInfo(propertyDto);
                tbCustStoreInfoMapper.insert(tbCustStoreInfo);
            } else {
                log.info("该客户:{}在库中已存在", propertyDto.getCustomerName());
            }

            TbCustStore existCustStoreId = tbCustStoreInfoMapper.selectTbCustStoreInfo3(propertyDto.getCuststoreName());
            if (Objects.isNull(existCustStoreId)) {
                log.info("该经销商在库中不存在,需要新建");
                TbCustStoreInfo tbCustStoreInfo = insertTbCustStoreInfo(propertyDto);
                tbCustStoreInfoMapper.insert(tbCustStoreInfo);
            } else {
                log.info("该经销商:{}在库中已存在", propertyDto.getCuststoreName());
            }


            String existProperty = tbPropertyInfoMapper.getPropertyId(propertyDto.getPropertyNo());
            if (Objects.isNull(existProperty)) {
                log.info("该资产编号未存在系统中,新建资产:{}", propertyDto.getPropertyName());
                TbPropertyInfo tbPropertyInfo = insertTbpropertyInfo(propertyDto);
                tbPropertyInfoMapper.insert(tbPropertyInfo);

                DistrictDto districtDto = updateTbpropertyInfoProCodeAndCityCOde(propertyDto);
                if (Objects.isNull(districtDto)) {
                    log.info("该资产对应的省市:{},{}没找到对应的code", propertyDto.getProName(), propertyDto.getCityName());
                } else {
                    tbPropertyInfo.setProCode(districtDto.getProCode());
                    tbPropertyInfo.setCityCode(districtDto.getCityCode());
                    tbPropertyInfoMapper.updateById(tbPropertyInfo);
                    log.info("该资产对应的省市code:{},{}", districtDto.getProCode(), districtDto.getCityCode());
                }
            } else {
                log.info("该资产:{}在库中已存在", propertyDto.getPropertyName());
            }
            TbPropertyInfo tbPropertyInfo = insertTbpropertyInfo(propertyDto);
            String propertyId = tbPropertyInfo.getId();

            String fileName = propertyDto.getFileName();
            String fileUrl = propertyDto.getFileUrl();
            if (fileName == null && fileUrl == null) {
                log.info("该资产无附件信息");
            } else {
                TbPropertyFileInfo tbPropertyFileInfo = new TbPropertyFileInfo();
                tbPropertyFileInfo.setPropertyId(propertyId);
                tbPropertyFileInfo.setFileName(propertyDto.getFileName());
                tbPropertyFileInfo.setFileUrl(propertyDto.getFileUrl());
                tbPropertyFileInfoMapper.insert(tbPropertyFileInfo);
                log.info("该资产附件信息已写入");
            }
            String deviceNumber = propertyDto.getDeviceNumber();
            if (!deviceNumber.trim().isEmpty()) {
                TbPropertyDeviceInfo existTbPropertyDeviceInfo = tbPropertyDeviceInfoMapper.getPropertyDevice(propertyDto.getDeviceNumber());
                if (Objects.isNull(existTbPropertyDeviceInfo)) {
                    log.info("该资产设备未存在系统中,新建资产设备:{}", propertyDto.getDeviceNumber());
                    String propertyDeviceId = tbPropertyDeviceInfoMapper.getPropertyDeviceId();
                    if (propertyDeviceId == null) {
                        log.warn("获得到的设备id为空,无法写入资产设备信息");
                        return;
                    }
                    TbPropertyDeviceInfo tbPropertyDeviceInfo = new TbPropertyDeviceInfo();
                    tbPropertyDeviceInfo.setId(propertyDeviceId);
                    tbPropertyDeviceInfo.setDeviceNumber(deviceNumber);
                    tbPropertyDeviceInfo.setPropertyId(propertyId);
                    boolean saveSuccess = insertTbPropertyDeviceInfo(tbPropertyDeviceInfo);
                    if (saveSuccess) {
                        log.info("该资产设备信息已写入,设备编号:{}", deviceNumber);
                    }
                } else {
                    log.info("该资产设备已存在系统中");
                }
            } else {
                log.info("该资产无设备信息,deviceNumber为空或仅包含空白字符");
            }
        } catch (Exception e) {
            log.error("异常信息:" + e.getMessage());
        }
    }

    public TbCustStoreInfo insertTbCustomerInfo(PropertyDto propertyDto) {
        TbCustStoreInfo bb = new TbCustStoreInfo();
        String inId = UUID.randomUUID().toString();
        bb.setId(inId);
        bb.setCustomerId(inId);
        bb.setParentId("DL9999999997");
        bb.setCustomerdealerid(inId);
        bb.setCode(tbCustStoreInfoMapper.getCustStoreCode("DL9999999997"));
        bb.setLevelCode(Short.parseShort("2"));
        bb.setName(propertyDto.getCustomerName());
        bb.setType(2);
        bb.setInsertTime(new Date());
        return bb;
    }

    public TbCustStoreInfo insertTbCustStoreInfo(PropertyDto propertyDto) {
        TbCustStoreInfo cc = new TbCustStoreInfo();
        TbCustStore tbCustStore = tbCustStoreInfoMapper.selectTbCustStoreInfo3(propertyDto.getCustomerName());
        cc.setId(UUID.randomUUID().toString());
        cc.setCustomerId((tbCustStore.getCustomerId()));
        cc.setParentId(tbCustStore.getId());
        cc.setCustomerdealerid(tbCustStore.getId());
        cc.setCode(tbCustStoreInfoMapper.getCustStoreCode(tbCustStore.getId()));
        cc.setLevelCode((short) (tbCustStore.getLevelCode() + 1));
        cc.setName(propertyDto.getCuststoreName());
        cc.setType(2);
        cc.setInsertTime(new Date());
        return cc;
    }

    public TbPropertyInfo insertTbpropertyInfo(PropertyDto propertyDto) {
        TbPropertyInfo aa = new TbPropertyInfo();
        TbCustStore tbCustStore = new TbCustStore();
        tbCustStore = tbCustStoreInfoMapper.selectTbCustStoreInfo3(propertyDto.getCustomerName());
        String custstoreId = tbCustStore.getId();
        aa.setCuststoreId(custstoreId);
        tbCustStore = tbCustStoreInfoMapper.selectTbCustStoreInfo3(propertyDto.getCuststoreName());
        String dealerId = tbCustStore.getId();
        aa.setDealerId(dealerId);
        aa.setId(tbPropertyInfoMapper.getPropertyInfoId());
        aa.setPropertyNo(propertyDto.getPropertyNo());
        aa.setPropertyName(propertyDto.getPropertyName());
        aa.setProName(propertyDto.getProName());
        aa.setCityName(propertyDto.getCityName());
        aa.setPropertyType(propertyDto.getPropertyType());
        aa.setJsonString(JSONObject.toJSONString(propertyDto));
        return aa;
    }

    public DistrictDto updateTbpropertyInfoProCodeAndCityCOde(PropertyDto propertyDto) {
        return tbDistrictMapper.selectProCodeAndCityCode(propertyDto.getProName(), propertyDto.getCityName());
    }

    public boolean insertTbPropertyDeviceInfo(TbPropertyDeviceInfo tbPropertyDeviceInfo) {
        return tbPropertyDeviceInfoMapper.insert(tbPropertyDeviceInfo) > 0;
    }

}

4、xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springbootmybatisplus.mapper.TbDistrictMapper">
    <select id="selectProCodeAndCityCode"
            resultType="com.example.springbootmybatisplus.dto.DistrictDto">
        select procode,proname, citycode,cityname,lat,lng from tb_district WHERE proname = #{proName,jdbcType=VARCHAR}
        AND cityname = #{cityName,jdbcType=VARCHAR} limit 1
    </select>
</mapper>

5、Controller:

package com.example.springbootmybatisplus.contronller;

import com.example.springbootmybatisplus.service.TbPropertyService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/tb-property")
@Api(tags = "资产管理")
public class TbPropertyController {
    @Autowired
    private TbPropertyService tbPropertyService;

    @PostMapping("/insert1")
    public ResponseEntity<String> insert1(@RequestBody String msg) {
        try {
            tbPropertyService.addProperty(msg);
            return ResponseEntity.status(HttpStatus.CREATED).body("Success");
        }catch (Exception e){
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed");
        }
    }
}

6、调用接口打印的日志(调用两次):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值