GuideboardInfoServiceImpl.java 5.54 KB
package com.bsth.service.schedule.impl;

import com.bsth.entity.schedule.GuideboardInfo;
import com.bsth.entity.schedule.TTInfoDetail;
import com.bsth.service.schedule.GuideboardInfoService;
import com.bsth.service.schedule.TTInfoDetailService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.utils.DataToolsFile;
import com.bsth.service.schedule.utils.DataToolsService;
import com.bsth.util.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.transaction.Transactional;
import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 路牌信息服务。
 */
@Service
public class GuideboardInfoServiceImpl extends BServiceImpl<GuideboardInfo, Long> implements GuideboardInfoService {
    @Autowired
    @Qualifier(value = "gbInfo_dataTool")
    private DataToolsService dataToolsService;

    @Autowired
    private TTInfoDetailService ttInfoDetailService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Long getMaxLpno(Integer xlid) {
        String sql = "select max(lp_no) as maxno from bsth_c_s_gbi where xl = ?";

        Long maxNo = jdbcTemplate.query(sql, new ResultSetExtractor<Long>() {
            @Override
            public Long extractData(ResultSet rs) throws SQLException, DataAccessException {
                if (rs.next()) {
                    return rs.getLong("maxno");
                } else {
                    return 0L;
                }
            }
        }, xlid);

        return maxNo + 1;
    }

    @Override
    public void validate_lpno(GuideboardInfo guideboardInfo) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (guideboardInfo.getId() != null) {
            param.put("id_ne", guideboardInfo.getId());
        }

        if (guideboardInfo.getXl() == null || guideboardInfo.getXl().getId() == null) {
            throw new ScheduleException(I18n.getInstance().getMessage("guideboardInfoServiceImpl-line69"));
        } else {
            param.put("isCancel_eq", false); // 作废的也算入判定区
            param.put("xl.id_eq", guideboardInfo.getXl().getId());
            param.put("lpNo_eq", guideboardInfo.getLpNo());
            if (!CollectionUtils.isEmpty(list(param))) {
                throw new ScheduleException(I18n.getInstance().getMessage("guideboardInfoServiceImpl-line75"));
            }
        }
    }

    @Override
    public void validate_lpname(GuideboardInfo guideboardInfo) throws ScheduleException {
        // 查询条件
        Map<String, Object> param = new HashMap<>();
        if (guideboardInfo.getId() != null) {
            param.put("id_ne", guideboardInfo.getId());
        }

        if (guideboardInfo.getXl() == null || guideboardInfo.getXl().getId() == null) {
            throw new ScheduleException(I18n.getInstance().getMessage("guideboardInfoServiceImpl-line89"));
        } else {
            param.put("isCancel_eq", false); // 作废的也算入判定区
            param.put("xl.id_eq", guideboardInfo.getXl().getId());
            param.put("lpName_eq", guideboardInfo.getLpName());
            if (!CollectionUtils.isEmpty(list(param))) {
                throw new ScheduleException(I18n.getInstance().getMessage("guideboardInfoServiceImpl-line95"));
            }
        }
    }

    @Transactional
    @Override
    public void delete(Long aLong) throws ScheduleException {
        // 删除操作就是作废操作
        toggleCancel(aLong);
    }

    // 作废方法
    @Transactional
    public void toggleCancel(Long id) throws ScheduleException {
        GuideboardInfo guideboardInfo = findById(id);
        Map<String, Object> param = new HashMap<>();
        if (guideboardInfo.getIsCancel()) {
            validate_lpno(guideboardInfo);
            validate_lpname(guideboardInfo);
            guideboardInfo.setIsCancel(false);
        } else {
            param.clear();
            param.put("xl.id_eq", guideboardInfo.getXl().getId());
            param.put("ttinfo.isCancel_eq", false);
            param.put("lp.id_eq", guideboardInfo.getId());
            List<TTInfoDetail> ttInfoDetailList = (List<TTInfoDetail>) ttInfoDetailService.list(param);
            if (CollectionUtils.isEmpty(ttInfoDetailList)) {
                guideboardInfo.setIsCancel(true);
            } else {
                throw new ScheduleException(I18n.getInstance().getMessage("guideboardInfoServiceImpl-line125",
                        ttInfoDetailList.get(0).getTtinfo().getName()));
            }
        }
    }

    @Override
    public DataToolsFile uploadFile(String filename, byte[] filedata) throws ScheduleException {
        return dataToolsService.uploadFile(filename, filedata);
    }

    @Override
    public void importData(File file, Map<String, Object> params) throws ScheduleException {
        dataToolsService.importData(file, params);
    }

    @Override
    public DataToolsFile exportData(Map<String, Object> params) throws ScheduleException {
        return dataToolsService.exportData(params);
    }

}