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

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.utils.FileUpload;
import com.newfiber.api.pc.dao.RiverManage.PartolManagePicDao;
import com.newfiber.api.pc.dao.RiverManage.PatrolManageLawBaseMapper;
import com.newfiber.api.pc.dao.RiverManage.PatrolManageProcessMapper;
import com.newfiber.api.pc.model.River.*;
import com.newfiber.api.pc.service.PatrolManageLawBaseService;
import com.newfiber.api.pc.service.PatrolManageLawFileService;
import com.newfiber.api.pc.service.PatrolManageProblemService;
import com.newfiber.api.pc.service.PatrolManageUserStateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;


@Service("patrolManageLawBaseService")
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class PatrolManageLawBaseServiceImpl extends ServiceImpl<PatrolManageLawBaseMapper, PatrolManageLawBase> implements PatrolManageLawBaseService {

    /** 节点文件保存路径 */
    @Value("${xnFile.pic}")
    private String xnPicPath;

    @Autowired
    private PatrolManageLawFileService patrolManageLawFileService;

    @Autowired
    private PatrolManageProblemService patrolManageProblemService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PartolManagePicDao partolManagePicDao;

    @Autowired
    private PatrolManageLawBaseMapper patrolManageLawBaseMapper;

    @Autowired
    private PatrolManageProcessMapper patrolManageProcessMapper;

    @Autowired
    private PatrolManageUserStateService patrolManageUserStateService;

    @Override
    public Long savePatrolManageLawBase(PatrolManageLawBase patrolManageLawBase) {
        List<PartolManagePic> picList = new ArrayList<>();
        String picNo = UUID.randomUUID().toString();
        //获取案件列表
        //判断是新增数据还是第二次推送数据
        PatrolManageLawBase oldData = patrolManageLawBaseMapper.selectByTaskNum(patrolManageLawBase.getOtherTaskNum());
        Long problemId = 0L;
        Integer i = 0;
        if(oldData!=null){
            PatrolManageLawBase newData = new PatrolManageLawBase();
            BeanUtils.copyProperties(patrolManageLawBase,newData);
            newData.setId(oldData.getId());
            i = patrolManageLawBaseMapper.updateById(newData);
        }else{
            i = patrolManageLawBaseMapper.insert(patrolManageLawBase);
        }
        if(i>0){
            //todo 删除上次传入的图片内容
            patrolManageLawFileService.deleteByTaskNum(patrolManageLawBase.getOtherTaskNum());
            List<PatrolManageLawFile> patrolManageLawFile = patrolManageLawBase.getMedias();
            if(patrolManageLawFile!=null && patrolManageLawFile.size() > 0){
                Calendar instance = Calendar.getInstance();
                StringBuffer sb = new StringBuffer();
                sb.append(instance.get(Calendar.YEAR))
                        .append(File.separator)
                        .append(instance.get(Calendar.MARCH) + 1).
                        append(File.separator);
                //设置关联,将访问到的图片写入本地,然后读取文件
                patrolManageLawFile.forEach(s -> {
                    String replace = s.getMediaUrl().replace("172.16.40.115", "10.50.10.130");
                    String fileUrl = FileUpload.uploadQianURL(replace, xnPicPath, sb.toString());
                    s.setMediaUrl(fileUrl);
                    s.setTaskNum(patrolManageLawBase.getOtherTaskNum());
                    PartolManagePic partolManagePic = new PartolManagePic();
                    partolManagePic.setCreateTime(s.getCreateTime());
                    partolManagePic.setPicNo(picNo);
                    partolManagePic.setPicAddress(fileUrl);
                    partolManagePic.setPicName(s.getMediaName());
                    partolManagePic.setType((byte)1);
                    picList.add(partolManagePic);
                });
                patrolManageLawFileService.insertBatch(patrolManageLawFile);


            }
        //    try{
                //需要将源数据抽取出来进行预警报警的任务
                PatrolManageProblem patrolManageProblem = new PatrolManageProblem();

                patrolManageProblem.setAddress(patrolManageLawBase.getAddress());

                patrolManageProblem.setSourceType(7);
                patrolManageProblem.setEventSrcId(patrolManageLawBase.getEventSrcId());
                patrolManageProblem.setEventSrc(patrolManageLawFileService.selectEventSrc(Integer.valueOf(patrolManageLawBase.getEventSrcId())));
                patrolManageProblem.setEventType(patrolManageLawFileService.selectEventType(Integer.valueOf(patrolManageLawBase.getEventTypeCode())));
                patrolManageProblem.setEventTypeCode(patrolManageLawBase.getEventTypeCode());
                patrolManageProblem.setEventLevelId(patrolManageLawBase.getEventLevelId());
                patrolManageProblem.setEventLevel(patrolManageLawFileService.selectEventLevel(patrolManageLawBase.getEventLevelId()));
                patrolManageProblem.setMainTypeCode(patrolManageLawBase.getMainTypeCode());
                patrolManageProblem.setDeadlineTime(patrolManageLawBase.getDeadlineTime());
                patrolManageProblem.setUrgentFlag(patrolManageLawBase.getUrgentFlag());
                patrolManageProblem.setCreateUserName(patrolManageLawBase.getPatrolName());
                patrolManageProblem.setCreateUserPhone(patrolManageLawBase.getPatrolTel());
                patrolManageProblem.setSiteNo(patrolManageLawBase.getOtherTaskNum());
                patrolManageProblem.setTaskNum(patrolManageLawBase.getOtherTaskNum());
                patrolManageProblem.setPicNo(picNo);

                StringBuffer sb = new StringBuffer();
                //浉河区政府-礼节社区居委会-民权街道办事处,
                //浉河区礼节路“胜利路小学”旁河沟,存在异味难闻问题,
                //(7天)水质恶化,有明显臭味及污染物,根据职责划分,请贵单位及时关注处理
                if(!StringUtils.isEmpty(patrolManageLawBase.getDistrictName())){ //浉河区政府
                    sb.append(patrolManageLawBase.getDistrictName());
                }
                if(!StringUtils.isEmpty(patrolManageLawBase.getCommunityName())){//礼节社区居委会
                    sb.append("-").append(patrolManageLawBase.getCommunityName());
                }
                if(!StringUtils.isEmpty(patrolManageLawBase.getStreetName())){//民权街道办事处
                    sb.append("-").append(patrolManageLawBase.getStreetName());
                }
                if(!StringUtils.isEmpty(patrolManageLawBase.getEventDesc())){//浉河区礼节路“胜利路小学”旁河沟,存在异味难闻问题
                    sb.append(",").append(patrolManageLawBase.getEventDesc());
                }
                if(!StringUtils.isEmpty(patrolManageLawBase.getNewInstCondName())){//(7天)水质恶化,有明显臭味及污染物
                    sb.append(",").append(patrolManageLawBase.getNewInstCondName());
                }
                if(!StringUtils.isEmpty(patrolManageLawBase.getOpinion())){//根据职责划分,请贵单位及时关注处理
                    sb.append(",").append(patrolManageLawBase.getOpinion());
                }
                patrolManageProblem.setDescription(sb.toString());
                problemId = patrolManageProblemService.dataSystem(patrolManageProblem);
                if(picList!=null && picList.size()>0){
                    partolManagePicDao.insertBatch(picList);
                }

//            } catch (Exception e){
//                log.info("数字证通下发任务时,写入patrolManageProblem表失败!");
//            }
        }
        return problemId;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void complete(CompleteRequest result) {
        //1.将结案的结果记录到流程
        if(result==null) {
            log.error("结案操作时,入参不能为空!");
            return;
        }
        //同意
        Integer state = 14;
        String stateDesc = "结案";
        //2.查询最新的办理经过 todo
        //Json processStr = new Json("");
        String processStr= selectTask(result.getOtherTaskNum());

        PatrolManageProblem problem = patrolManageProblemService.selectByTaskNum(result.getOtherTaskNum());
        if(problem == null){
            log.error("查询不到数管任务!");
            return;
        }
        if(StringUtils.isEmpty(processStr)){
            problem.setRecProcess(processStr.toString());
        }
        problem.setState(state);
        problem.setStateDesc(stateDesc);
        patrolManageProblemService.updateById(problem);

        //修改patrolManageLawBase的数据
        PatrolManageLawBase oldData = patrolManageLawBaseMapper.selectByTaskNum(result.getOtherTaskNum());
        if(oldData!=null) {
            if(StringUtils.isEmpty(processStr)){
                oldData.setRecProcess(processStr.toString());
            }
            oldData.setTaskState(state.toString());
            oldData.setTaskStateDesc(stateDesc);
            patrolManageLawBaseMapper.updateById(oldData);
        }

        //2.写入流程
        PatrolManageProcess process = new PatrolManageProcess();
        process.setProblemId(problem.getId());
        process.setPatrolType(problem.getPatrolType());
        process.setState(state);
        process.setStateDesc(stateDesc);
        process.setDescription(result.getOpinion());
        patrolManageProcessMapper.insert(process);


    }

    private String selectTask(String taskNum){
        String url ="http://10.50.10.25:8018/free/exchange/9560008c-e54d-4795-96eb-b10cc8510ccd";
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");

        // 设置请求参数
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("taskNum",taskNum);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(postParameters, headers);

        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        log.info("调用数字政通远程接口返回的数据为:{}",postForEntity.getBody());
        return postForEntity.getBody();
    }

    @Override
    public void backoff(BackoffResult result) {
        log.error("回退操作");
        //判断回退操作,生成流程和改变状态
        if(result==null) {
            log.error("回退操作时,入参不能为空!");
            return;
        }
        Integer state = 0;
        String stateDesc = "";
        if(result.getResult() == 1){
            //同意
            state = 22;
            stateDesc = "回退成功";
        }else{
            state = 2;
            stateDesc = "不同意回退";
        }
        addProcess(result.getOpinion(),result.getOtherTaskNum(),state,result.getResult(),stateDesc);
    }

    @Override
    public void charge(ChargeResult result) {
        //判断挂账操作,生成流程和改变状态
        if(result==null) {
            log.error("挂账操作时,入参不能为空!");
            return;
        }
        Integer state = 0;
        String stateDesc = "";
        if(result.getResult() == 1){
            //同意
            state = 24;
            stateDesc = "挂账成功";
        }else{
            state = 2;
            stateDesc = "不同意挂账";
        }
        addProcess(result.getOpinion(),result.getOtherTaskNum(),state,result.getResult(),stateDesc);
    }

    @Override
    public void delay(DelayResult result) {
        //判断延期操作,生成流程和改变状态
        if(result==null) {
           log.error("延期操作时,入参不能为空!");
           return;
        }
        Integer state = 0;
        String stateDesc = "";
        if(result.getResult() == 1){
            //同意
            state = 20;
            stateDesc = "延期成功";
        }else{
            state = 2;
            stateDesc = "不同意延期";
        }
        addProcess(result.getOpinion(),result.getOtherTaskNum(),state,result.getResult(),stateDesc);
    }

    /**
     * 添加流程状态和流程记录
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    public void addProcess(String opinion,String taskNum,Integer state,Integer result,String stateDesc) {
        PatrolManageProblem problem = patrolManageProblemService.selectByTaskNum(taskNum);
        if(result!=1){
            patrolManageUserStateService.addOrUpdateUserState(problem.getId(),problem.getPatrolType(),problem.getCenterNo(),1);
        }
        if(problem == null){
            log.error("查询不到数管任务!");
            return;
        }
        problem.setState(state);
        problem.setStateDesc(stateDesc);
        patrolManageProblemService.updateById(problem);

        //修改patrolManageLawBase的数据
        PatrolManageLawBase oldData = patrolManageLawBaseMapper.selectByTaskNum(taskNum);
        if(oldData!=null) {
            oldData.setTaskState(state.toString());
            oldData.setTaskStateDesc(stateDesc);
            patrolManageLawBaseMapper.updateById(oldData);
        }

        //2.写入流程
        PatrolManageProcess process = new PatrolManageProcess();
        process.setPatrolType(problem.getPatrolType());
        process.setState(state);
        process.setStateDesc(stateDesc);
        process.setDescription(opinion);
        process.setProblemId(problem.getId());
        patrolManageProcessMapper.insert(process);
    }

    public String getXnPicPath() {
        return xnPicPath;
    }

    public void setXnPicPath(String xnPicPath) {
        this.xnPicPath = xnPicPath;
    }
}