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


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newfiber.api.config.UrlConfig;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.pc.dao.RiverManage.PatrolManageBaseMapper;
import com.newfiber.api.pc.model.River.*;
import com.newfiber.api.pc.service.PatrolManageBaseService;
import com.newfiber.api.pc.service.PatrolManageUserRelationService;
import com.newfiber.api.pc.service.PatrolManageUserTypeService;
import com.newfiber.api.pc.service.PatrolUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Service("patrolManageBaseService")
@Slf4j
public class PatrolManageBaseServiceImpl extends ServiceImpl<PatrolManageBaseMapper, PatrolManageBase> implements PatrolManageBaseService {

    @Autowired
    private PatrolManageBaseMapper patrolManageBaseMapper;

    @Autowired
    private PatrolManageUserRelationService patrolManageUserRelationService;

    @Autowired
    private PatrolManageUserTypeService patrolManageUserTypeService;

    @Resource
    private UrlConfig urlConfig;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PatrolUserInfoService patrolUserInfoService;

    @Override
    public PageInfo<PatrolManageBaseDto> listForPage(String patrolType,String projectName, Integer pageNo, Integer pageSize) {
        //patrolType 1.河道 2.管网
        PageHelper.startPage(pageNo, pageSize);
        List<PatrolManageBaseDto> list = patrolManageBaseMapper.selectListByTypeAndName(patrolType,projectName);
        PageInfo<PatrolManageBaseDto> result = new PageInfo<>();
        if (!list.isEmpty()) {
            result = new PageInfo<>(list);
        }
        return result;
    }

    @Override
    public PageInfo<PatrolManageBase> selectBase(String patrolType, String areaCode, String projectName, String projectParentId, Integer pageNo, Integer pageSize) {
        //patrolType 1.河道 2.管网
        PageHelper.startPage(pageNo, pageSize);
        List<PatrolManageBase> list = patrolManageBaseMapper.selectBase(patrolType, areaCode, projectName, projectParentId);
        PageInfo<PatrolManageBase> result = new PageInfo<>();
        if (!list.isEmpty()) {
            result = new PageInfo<>(list);
        }
        return result;
    }

    @Override
    public List<PatrolManageBase> listByUser(String patrolType, String userNo,String highest) {
        EntityWrapper<PatrolManageUserRelation> wrapper = new EntityWrapper<>();
        if(!StringUtils.isEmpty(highest) && !"yes".equalsIgnoreCase(highest)){
            wrapper.eq("patrol_user_no",userNo);
        }
        wrapper.eq("patrol_type",patrolType);
        List<PatrolManageUserRelation> relation = patrolManageUserRelationService.selectList(wrapper);
        List<Long> collect = new ArrayList<>();
        if(relation!=null && relation.size()>0){
            collect = relation.stream().map(PatrolManageUserRelation::getBindNo).collect(Collectors.toList());
        }
        List<PatrolManageBase> result =  patrolManageBaseMapper.selectListByIds(collect,patrolType);
        return result;
    }

    @Override
    public Map<String, Object> getProject(String patrolType, String userNo, String highest) {
        Map<String,Object> map = new HashMap<>();
        EntityWrapper<PatrolManageUserRelation> wrapper = new EntityWrapper<>();
        /*if(!StringUtils.isEmpty(highest) && !"yes".equalsIgnoreCase(highest)){
            wrapper.eq("patrol_user_no",userNo);
        }*/
        wrapper.eq("patrol_type",patrolType);
        List<PatrolManageUserRelation> relation = patrolManageUserRelationService.selectList(wrapper);
        List<Long> collect = new ArrayList<>();
        if(relation!=null && relation.size()>0){
            collect = relation.stream().map(PatrolManageUserRelation::getBindNo).collect(Collectors.toList());
        }
        List<PatrolManageBase> result =  patrolManageBaseMapper.selectListByIds(collect,patrolType);
        if(result!=null && !result.isEmpty() && result.size()==1){
            map.put("state",1);
            map.put("project",Arrays.asList(result.get(0)));
        }else{
            map.put("state",0);
            map.put("project",result);
        }
        return map;
    }

    @Override
    public List<PatrolManageProjectDto> selectProjectList(Integer projectParentId, Integer patrolType, String projectName) {
        return patrolManageBaseMapper.selectProjectList(projectParentId,patrolType,projectName,null);
    }

    @Override
    public PatrolManageUserDto getProjectByUserType(Integer patrolType, String userNo) {
        PatrolManageUserType userType = patrolManageUserTypeService.getUserType(userNo,patrolType);
        List<PatrolManageProjectDto> patrolManageProjectDtos = new ArrayList<>();
        PatrolManageUserDto dto = new PatrolManageUserDto();
        if(userType!=null){
            //1领导 2调度中心 3负责人 4厂网河主负责人 5厂网河下级处理人
            if(userType.getUserType() == 5){
                patrolManageProjectDtos = patrolManageBaseMapper.selectProjectList(null, patrolType, null, userNo);
            }else if(userType.getUserType() == 4){
                patrolManageProjectDtos = patrolManageBaseMapper.selectProjectList(0, patrolType, null, userNo);
            }else{
                patrolManageProjectDtos = patrolManageBaseMapper.selectProjectList(0, patrolType, null, null);
            }
            BeanUtils.copyProperties(userType,dto);
            dto.setPatrolType(patrolType);
        }else{
            patrolManageProjectDtos = patrolManageBaseMapper.selectProjectList(0, patrolType, null, null);
            dto.setUserNo(userNo);
            dto.setPatrolType(patrolType);
            dto.setUserType(6);
        }
        dto.setProject(patrolManageProjectDtos);
        return dto;
    }

