PersonnelServiceImpl.java 11.8 KB

package com.objecteye.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.objecteye.entity.*;
import com.objecteye.mapper.SyFeatureMapper;
import com.objecteye.mapper.SyPersonnelMapper;
import com.objecteye.service.DeployService;
import com.objecteye.service.FeatureService;
import com.objecteye.service.PersonnelService;
import com.objecteye.utils.GlobalUtil;
import com.objecteye.utils.VehicleEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 服务实现层
 *
 * @author Administrator
 */

@Service
public class PersonnelServiceImpl implements PersonnelService {

    @Autowired
    private SyPersonnelMapper personnelMapper;
    @Autowired
    private SyFeatureMapper syFeatureMapper;
    @Autowired
    private FeatureService featureService;
    @Autowired
    private VehicleEngine vehicleEngine;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DeployService deployService;

    @Value("${picture.url}")
    private String picIpAndPort;
    @Value("${addUrlToDeployDb}")
    private String addUrlToDeployDb;


    /**
     * 查询全部
     */

    @Override
    public List<SyPersonnel> findAll() {
        SyPersonnelExample example = new SyPersonnelExample();
        SyPersonnelExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0);
        return personnelMapper.selectByExample(example);
    }

    /**
     * 按分页查询
     */

    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        SyPersonnelExample example = new SyPersonnelExample();
        SyPersonnelExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0);
        Page<SyPersonnel> page = (Page<SyPersonnel>) personnelMapper.selectByExample(example);
        return new PageResult(page.getPages(), page.getResult());
    }


    /**
     * 增加
     */

    @Override
    public int add(SyPersonnel personnel) {
        String now = DateUtil.now();
        personnel.setCreateDate(now);
        personnel.setUpdateDate(now);
        personnel.setStatus(0);
        personnel.setConreason(null);
        return personnelMapper.insert(personnel);
    }


    /**
     * 修改
     */

    @Override
    public void update(SyPersonnel personnel) {
        String now = DateUtil.now();
        personnel.setStatus(0);
        personnel.setUpdateDate(now);
        personnelMapper.updateByPrimaryKey(personnel);

    }


    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */

    @Override
    public SyPersonnel findOne(int id) {
        return personnelMapper.selectByPrimaryKey(id);
    }


    /**
     * 批量删除
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(int[] ids) {

        for (int id : ids) {
            SyPersonnel syPersonnel = personnelMapper.selectByPrimaryKey(id);
            syPersonnel.setStatus(1);
            personnelMapper.updateByPrimaryKey(syPersonnel);
            Integer fid = syPersonnel.getFid();
            Integer personCountByFid = findPersonCountByFid(fid);
            SyFeature syFeature = syFeatureMapper.selectByPrimaryKey(fid);
            syFeature.setCount(personCountByFid);
            syFeatureMapper.updateByPrimaryKeySelective(syFeature);
            List<Integer> deployListByLibAndDeployType = deployService.getDeployListByLibAndDeployType(fid, 2);
            if (deployListByLibAndDeployType != null && deployListByLibAndDeployType.size() > 0) {
                for (int i = 0; i < deployListByLibAndDeployType.size(); i++) {
                    String key = deployListByLibAndDeployType.get(i) + "|" + fid;
                    if (redisTemplate.opsForHash().hasKey(key, id)) {
                        redisTemplate.opsForHash().delete(key, id);
                    }
                }
            }
            String imageUrl = syPersonnel.getImageUrl();
            String key = id + "&" + fid + "&" + imageUrl;
            HashMap<String, Object> map = new HashMap<>();
            map.put("retrieveKey", key);
            redisTemplate.opsForHash().delete("personUrl", id);
            String s = vehicleEngine.deleteDataFromDeployDb(map);
            System.out.println("删除");
        }
    }


    @Override
    public PageResult findPage(int fid, String name, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<PersonnelResultMsg> list = new ArrayList<>();

        SyPersonnelExample example = new SyPersonnelExample();
        SyPersonnelExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0);
        criteria.andFidEqualTo(fid);

        /*if (name != null && name.length() > 0) {
            criteria.andNameLike("%" + name + "%");
        }*/
        if (!name.equals("")) {
            criteria.andNameLike("%" + name + "%");
        }
        SyPersonnelExample.Criteria criteria1 = example.createCriteria();
        criteria1.andStatusEqualTo(0);
        criteria1.andFidEqualTo(fid);

        /*if (name != null && name.length() > 0) {
            criteria1.andCreateDateLike("%" + name + "%");
        }*/
        if (!name.equals("")) {
            criteria1.andCreateDateLike("%" + name + "%");
        }
        example.or(criteria1);

        Page<SyPersonnel> page = (Page<SyPersonnel>) personnelMapper.selectByExample(example);
        List<SyPersonnel> result = page.getResult();
        int size = result.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                SyPersonnel syPersonnel = result.get(i);
                Integer personnelId = syPersonnel.getId();
                int alarmNum = 0;
                PersonnelResultMsg personnelResultMsg = new PersonnelResultMsg(personnelId, syPersonnel.getName(), syPersonnel.getCreateDate(), syPersonnel.getConreason(), alarmNum, syPersonnel.getImageUrl(), syPersonnel.getIndentity());

                list.add(personnelResultMsg);
            }
        }
        return new PageResult<PersonnelResultMsg>(page.getPages(), list);
    }


    @Override
    public PageResult findPageByNameOCard(SyPersonnel personnel, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        ArrayList<PersonnelResultMsg> list = new ArrayList<>();
        SyPersonnelExample example = new SyPersonnelExample();
        SyPersonnelExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0);

        String name = personnel.getName();
        if (name != null && name.length() > 0) {
            criteria.andNameLike("%" + name + "%");
        }
        String indentity = personnel.getIndentity();
        if (indentity != null && indentity.length() > 0) {
            criteria.andIndentityLike("%" + indentity + "%");
        }
        Page<SyPersonnel> page = (Page<SyPersonnel>) personnelMapper.selectByExample(example);
        return new PageResult(page.getPages(), page.getResult());
    }

    @Override
    public List<Map<String, Object>> findByImage(float similarity, MultipartFile file, int featureId) {
        return null;
    }

    @Override
    public Integer findPersonCountByFid(int fid) {
        SyPersonnelExample syPersonnelExample = new SyPersonnelExample();
        SyPersonnelExample.Criteria criteria = syPersonnelExample.createCriteria();
        criteria.andStatusEqualTo(0);
        criteria.andFidEqualTo(fid);
        int count = personnelMapper.countByExample(syPersonnelExample);

        return count;
    }


    @Override
    public List<SyPersonnel> findPersonIdByFid(int fid) {
        SyPersonnelExample syPersonnelExample = new SyPersonnelExample();
        SyPersonnelExample.Criteria criteria = syPersonnelExample.createCriteria();
        criteria.andStatusEqualTo(0);
        criteria.andFidEqualTo(fid);

        return personnelMapper.selectByExample(syPersonnelExample);
    }

    /**
     * 上传人像到人像库
     *
     * @param uploadFiles
     * @param featureId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String uploadFiles(MultipartFile[] uploadFiles, int featureId) {

        int count = 0;
        SyFeature one = featureService.findOne(featureId);
        System.out.println(one.getCreateDate());
        List<Integer> deployListByLibAndDeployType = deployService.getDeployListByLibAndDeployType(featureId, 2);

        if (uploadFiles.length > 0 && one != null) {
            int len = uploadFiles.length;
            for (int i = 0; i < len; i++) {
                MultipartFile multipartFile = uploadFiles[i];
                String fileName = multipartFile.getOriginalFilename();
                System.out.println(fileName);
                fileName = fileName.replaceAll("\\\\", "/");
                String personName = fileName.split("\\_")[0];
                if (personName.contains("/")) {
                    String[] personNameArray = personName.split("/");
                    personName = personNameArray[personNameArray.length - 1];
                }
                String personIndentity = fileName.split("\\_")[1].split("\\.")[0];
                String picPath = GlobalUtil.dbPath1() + File.separator + "picture" + File.separator + fileName;
                String imageUrl = picIpAndPort + fileName;
                File newFile = new File(picPath);
                try {
                    multipartFile.transferTo(newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                SyPersonnel syPersonnel = new SyPersonnel();
                syPersonnel.setName(personName);
                syPersonnel.setIndentity(personIndentity);
                syPersonnel.setImageUrl(imageUrl);
                syPersonnel.setFid(featureId);
                int add = add(syPersonnel);
                Integer personnelId = syPersonnel.getId();
                String key = personnelId + "&" + featureId + "&" + imageUrl;
                Map<String, Object> map = new HashMap<>(16);
                map.put("TPXX", imageUrl);
                map.put("retrieveKey", key);
                String s = vehicleEngine.addUrlToDeployDb(map);
                if (deployListByLibAndDeployType.size() > 0) {
                    for (int j = 0; j < deployListByLibAndDeployType.size(); j++) {
                        Integer deployId = deployListByLibAndDeployType.get(j);
                        String key1 = deployId + "|" + featureId;
                        redisTemplate.opsForHash().put(key1, personnelId, 0);
                    }
                }
                redisTemplate.opsForHash().put("personUrl", personnelId, imageUrl);
                System.out.println("请求返回:" + s);
                String code = JSONObject.parseObject(s).getString("code");
                //ResponseParam responseParam = JSON.parseObject(s, ResponseParam.class);
                //String code = responseParam.getCode();
                if ("0".equals(code)) {
                    if (add > 0) {
                        count++;
                    }
                }
            }
            Integer count1 = one.getCount();
            one.setCount(count + count1);
            featureService.updateCountById(one);
            return "上传的文件共有" + len + ",成功上传的个数为: " + count;
        }
        return null;
    }
}