Newer
Older
huludao / src / main / java / com / newfiber / modules / inspection / service / impl / InspectionTemplateServiceImpl.java
package com.newfiber.modules.inspection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newfiber.modules.inspection.dao.InspectionTemplateDao;
import com.newfiber.modules.inspection.entity.InspectionTemplateEntity;
import com.newfiber.modules.inspection.service.InspectionDetectionEquService;
import com.newfiber.modules.inspection.service.InspectionTemplateService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("inspectionTemplateService")
public class InspectionTemplateServiceImpl extends ServiceImpl<InspectionTemplateDao, InspectionTemplateEntity> implements InspectionTemplateService {

    @Autowired
    private InspectionTemplateDao inspectionTemplateDao;

    @Resource
    private InspectionDetectionEquService inspectionDetectionEquService;

    @Override
    public InspectionTemplateEntity queryByNo(String templateNo) {
        InspectionTemplateEntity inspectionTemplateEntity = null;

        Map<String, Object> param = new HashMap<>();
        param.put("template_no",templateNo);

        List<InspectionTemplateEntity> inspectionTemplateEntityList = listByMap(param);
        if(CollectionUtils.isNotEmpty(inspectionTemplateEntityList)){
            inspectionTemplateEntity = inspectionTemplateEntityList.get(0);
        }

        return inspectionTemplateEntity;
    }

    @Override
    public List<HashMap<String, Object>> queryList() {
        List<InspectionTemplateEntity> list = inspectionTemplateDao.queryList(null);

        List<String> templateNos = list.stream().map(InspectionTemplateEntity::getTemplateNo).distinct().collect(Collectors.toList());

        /**返回数据集*/
        List<HashMap<String, Object>> result = new ArrayList<>();
        for (String templateNo : templateNos) {

            HashMap<String, Object> map = new HashMap();
            List<InspectionTemplateEntity> entities = new ArrayList<>();

            for (InspectionTemplateEntity templateEntity : list) {
                if (templateEntity.getTemplateNo().equals(templateNo)) {
                    templateEntity.setInspectionDetectionEquList(inspectionDetectionEquService.queryByTemplateNo(templateNo));
                    entities.add(templateEntity);
                    map.put("templateName", templateEntity.getTemplateName());
                }
            }
            map.put("templateNo", templateNo);
            map.put("data", entities);

            result.add(map);
        }

        return result;
    }

    @Override
    @Transactional
    public void removeInfo(List<Integer> list) {

        /**获取删除模版的模版编号*/
        QueryWrapper<InspectionTemplateEntity> wrapper = new QueryWrapper<InspectionTemplateEntity>().in("id", list);
        List<InspectionTemplateEntity> entities = inspectionTemplateDao.selectList(wrapper);
        inspectionTemplateDao.deleteBatchIds(list);
    }

    @Override
    public void increaseRelatedUserCount(String templateNo, Integer count) {
        UpdateWrapper<InspectionTemplateEntity> updateWrapper = new UpdateWrapper<InspectionTemplateEntity>().
                setSql("related_user_count = related_user_count + " + count).
                eq("template_no", templateNo);
        this.update(updateWrapper);
    }

}