    @Override
    public PatrolManageBase getProjectId(Long projectId,Integer patrolType) {
        if(projectId == null){
            return new PatrolManageBase();
        }
        return patrolManageBaseMapper.getProjectId(projectId,patrolType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RespBodyObj saveOrUpdateUser(PatrolManageBaseVo data) {
        //如果是下级巡查项目,组成项目名称
        setProjectName(data);
        //1.添加和编辑巡查项目
        PatrolManageBase base = new PatrolManageBase();
        BeanUtils.copyProperties(data, base);
        this.insertOrUpdate(base);
        //删除旧用户的关联数据
        patrolManageUserRelationService.changeUser(data);
        //2.添加是否启用的用户
        RespBodyObj respBodyObj = addOrUpdateUser(data);
        if(200!=respBodyObj.getCode()){
            return respBodyObj;
        }
        return RespBodyObj.ok(base.getId());
    }

    /**
     * 添加或修改用户
     *
     * @param data
     */
    private RespBodyObj addOrUpdateUser(PatrolManageBaseVo data) {
        RespBodyObj respBodyObj = new RespBodyObj();
        if (StringUtils.isEmpty(data.getId())) {
            //1.需要新创建用户
            UserPatrolInfoDto user = new UserPatrolInfoDto();
            user.setUserNo(data.getUserNo());
            user.setPhoneNum(data.getPhone());
            user.setUserName(data.getName());
            user.setIsUsed(data.getIsUsed());
            user.setPatrolType(data.getPatrolType().longValue());
            respBodyObj = addUserInfo(user);
        } else {
            //2.不需要新创建用户
            UserPatrolInfoDto user = new UserPatrolInfoDto();
            user.setUserNo(data.getUserNo());
            user.setPhoneNum(data.getPhone());
            user.setUserName(data.getName());
            user.setIsUsed(data.getIsUsed());
            user.setPatrolType(data.getPatrolType().longValue());
            respBodyObj = updateUserInfo(user);
        }
        if(200!=respBodyObj.getCode() && !"该用户名被使用".equals(respBodyObj.getMessage())){
            return respBodyObj;
        }
        PatrolUserInfo info = new PatrolUserInfo();
        BeanUtils.copyProperties(data, info);
        if (data.getUserId() != null) {
            info.setId(data.getUserId());
        } else {
            info.setId(null);
        }
        boolean b = patrolUserInfoService.insertOrUpdate(info);
        if(b){
            return new RespBodyObj().ok();
        }
        return new RespBodyObj().error();
    }

    /**
     * 组装下级巡查河道名称
     *
     * @param data
     */
    private void setProjectName(PatrolManageBaseVo data) {
        if (!"0".equalsIgnoreCase(data.getProjectParentId())) {
            StringBuffer sb = new StringBuffer();
            sb.append(data.getProjectName());
            if (data.getStartName() != null && data.getEndName() != null) {
                sb.append("(").append(data.getStartName())
                        .append("-").append(data.getEndName()).append(")");
            }
            data.setProjectName(sb.toString());
        }
    }

    /**
     * 添加用户信息
     */
    private RespBodyObj addUserInfo(UserPatrolInfoDto user) {
        JSONObject data = new JSONObject();
        data.put("data", user);
        String url = urlConfig.getHuludaoMainUrl() + "/users/addPatrolUser";
        log.info("保存系统用户,保存历史数据:{}", url);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, data, String.class);
        if(postForEntity.getStatusCode().is2xxSuccessful()){
            JSONObject jsonObject = JSONObject.parseObject(postForEntity.getBody(), JSONObject.class);
            if(jsonObject.get("code").toString().equals("500")){
                log.info("创建用户失败"+JSON.toJSONString(null));
                return RespBodyObj.error(jsonObject.get("message").toString());
            }else{
                log.info("创建用户成功"+ JSON.toJSONString(user));
                return RespBodyObj.ok("创建成功");
            }
        }
        return RespBodyObj.error("添加用户失败");
    }

    /**
     * 添加用户信息
     */
    private RespBodyObj updateUserInfo(UserPatrolInfoDto user) {
        JSONObject data = new JSONObject();
        data.put("data",user);
        String url = urlConfig.getHuludaoMainUrl() + "/users/updatePatrolUser";
        log.info("修改用户,保存历史数据:{}",url);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, data, String.class);
        if(postForEntity.getStatusCode().is2xxSuccessful()){
            JSONObject jsonObject = JSONObject.parseObject(postForEntity.getBody(), JSONObject.class);
            if(jsonObject.get("code").equals("500")){
                log.info("修改用户失败"+ JSON.toJSONString(user));
                return RespBodyObj.error(jsonObject.get("message").toString());
            }else{
                log.info("修改用户成功"+ JSON.toJSONString(user));
                return RespBodyObj.ok("修改用户成功");
            }
        }
        return RespBodyObj.error("修改用户失败");
    }

}