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


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.base.domain.Accessory;
import com.newfiber.api.core.base.domain.BaseEntity;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.core.commons.PageResultObject;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.pc.dao.WaterloggingPointMapper;
import com.newfiber.api.pc.model.entity.WaterloggingPoint;
import com.newfiber.api.pc.model.entity.WaterloggingPoint;
import com.newfiber.api.pc.model.entity.WaterloggingPoint;
import com.newfiber.api.pc.model.request.waterloggingPoint.WaterloggingPointPageQueryRequest;
import com.newfiber.api.pc.model.request.waterloggingPoint.WaterloggingPointQueryRequest;
import com.newfiber.api.pc.model.request.waterloggingPoint.WaterloggingPointSaveRequest;
import com.newfiber.api.pc.model.request.waterloggingPoint.WaterloggingPointUpdateRequest;
import com.newfiber.api.pc.service.IWaterloggingPointService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

/**
 * 防汛应急指挥-易涝点管理Service业务层处理
 * 
 * @author newfiber
 * @date 2024-07-24
 */
@Service
public class WaterloggingPointServiceImpl extends ServiceImpl<WaterloggingPointMapper, WaterloggingPoint> implements IWaterloggingPointService {

    @Resource
    private WaterloggingPointMapper waterloggingPointMapper;

    @Override
    public long insert(WaterloggingPointSaveRequest request) {
        WaterloggingPoint waterloggingPoint = new WaterloggingPoint();
        BeanUtils.copyProperties(request, waterloggingPoint);
        if (!CollectionUtils.isEmpty(waterloggingPoint.getPreHandlePicList())){
            waterloggingPoint.setPreHandlePic(JSONObject.toJSONString(waterloggingPoint.getPreHandlePicList()));
        }
        if (!CollectionUtils.isEmpty(waterloggingPoint.getAfterHandlePicList())){
            waterloggingPoint.setAfterHandlePic(JSONObject.toJSONString(waterloggingPoint.getAfterHandlePicList()));
        }
        waterloggingPoint.setDelFlag("0");
        insert(waterloggingPoint);
        return Optional.of(waterloggingPoint).map(BaseEntity::getId).orElse(0L);
    }

    @Override
    public boolean update(WaterloggingPointUpdateRequest request) {
        WaterloggingPoint waterloggingPoint = new WaterloggingPoint();
        BeanUtils.copyProperties(request, waterloggingPoint);
        if (!CollectionUtils.isEmpty(waterloggingPoint.getPreHandlePicList())){
            waterloggingPoint.setPreHandlePic(JSONObject.toJSONString(waterloggingPoint.getPreHandlePicList()));
        }
        if (!CollectionUtils.isEmpty(waterloggingPoint.getAfterHandlePicList())){
            waterloggingPoint.setAfterHandlePic(JSONObject.toJSONString(waterloggingPoint.getAfterHandlePicList()));
        }
        waterloggingPoint.setDelFlag("0");
        return updateById(waterloggingPoint);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(String ids) {
        if (StringUtils.isNotEmpty(ids)){
            for (String s : ids.split(",")) {
                waterloggingPointMapper.deleteById(Long.valueOf(s));
            }

        }
	    return true;
    }

    @Override
    public WaterloggingPoint selectDetail(Long id) {
        WaterloggingPoint waterloggingPoint = waterloggingPointMapper.selectById(id);
        if(null == waterloggingPoint){
	        throw new CustomException(ResultCode.ERROR.getCode(),String.format("%s ID=%s 的记录不存在", this.getClass().getSimpleName(), id));
        }
        if (StringUtils.isNotEmpty(waterloggingPoint.getPreHandlePic())){
            waterloggingPoint.setPreHandlePicList(JSONArray.parseArray(waterloggingPoint.getPreHandlePic(), Accessory.class));
        }
        if (StringUtils.isNotEmpty(waterloggingPoint.getAfterHandlePic())){
            waterloggingPoint.setAfterHandlePicList(JSONArray.parseArray(waterloggingPoint.getAfterHandlePic(), Accessory.class));
        }
        return waterloggingPoint;
    }

    @Override
    public PageResultObject<WaterloggingPoint> selectPage(WaterloggingPointPageQueryRequest request) {
        Page<WaterloggingPoint> page = new Page<>(request.getCurrent(), request.getSize());
        EntityWrapper<WaterloggingPoint> wrapper = new EntityWrapper<>();
        if(!org.springframework.util.StringUtils.isEmpty(request) && !org.springframework.util.StringUtils.isEmpty(request.getName())){
            wrapper.like("name",request.getName());
        }
        Page<WaterloggingPoint> waterloggingPointPage = this.selectPage(page, wrapper);
        if (!ObjectUtils.isEmpty(waterloggingPointPage) && !CollectionUtils.isEmpty(waterloggingPointPage.getRecords())){
            waterloggingPointPage.getRecords().forEach(a -> {
                if (StringUtils.isNotEmpty(a.getPreHandlePic())){
                    a.setPreHandlePicList(JSONArray.parseArray(a.getPreHandlePic(), Accessory.class));
                }
                if (StringUtils.isNotEmpty(a.getAfterHandlePic())){
                    a.setAfterHandlePicList(JSONArray.parseArray(a.getAfterHandlePic(), Accessory.class));
                }
            });

        }

        int count = this.selectCount(wrapper);
        return new PageResultObject<>(request.getCurrent(),request.getSize(),(long)count,waterloggingPointPage.getRecords());
    }

    @Override
    public List<WaterloggingPoint> selectList(WaterloggingPointQueryRequest request) {
        List<WaterloggingPoint> waterloggingPointList = waterloggingPointMapper.selectByCondition(request);
        if (!CollectionUtils.isEmpty(waterloggingPointList)){
            waterloggingPointList.forEach(a -> {
                if (StringUtils.isNotEmpty(a.getPreHandlePic())){
                    a.setPreHandlePicList(JSONArray.parseArray(a.getPreHandlePic(), Accessory.class));
                }
                if (StringUtils.isNotEmpty(a.getAfterHandlePic())){
                    a.setAfterHandlePicList(JSONArray.parseArray(a.getAfterHandlePic(), Accessory.class));
                }
            });
        }
        return waterloggingPointList;
    }

}