Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / HouseSiteServiceImpl.java
package com.newfiber.api.pc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.*;
import com.newfiber.api.pc.dao.HouseSiteMapper;
import com.newfiber.api.pc.dto.HouseSiteDto;
import com.newfiber.api.pc.model.meet.HouseSite;
import com.newfiber.api.pc.model.meet.HouseSiteSupplies;
import com.newfiber.api.pc.service.HouseSiteService;
import com.newfiber.api.pc.service.HouseSiteSuppliesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author:zzh
 * @CreateDate:2020/11/25 13:22
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class HouseSiteServiceImpl extends ServiceImpl<HouseSiteMapper,HouseSite> implements HouseSiteService {

    @Autowired
    private HouseSiteSuppliesService houseSiteSuppliesService;
    @Autowired
    private HouseSiteMapper houseSiteMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageResultObject<HouseSite> queryPage(PageRequestObject<String> pageRequestObject) {
        Page<HouseSite> objectPage = new Page<>(pageRequestObject.getCurrent(), pageRequestObject.getSize());
        EntityWrapper<HouseSite> wrapper = new EntityWrapper<>();
        if(!StringUtils.isEmpty(pageRequestObject.getObject())){
            wrapper.like("site_name",pageRequestObject.getObject());
        }
        Page<HouseSite> houseSitePage = this.selectPage(objectPage, wrapper);
        int count = this.selectCount(wrapper);
        return new PageResultObject<>(pageRequestObject.getCurrent(), pageRequestObject.getSize(),(long)count,houseSitePage.getRecords());
    }

    @Override
    public void addHouseSite(HouseSiteDto houseSiteDto,String userNo) {
        if(StringUtils.isEmpty(houseSiteDto)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        HouseSite houseSite = new HouseSite();
        houseSite.setUserNo(userNo);
        houseSite.setCreateTime(new Date());
        houseSite.setDescription(houseSiteDto.getDescription());
        houseSite.setCoordX(houseSiteDto.getCoordX());
        houseSite.setCoordY(houseSiteDto.getCoordY());
        houseSite.setSiteAddress(houseSiteDto.getSiteAddress());
        houseSite.setSiteName(houseSiteDto.getSiteName());
        houseSite.setType(houseSiteDto.getType());
        boolean insert = this.insert(houseSite);

        if(!insert){
            throw new CustomException(ResultCode.SAVE_ERROR);
        }
    }


    @Override
    public void updateHouseSite(HouseSiteDto houseSiteDto) {
        if(StringUtils.isEmpty(houseSiteDto)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        HouseSite houseSite = new HouseSite();
        houseSite.setHId(houseSiteDto.getHId());
        houseSite.setUpdateTime(new Date());
        houseSite.setDescription(houseSiteDto.getDescription());
        houseSite.setCoordX(houseSiteDto.getCoordX());
        houseSite.setCoordY(houseSiteDto.getCoordY());
        houseSite.setSiteAddress(houseSiteDto.getSiteAddress());
        houseSite.setSiteName(houseSiteDto.getSiteName());
        houseSite.setType(houseSiteDto.getType());
        boolean update = this.updateById(houseSite);
        if(!update){
            throw new CustomException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public void deletHouseSite(Integer hId) {
        List<HouseSiteSupplies> houseSiteSupplies = houseSiteSuppliesService.querySupByHouseSiteId(hId);
        if(houseSiteSupplies.size() > 0){
            throw new CustomException(777,"该站点下还有物资,请现将物资移至其他站点后删除!");
        }
        boolean delete = this.deleteById(hId);
        //删除Redis中对应的存放点数据,以免为空
        if(stringRedisTemplate.hasKey("supplies:" + hId)){
            stringRedisTemplate.delete("supplies:" + hId);
        }
        if(!delete){
            throw new CustomException(ResultCode.DELETE_ERROR);
        }
    }


    @Override
    public List<HouseSite> queryHouseSiteAndSup() {
        return houseSiteMapper.queryHouseSiteAndSup();
    }
}