Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / EventsManage / WaitToBeDoneEventController.java
package com.newfiber.api.pc.controller.EventsManage;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.pc.dao.EventsManage.*;
import com.newfiber.api.pc.model.EventsManage.*;
import com.newfiber.api.pc.model.Users;
import com.newfiber.api.pc.service.RestTemplateService;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by XuChengChao on 2017/11/30.
 */
@Controller
public class WaitToBeDoneEventController {


    @Autowired
    private ComplainPicDao complainPicDao;

    @Autowired
    private ComplainTypeDao complainTypeDao;

    @Autowired
    private AdministrativeDivisionDao administrativeDivisionDao;

    @Autowired
    private DivisionOrgDao divisionOrgDao;

    @Autowired
    private ComplaintDao complaintDao;

    @Autowired
    private EventsProcessDao eventsProcessDao;

    @Resource
    private RestTemplateService restTemplateService;


    /**
     * 递归程序(本级以及下级行政区划的编号)
     * @param divisionList
     * @return
     */
    public List<String> addUserDivisionList(List<AdministrativeDivision> divisionList){
        List<String> list = new ArrayList<>();
        if (divisionList.size()>0){

            for (AdministrativeDivision ad : divisionList){
                String id = ad.getDivisionNo();
                String text = ad.getDivisionName();
                list.add(id);
                AdministrativeDivisionQuery query = new AdministrativeDivisionQuery();
                query.createCriteria().andParentDivisionNoEqualTo(id);
                List<AdministrativeDivision> childList = administrativeDivisionDao.selectByExample(query);
                if (childList.size()>0){
                    List<String> newList=addUserDivisionList(childList);
                    for (String s : newList){
                        list.add(s);
                    }
                }

            }
        }
        return list;

    }

