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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newfiber.api.config.FileVisitProperty;
import com.newfiber.api.core.base.BaseProcessState;
import com.newfiber.api.core.base.BaseService;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.core.utils.DateUtils;
import com.newfiber.api.pc.dao.EventsManage.ComplainPicDao;
import com.newfiber.api.pc.dao.EventsManage.ComplainProcessDao;
import com.newfiber.api.pc.dao.EventsManage.ComplaintDao;
import com.newfiber.api.pc.dao.EventsManage.ComplaintdetailsDao;
import com.newfiber.api.pc.dao.RiverManage.RiverInfoBasisDao;
import com.newfiber.api.pc.model.EventsManage.ComplainPic;
import com.newfiber.api.pc.model.EventsManage.ComplainProcess;
import com.newfiber.api.pc.model.EventsManage.Complaint;
import com.newfiber.api.pc.model.EventsManage.Complaintdetails;
import com.newfiber.api.pc.model.River.RiverInfoBasis;
import com.newfiber.api.pc.model.UserManager.SysOrganization;
import com.newfiber.api.pc.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 投诉基础表 Service
 *
 * @author xcc
 * @date 2018-12-04 16:22:00
 */
@Service("complaintService")
public class ComplaintService extends BaseService<ComplaintDao, Complaint> {
    @Autowired
    private ComplaintDao complaintDao;

    @Autowired
    private ComplainProcessDao complainProcessDao;

    @Autowired
    private ComplainPicDao complainPicDao;

    @Autowired
    private ComplaintdetailsDao complaintdetailsDao;

    @Resource
    private RestTemplateService restTemplateService;

    @Autowired
    private FileVisitProperty fileVisitProperty;

    @Autowired
    private RiverChiefService riverChiefService;

    @Autowired
    private RiverInfoBasisDao riverInfoBasisDao;

    /**
     * 根据行政区划编号查询投诉处理列表
     *
     * @param divisionNo --行政区划编号
     * @return
     */
    public List<Complaint> getComplainListByDivisionNo(String divisionNo,Integer page,Integer rows) {
        //根据行政区划编号查询投诉列表
        List<Complaint> complaints = new ArrayList<>();
        PageHelper.startPage(page,rows);
        complaints = complaintDao.selectByDivisionNo(divisionNo);
        if(complaints!=null && complaints.size()>0){
            PageInfo<Complaint> result = new PageInfo<Complaint>(complaints);
            complaints = result.getList();
        }
        return complaints;
    }


    /**
     * 根据事件编号查询详情
     *
     * @param eventNo
     * @return
     */
    public Complaint getInfo(String eventNo) {
        return complaintDao.selectInfo(eventNo);
    }

    /**
     * 投诉事件提交审核
     *
     * @param param
     * @return
     */
    public RespBodyObj<ComplainProcess> accept(ReqBodyObj<ComplainProcess> param,HttpServletRequest request) {

        ComplainProcess process = param.getData();
        process.setEventFlowId(BaseProcessState.process_accept_code);
        process.setEventFlowName(BaseProcessState.process_accept_zh);
        process.setEventTime(new Date());
        process.setDisposeUserNo(request.getHeader("userNo"));
        try {
            //向流程表中插入数据
            complainProcessDao.insert(process);
            //修改投诉表状态
            EntityWrapper<Complaint> wrapper = new EntityWrapper<>();
            wrapper.eq("Event_No", process.getEventNo());
            Complaint complaint = new Complaint();
            complaint.setEventStatusId(BaseProcessState.state_accept_code);
            complaint.setEventStatusName(BaseProcessState.state_accept_zh);
            complaintDao.update(complaint, wrapper);
            return RespBodyObj.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error();
        }
    }

    /**
     * 投诉事件审核
     *
     * @return
     */
    public RespBodyObj<ComplainProcess> auditing(ReqBodyObj<Map<String, Object>> param,HttpServletRequest request) {

        Map<String, Object> map = param.getData();
        ComplainProcess process = new ComplainProcess();
        process.setProcessContent((String) map.get("processContent"));
        process.setDisposeUserNo((String) map.get("disposeUserNo"));
        process.setEventNo((String) map.get("eventNo"));
        process.setDisposeUserNo(request.getHeader("userNo"));
        //判断审核是否通过
        String success = (String) map.get("success");
        process.setEventFlowId("true".equals(success) ? BaseProcessState.process_auditing_yes_code : BaseProcessState.process_auditing_no_code);
        process.setEventFlowName("true".equals(success) ? BaseProcessState.process_auditing_yes_zh : BaseProcessState.process_auditing_no_zh);
        process.setEventTime(new Date());
        try {
            //向流程表中插入数据
            complainProcessDao.insert(process);
            //修改投诉表状态
            EntityWrapper<Complaint> wrapper = new EntityWrapper<>();
            wrapper.eq("Event_No", process.getEventNo());
            Complaint complaint = new Complaint();
            complaint.setEventStatusId(BaseProcessState.state_auditing_code);
            complaint.setEventStatusName(BaseProcessState.state_auditing_zh);
            complaintDao.update(complaint, wrapper);
            return RespBodyObj.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error();
        }

    }

    /**
     * 投诉事件的指派
     *
     * @param param
     * @return
     */
    public RespBodyObj<ComplainProcess> assign(ReqBodyObj<ComplainProcess> param,HttpServletRequest request ) {

        ComplainProcess process = param.getData();
        process.setEventFlowId(BaseProcessState.process_assign_code);
        //根据事件编号查询河长机构名称
        String eventNo = process.getEventNo();
        //String orgName = complaintDao.selectOrgNameByEventNo(eventNo);
        process.setEventFlowName(BaseProcessState.process_assign_zh + process.getNextDisposeUserNo());
        process.setEventTime(new Date());
        process.setDisposeUserNo(request.getHeader("userNo"));
        try {
            //向流程表中插入数据
            complainProcessDao.insert(process);
            //修改投诉事件的状态
            EntityWrapper<Complaint> wrapper = new EntityWrapper<>();
            wrapper.eq("Event_No", process.getEventNo());
            Complaint complaint = new Complaint();
            complaint.setEventStatusId(BaseProcessState.state_assign_code);
            complaint.setEventStatusName(BaseProcessState.state_assign_zh);
            complaintDao.update(complaint, wrapper);
            return RespBodyObj.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error();
        }

    }


    /**
     * 相关河长办进行投诉的处理
     * @param param
     * @return
     */
    public RespBodyObj<Object> dispose(ReqBodyObj<Map<String, Object>> param, HttpServletRequest request) {

        Map<String, Object> map = param.getData();
        String eventNo = (String) map.get("eventNo");//投诉编号
        String disposeUserNo = request.getHeader("USERNO");//处理人用户编号
        String content = (String) map.get("processContent");//处理内容
        int disposeType = Integer.valueOf(map.get("disposeType").toString());//处置方式
        int isPublic = Integer.valueOf(map.get("isPublic").toString());//是否公开
        int satisfaction = Integer.valueOf(map.get("satisfaction").toString());//满意度
        String isPublicZn = (String) map.get("isPublicZn");
        String satisfactionZn = (String) map.get("satisfactionZn");



        ComplainProcess process = new ComplainProcess();
        process.setEventNo(eventNo);
        process.setDisposeUserNo(request.getHeader("USERNO"));
        process.setProcessContent(content);
        process.setEventFlowId(BaseProcessState.process_dispose_code);
        process.setEventFlowName(BaseProcessState.process_dispose_zh);
        Date now = new Date();
        process.setEventTime(now);
        try {
            //向流程表插入数据
            complainProcessDao.insert(process);
            //修改投诉表状态
            EntityWrapper<Complaint> wrapper = new EntityWrapper<>();
            wrapper.eq("Event_No", process.getEventNo());
            Complaint complaint = new Complaint();
            complaint.setEventStatusId(BaseProcessState.state_dispose_code);
            complaint.setEventStatusName(BaseProcessState.state_dispose_zh);
            complaintDao.update(complaint, wrapper);
            //插入处理后图片以及处理详情信息
            List<ComplainPic> files = (List<ComplainPic>) map.get("files");
            List<ComplainPic> pic = JSON.parseArray(JSON.toJSONString(files),ComplainPic.class);
            if (!pic.isEmpty()) {
                for (ComplainPic filepath : pic) {
                    try {
                        ComplainPic complainPic = new ComplainPic();
                        complainPic.setEventsNo(eventNo);
                        complainPic.setPicName(filepath.getPicName());
                        complainPic.setPicPath(filepath.getPicPath());
                        complainPic.setPicType(2);
                        complainPic.setPicTypeName("处理后图片");
                        complainPic.setCreateTime(new Date());
                        //插入投诉处理图片
                        complainPicDao.insert(complainPic);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            //插入投诉处理详情数据
            Complaintdetails details = new Complaintdetails();
            details.setEventno(eventNo);
            details.setDisposecontent(content);
            details.setDisposetype(disposeType);
            details.setIspublic(isPublic);
            details.setEndtime(now);
            details.setSatisfaction(satisfaction);
            details.setIspubliczn(isPublicZn);
            details.setSatisfactionzn(satisfactionZn);
            //根据用户编号查询所属机构
            SysOrganization org = restTemplateService.getOrgByUserNo(disposeUserNo);
            details.setOrgno(org.getOrgNo());
            //计算处理历时
            //根据事件编号查询事件投诉时间
            complaint =  complaintDao.selectInfo(eventNo);
            Date startTime = complaint.getComplainTime();
            String poor = DateUtils.getDatePoor(now,startTime);
            details.setTake(poor);
            complaintdetailsDao.insert(details);

            return RespBodyObj.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.ok();
        }
    }


    /**
     * 投诉处理专员进行结案
     * @param param
     * @return
     */
    public RespBodyObj<Object> close(ReqBodyObj<ComplainProcess> param, HttpServletRequest httpServletRequest){
        ComplainProcess process = param.getData();
        process.setEventFlowId(BaseProcessState.process_close_code);
        process.setEventFlowName(BaseProcessState.process_close_zh);
        process.setEventTime(new Date());
        process.setDisposeUserNo(httpServletRequest.getHeader("userNo"));
        try {
            //向流程表中添加数据
            complainProcessDao.insert(process);
            //修改投诉表
            EntityWrapper<Complaint> wrapper = new EntityWrapper<>();
            wrapper.eq("Event_No", process.getEventNo());
            Complaint complaint = new Complaint();
            complaint.setEventStatusId(BaseProcessState.state_close_code);
            complaint.setEventStatusName(BaseProcessState.state_close_zh);
            complaintDao.update(complaint, wrapper);
            return RespBodyObj.ok();

        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }
    }

    /**
     * 投诉专员对处理情况进行驳回
     * @param param
     * @return
     */
    public RespBodyObj<Object> reject(ReqBodyObj<ComplainProcess> param,HttpServletRequest request){
        ComplainProcess process = param.getData();
        process.setEventFlowId(BaseProcessState.process_reject_code);
        process.setEventFlowName(BaseProcessState.process_reject_zh);
        process.setEventTime(new Date());
        process.setDisposeUserNo(request.getHeader("userNo"));
        try {
            //向流程表中添加数据
            complainProcessDao.insert(process);
            //修改投诉表
            EntityWrapper<Complaint> wrapper = new EntityWrapper<>();
            wrapper.eq("Event_No", process.getEventNo());
            Complaint complaint = new Complaint();
            complaint.setEventStatusId(BaseProcessState.process_assign_code);
            complaint.setEventStatusName(BaseProcessState.state_redispose_zh);
            complaintDao.update(complaint, wrapper);
            return RespBodyObj.ok();

        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }
    }


    /**
     * 根据用户编号查询待办事件列表
     * @param param
     * @return
     */
    public RespBodyObj<List<Complaint>> getPendingComplaint(ReqBodyObj<Map<String,Object>> param,HttpServletRequest request){
        List<Complaint> dataList = new ArrayList<>();
        Map<String,Object> map = param.getData();
        String userNo = request.getHeader("userNo");//用户编号
        Integer page = param.getCurrent();
        Integer rows = param.getSize();
        //根据用户编号查询用户所属机构
        //SysOrganization org = restTemplateService.getOrgByUserNo(userNo);
        try {
            if("Admin".equalsIgnoreCase(userNo)){
                PageHelper.startPage(page,rows);
                dataList = complaintDao.selectByRivers(1,null,null);
                if(dataList.isEmpty()){
                    return RespBodyObj.ok(new ArrayList<>());
                }
                PageInfo<Complaint> result = new PageInfo<>();
                if (!dataList.isEmpty()) {
                    result = new PageInfo<Complaint>(dataList);
                }
                return RespBodyObj.ok(result.getList());
            }

            RiverInfoBasis riverInfoBasis = riverInfoBasisDao.selectDivisionNoByUserNo(userNo);
            List<String> nextRiver = riverChiefService.getNextDivision(userNo);
            if(riverInfoBasis != null){
                nextRiver.add(riverInfoBasis.getDivisionNo());
            }
            if(nextRiver.size() == 0){
                return RespBodyObj.ok(dataList);
            }
            PageHelper.startPage(page,rows);
            dataList = complaintDao.selectByRivers(1,nextRiver,userNo);
            if(dataList.isEmpty()){
                return RespBodyObj.ok(new ArrayList<>());
            }
            PageInfo<Complaint> result = new PageInfo<>();
            if (!dataList.isEmpty()) {
                result = new PageInfo<Complaint>(dataList);
            }
            return RespBodyObj.ok(result.getList());
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }
    }

    public RespBodyObj getComplaint(ReqBodyObj<Map<String,Object>> param,HttpServletRequest request){
        String userNo = request.getHeader("userNo");//用户编号
        String eventNo = String.valueOf(param.getData().get("eventNo"));
        //根据用户编号查询用户所属机构
        SysOrganization org = restTemplateService.getOrgByUserNo(userNo);

        try {
            if(org == null) return RespBodyObj.ok();
            Complaint complaint = complaintDao.getComplaint(eventNo);
            //设置图片长路径
             if(complaint!=null && !complaint.getPicList().isEmpty()){
                 List<ComplainPic> picList = complaint.getPicList();
                 for (ComplainPic pic : picList) {
                     if(!StringUtils.isEmpty(pic.getPicPath())){
                         pic.setPicRealPath(fileVisitProperty.getWebFile()+pic.getPicPath());
                     }
                 }

             }
            return RespBodyObj.ok(complaint);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }
    }

    /**
     * 设置图片
     * @param dataList
     */
    private void setRealPath(List<Complaint> dataList) {
        if(!dataList.isEmpty()){
            for (Complaint complaint : dataList) {
                List<ComplainPic> picList = complaint.getPicList();
                if(!picList.isEmpty()){
                    for (ComplainPic pic : picList) {
                        if(!StringUtils.isEmpty(pic.getPicPath())){
                            pic.setPicRealPath(fileVisitProperty.getWebFile()+pic.getPicPath());
                        }
                    }
                }
            }
        }
    }
}