    /**
     * 待办事件的查询
     * @return
     */
    @PostMapping(value = "waitToDo/selectWaitTodoEvent", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj<Page<Complaint>> selectWaitToBeDoneList(HttpServletRequest request,ReqBodyObj<Map<String,Object>> param
                                              ) throws UnsupportedEncodingException, ParseException {

        Map<String,Object> map = param.getData();
        Integer eventSource = (Integer) map.get("eventSource");
        Integer eventUrgency = (Integer) map.get("eventUrgency");
        String startTime = (String) map.get("startTime");
        String endTime = (String) map.get("endTime");
        String eventType = (String) map.get("eventType");
        String str = (String) map.get("str");
        String divisionNo = (String) map.get("divisionNo");
        Integer eventStatus = (Integer) map.get("eventStatus");
        String tel = (String) map.get("tel");
        Integer page = param.getCurrent();
        Integer rows = param.getSize();

        String userNo = request.getHeader("userNo");
        //查询出系统管理员
        List<Users> managers = restTemplateService.getSystemManager();
        for (Users uu : managers) {
            if (uu.getLoginname().equals(userNo)) {
                userNo = null;
            }
        }

        List<String> typeList = new ArrayList<>();//存放用户所能够查看事件类型
        ComplaintQuery example = new ComplaintQuery();
        ComplaintQuery.Criteria criteria = example.createCriteria();
        if (eventType == null  || eventType.equals("")) {

            //查询用户所能够查看的事件类型(只有父级)
            List<ComplainTypeBasis> userType = complainTypeDao.getComplainTypeByUserNo(null);

            if (userType.size() > 0) {

                ComplainTypeQuery query = new ComplainTypeQuery();
                for (ComplainTypeBasis type : userType) {

                    String parentNo = type.getComplainTypeNo();
                    String parentName = type.getComplainTypeName();
                    query.createCriteria().andParentTypeNoEqualTo(parentNo);
                    List<ComplainTypeBasis> list = complainTypeDao.selectByExample(query);
                    if (list.size() > 0) {
                        for (ComplainTypeBasis cc : list) {
                            String id = cc.getComplainTypeNo();
                            String text = cc.getComplainTypeName();
                            typeList.add(id);
                        }
                    }
                }
            }

        } else {
            typeList.add(eventType);
        }

        //设置事件类型
        if (userNo != null || (!("".equals(eventType)) && eventType != null)){
            criteria.andComplainTypeIn(typeList);
        }

        List<String> divisionList = new ArrayList<>();

        //设置行政区划
        if (divisionNo == null || "".equals(divisionNo)){
            //查询出用户本级
            List<AdministrativeDivision> userDivisionList = divisionOrgDao.getDivisionNoByUserNo(userNo);
            divisionList = this.addUserDivisionList(userDivisionList);
            if (userNo != null){
                criteria.andDivisionNoIn(divisionList);
            }
        }else {
            divisionList.add(divisionNo);
            criteria.andDivisionNoIn(divisionList);
        }

        if (eventSource != null) {
            criteria.andEventSourceEqualTo(eventSource);
        }

        if (eventUrgency != null) {
            criteria.andComplainUrgencyEqualTo(eventUrgency);
        }
        if (eventStatus != null) {
            criteria.andEventStatusIdEqualTo(eventStatus);
        }

        if (str != null && str !="") {

            String str1 = URLDecoder.decode(str, "UTF-8");
            criteria.andEventContentLike("%" + str1 + "%");
        }
        if (tel != null && tel !="") {
            criteria.andComplainPersonTelLike("%" + tel + "%");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time1 = null;
        Date time2 = null;
        if (startTime != null && endTime != null) {
            time1 = sdf.parse(startTime);
            time2 = sdf.parse(endTime);
            criteria.andComplainTimeBetween(time1, time2);
        }
        if (page != null) {
            example.setPageNo(page);
            example.setPageSize(rows);
        }

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(5);
        list.add(6);
        criteria.andEventStatusIdNotIn(list);

        try {
            Page page1 = new Page();

            List<Complaint> complaintList = complaintDao.selectByExample(example);
            int count = complaintDao.countByExample(example);
            page1.setTotal(count);
            page1.setRecords(complaintList);
            return RespBodyObj.ok(page1);

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

    }

    /**
     * 事件批示
     *
     * @param request
     * @return
     */
    @PostMapping(value = "event/approveEvents", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj approveEvents( HttpServletRequest request,ReqBodyObj<Map<String,String>> param) {
        String userNo = request.getHeader("userNo");

        String eventNo = param.getData().get("eventNo");
        String content = param.getData().get("content");

        EventsProcess process = new EventsProcess();
        process.setDisposeUserNo(userNo);
        process.setProcessContent(content);
        process.setNextDisposeUserNo("-----");//下一处理机构
        process.setEventFlowId(2);
        process.setEventFlowName("领导批示");
        process.setEventNo(eventNo);

        try {
            int add = eventsProcessDao.insertSelective(process);
            return RespBodyObj.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error();
        }


    }


    /**
     * 事件派单
     *
     * @param request
     * @return
     */
    @PostMapping(value = "event/sendEventOrders", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj sendOrdersEvents( HttpServletRequest request,ReqBodyObj<Map<String,String>> param ) {


        String eventNo = param.getData().get("eventNo");
        String content = param.getData().get("content");
        String userNo = request.getHeader("userNo");
        if (userNo == null){
            Users users = (Users) request.getSession().getAttribute("user");
            userNo =users.getLoginname();
        }
        EventsProcess process = new EventsProcess();
        process.setEventNo(eventNo);
        process.setEventFlowId(3);
        process.setEventFlowName("派单");
        process.setProcessContent(content);
        process.setDisposeUserNo(userNo);

        Complaint event = complaintDao.selectEvetByEventNo(eventNo);
        String divisionNo = event.getDivisionNo();
        DivisionOrg org = divisionOrgDao.selectChiefOrgByDivisionNo(divisionNo, 2);//查询出管理处机构

        String nextDiposeNo = "";
        if (org != null) {
            nextDiposeNo = org.getOrgName();
        }
        process.setNextDisposeUserNo(nextDiposeNo);

        JSONObject json = new JSONObject();
        try {
            int add = eventsProcessDao.insertSelective(process);

            if (add > 0) {
                //修改事件状态
                ComplaintQuery query = new ComplaintQuery();
                query.createCriteria().andEventNoEqualTo(eventNo);

                Complaint complaint = new Complaint();
                complaint.setEventStatusId(2);
                complaint.setEventStatusName("已派单");

                int update = complaintDao.updateByExampleSelective(complaint, query);
            }

            return RespBodyObj.ok();

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

    }


    /**
     * 结案
     *
     *  eventNo 事件唯一编号
     *  flag    是否是作废案件(0:作废案件;1:正常结束案件;2:案件驳回)
     * @return
     */
    @PostMapping(value = "event/finish", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj finishCase( HttpServletRequest request,ReqBodyObj<Map<String,Object>> param) {


        String userNo = request.getHeader("userNo");

        String eventNo = (String) param.getData().get("eventNo");
        Integer flag = (Integer) param.getData().get("flag");
        String content = (String) param.getData().get("content");
        ComplaintQuery query = new ComplaintQuery();
        query.createCriteria().andEventNoEqualTo(eventNo);
        Complaint complaint = new Complaint();
        if (flag == 0) {

            complaint.setEventStatusId(6);
            complaint.setEventStatusName("作废案件");
        } else if (flag==1){
            complaint.setEventStatusId(5);
            complaint.setEventStatusName("结案");
        }else if (flag ==2){
            complaint.setEventStatusId(2);
            complaint.setEventStatusName("已派单");

        }
        JSONObject json = new JSONObject();
        try {
            //更新事件表
            int update = complaintDao.updateByExampleSelective(complaint, query);
            EventsProcess process = new EventsProcess();
            process.setDisposeUserNo(userNo);
            if (flag.equals(1)){
                process.setEventFlowId(6);
                process.setEventFlowName("结案");
            }else if(flag.equals(2)){
                process.setEventFlowId(7);
                process.setEventFlowName("驳回");
            }

            process.setNextDisposeUserNo("----");
            process.setEventNo(eventNo);
            process.setProcessContent(content);
            //添加流程表
            int add = eventsProcessDao.insertSelective(process);
            if (flag.equals(1)){
                json.put("meesage", "结案成功");
            }else {
                json.put("meesage", "已驳回");
            }


        } catch (Exception e) {
            e.printStackTrace();
            if (flag.equals(1)){
                json.put("message", "结案失败");
            }else {
                json.put("message", "驳回失败");
            }


        }
        return RespBodyObj.ok(json);

    }


    /**
     * 接单
     * @return
     */
    @PostMapping(value = "event/recieveEvent", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj recieveEvent( HttpServletRequest request,ReqBodyObj<Map<String,String>> param) {

        String userNo =request.getHeader("userNo");

        String eventNo = param.getData().get("eventNo");
        String content = param.getData().get("content");
        String userName =  restTemplateService.getUserInfo(userNo).getUserName();

        ComplaintQuery query = new ComplaintQuery();
        query.createCriteria().andEventNoEqualTo(eventNo);

        Complaint complaint = new Complaint();
        complaint.setEventStatusId(3);
        complaint.setEventStatusName("处理中");


        JSONObject json = new JSONObject();
        try {
            //更新事件表
            int update = complaintDao.updateByExampleSelective(complaint, query);

            EventsProcess process = new EventsProcess();
            process.setProcessContent(content);
            process.setEventNo(eventNo);
            process.setEventFlowId(4);
            process.setEventFlowName("接单");
            process.setDisposeUserNo(userNo);
            process.setNextDisposeUserNo(userName);
            //添加事件流程表
            int add = eventsProcessDao.insertSelective(process);
            return RespBodyObj.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return RespBodyObj.error();
        }
    }


    /**
     * 处理事件(河长反馈)
     *
     * @param request
     * @return
     */
    @PostMapping(value = "event/disposeWithEvent", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj DisposeRiver(HttpServletRequest request, ReqBodyObj<Map<String,Object>> param) {


        String eventNo = (String) param.getData().get("eventNo");
        String content = (String) param.getData().get("content");
        String [] pic = (String[]) param.getData().get("pic");

        Users users = (Users) request.getSession().getAttribute("user");
        String userNo = users.getLoginname();

        if (pic.length > 0) {

            for (int i = 0; i < pic.length; i++) {
                if (!(pic[i].isEmpty())) {
                    String path = pic[i];
                    String picName = path.substring(path.lastIndexOf("/")+1,path.length()+1);
                    String picPath = path;

                    ComplainPic eventsPic = new ComplainPic();
                    eventsPic.setPicPath(picPath);
                    eventsPic.setPicName(picName);
                    eventsPic.setPicType(2);
                    eventsPic.setPicTypeName("处理后图片");
                    eventsPic.setEventsNo(eventNo);

                    int add = complainPicDao.insertSelective(eventsPic);
                }
            }


        }
        EventsProcess process = new EventsProcess();
        process.setDisposeUserNo(userNo);
        process.setEventNo(eventNo);
        process.setEventFlowId(5);
        process.setEventFlowName("河长反馈");
        process.setProcessContent(content);
        EventsProcessQuery example = new EventsProcessQuery();
        example.createCriteria().andEventNoEqualTo(eventNo).andEventFlowIdEqualTo(1);
        List<EventsProcess> processList = eventsProcessDao.selectByExample(example);
        String nextName = "----";
        if (processList.size() > 0) {
            for (EventsProcess e : processList) {
                String name = e.getNextDisposeUserNo();
                nextName = name;
            }
        }
        process.setNextDisposeUserNo(nextName);

        JSONObject json = new JSONObject();
        try {
            //新增流程表数据
            int add = eventsProcessDao.insertSelective(process);
            if (add>0){
                ComplaintQuery query = new ComplaintQuery();
                query.createCriteria().andEventNoEqualTo(eventNo);

                Complaint complaint = new Complaint();
                complaint.setEventStatusId(4);
                complaint.setEventStatusName("待审批");

                int update = complaintDao.updateByExampleSelective(complaint,query);
                return RespBodyObj.ok("成功");
            }else {
                return RespBodyObj.ok("失败");
            }

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









}