CompanyInvestigationController.java 12.4 KB
package com.project.demo.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.demo.domain.*;
import com.project.demo.mapper.AjaxJson;
import com.project.demo.service.InvestigateService;
import com.project.demo.service.InvestigateUserService;
import com.project.demo.service.TrackingRecordService;
import com.project.demo.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bert on 2018/7/5.
 */
@Controller
@RequestMapping("investigate")
public class CompanyInvestigationController {

    @Autowired
    private InvestigateService investigateService;
    @Autowired
    private TrackingRecordService trackingRecordService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private InvestigateUserService investigateUserService;


    @RequestMapping(method = RequestMethod.GET)
    public String findAll() {
        return "/investigate/index";
    }

    @RequestMapping(value = "/investigateList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String investigateList(final HttpSession session, final HttpServletRequest request){
        Users currentUser = (Users) session.getAttribute("users");
        final Map<String,Object> params = new HashMap<>();
        DataTablePageUtil<Investigate> dataTable = new DataTablePageUtil<Investigate>(request);
        PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());//分页
        List<Investigate> investigates = investigateService.search();
        PageInfo<Investigate> pageInfo = new PageInfo<>(investigates);
         //封装数据给DataTables
         dataTable.setDraw(dataTable.getDraw());
         dataTable.setData(pageInfo.getList());
         dataTable.setRecordsTotal((int)pageInfo.getTotal());
         dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
         //返回数据到页面
         String jsonObject = JSONObject.toJSONString(dataTable);
         return jsonObject;
    }


    @RequestMapping(value = "/create")
    public String create() {
        return "/investigate/new";
    }

    // id companyName name job mobile  personnelMatters finance information marketing administration customerManager managerMobile managerEmail
    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String save(final HttpSession session, @RequestParam String companyName,@RequestParam String name,
                       @RequestParam String job,@RequestParam String mobile,@RequestParam String personnelMatters,
                       @RequestParam String finance, @RequestParam String information, @RequestParam String marketing,
                       @RequestParam String administration){
        Users users = (Users)session.getAttribute("users");
        Investigate investigate = new Investigate();
        investigate.setCompanyName(companyName);
        investigate.setName(name);
        investigate.setJob(job);
        investigate.setMobile(mobile);
        investigate.setPersonnelMatters(personnelMatters);
        investigate.setFinance(finance);
        investigate.setInformation(information);
        investigate.setMarketing(marketing);
        investigate.setAdministration(administration);
        investigate.setCustomerManager(users.getName());
        investigate.setManagerMobile(users.getMobile());
       // investigate.setManagerEmail(managerEmail);
        investigate.setUserId(users.getId());
        investigateService.save(investigate);

        return "redirect:/investigate";
    }

    @RequestMapping(value = "/isExist",method = RequestMethod.POST,  produces = "application/json")
    @ResponseBody
    public Result isExist(Model model, @RequestParam String mobile){
        Result result = new Result();
            result.setMessage("<font color='green'>添加成功</font>");
            result.setSuccess(true);
        return result;
    }


    @RequestMapping(value = "/{companyId}/details", method = RequestMethod.GET)
    public String showDetails(final HttpSession session, @PathVariable Integer companyId, Model model)
    {
        //通过companyId查询相关信息
        Investigate investigate = investigateService.searchDetails(companyId);
        model.addAttribute("customerManager", investigate.getCustomerManager());
        model.addAttribute("managerMobile", investigate.getManagerMobile());
        model.addAttribute("mobile", investigate.getMobile());
        model.addAttribute("personnelMatters", investigate.getPersonnelMatters());//人事
        model.addAttribute("finance", investigate.getFinance());
        model.addAttribute("information", investigate.getInformation());
        model.addAttribute("marking", investigate.getMarketing());
        model.addAttribute("administration", investigate.getAdministration());
        model.addAttribute("thisInvestigate", investigate);
        model.addAttribute("name", investigate.getName());
        final Map<String,Object> params = new HashMap<>();
        params.put("comInvestigationId", companyId);

        final Map<String,Object> params2 = new HashMap<>();
        params2.put("comInvestigationId",companyId);
        List<InvestigateUser> investigateUserList =  investigateUserService.search(params);
        investigateUserList.stream().forEach(d ->{
            Users users = usersService.findById(d.getUsersId());
            d.setName(users.getName());
            d.setMobile(users.getMobile());
        });
        List<String> investigateUserListForShow = new ArrayList<>();
        for (int i=0;i<investigateUserList.size();i++){
            investigateUserListForShow.add(investigateUserList.get(i).getName());
            investigateUserListForShow.add(investigateUserList.get(i).getMobile());
        }
        if (investigateUserListForShow.toString().contains("[")){
            String listString = investigateUserListForShow.toString().replace("[","");
            String listForShow  = listString.replace("]","");
            model.addAttribute("investigateUserList", listForShow);
        }

        return "/investigate/details";
    }

    //跟踪记录显示
    @RequestMapping(value = "/projectContent",method = RequestMethod.POST)
    @ResponseBody
    public Investigate projectContent(HttpSession session , @RequestParam Integer investigateId){
        Users currentUser = (Users) session.getAttribute("users");
        Investigate investigate = investigateService.searchDetails(investigateId);
//        ProjectsValue projectsValue = projectsValueService.findByProjectsId(projects.getId());
//        projects.setProjectsValue(projectsValue);
        return investigate;
    }


    //创建跟踪记录
    @RequestMapping(value = "/addTracks",method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson addTracks(HttpSession session , @RequestParam Integer investigateId, @RequestParam String investigateTracks){
        AjaxJson ajaxJson = new AjaxJson();
        Users currentUser = (Users) session.getAttribute("users");
        InvestigateTrackingRecord trackingRecord = new InvestigateTrackingRecord();
        trackingRecord.setInvestigateId(investigateId);
        trackingRecord.setUsersId(currentUser.getId());
        trackingRecord.setContent(investigateTracks);
        System.out.println("trackingRecord:"+ trackingRecord.toString());
        trackingRecordService.insertForInvestigate(trackingRecord);
        ajaxJson.setCode(0);
        ajaxJson.setMsg("添加成功");
        return ajaxJson;
    }

    //已关联人员删除
    @RequestMapping(value = "/salesList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String salesList(final HttpSession session, final HttpServletRequest request,@RequestParam Integer investigateId){
        Users currentUser = (Users) session.getAttribute("users");
        final Map<String,Object> params = new HashMap<>();
        DataTablePageUtil<InvestigateUser> dataTable = new DataTablePageUtil<InvestigateUser>(request);
        System.out.println(dataTable.getPage_num()+"||"+dataTable.getPage_size());
        params.put("comInvestigationId",investigateId);
        List<InvestigateUser> investigateUserList =  investigateUserService.search(params);
        System.out.println("investigateUserList.size:"+investigateUserList.size()); //2
        investigateUserList.stream().forEach(d ->{
            System.out.println("userId:" + d.getUsersId());
            Users users = usersService.findById(d.getUsersId());
            d.setName(users.getName());
            d.setMobile(users.getMobile());
        });
        PageInfo<InvestigateUser> pageInfo = new PageInfo<>(investigateUserList, 2);
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        String jsonObject = JSONObject.toJSONString(dataTable);
        return jsonObject;
    }

    //usersList
    @RequestMapping(value = "/usersList",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String usersList(final HttpSession session, final HttpServletRequest request,@RequestParam Integer investigateId){
        Users currentUser = (Users) session.getAttribute("users");
        final Map<String,Object> params = new HashMap<>();
        //获取当前项目关联的人员和创建人员
        Integer createManId= investigateService.findById(investigateId).getUserId();  //创建人
        Users createUsers = usersService.findById(createManId);
        //找到粗了创建人跟已关联的
        params.put("comInvestigationId", investigateId);
        List<InvestigateUser> investigateUserList =  investigateUserService.search(params);
        List<String> mobiles = new ArrayList<>();
        for(int i =0;i<investigateUserList.size();i++){
            mobiles.add(usersService.findById(investigateUserList.get(i).getUsersId()).getMobile());
        }
        mobiles.add(createUsers.getMobile());
        params.put("mobiles",mobiles);
        DataTablePageUtil<Users> dataTable = new DataTablePageUtil<Users>(request);
        PageHelper.startPage(dataTable.getPage_num(),dataTable.getPage_size());
        List<Users> usersList = usersService.search(params);
        PageInfo<Users> pageInfo = new PageInfo<>(usersList, 2);
        //封装数据给DataTables
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面
        String jsonObject = JSONObject.toJSONString(dataTable);
        return jsonObject;
    }

    @RequestMapping(value = "/addSales",method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson addSales(HttpSession session , @RequestParam Integer investigateId, @RequestParam String mobiles){
        AjaxJson ajaxJson = new AjaxJson();
        String[] mobilesArray = mobiles.split(",");
        Users users ;
        InvestigateUser investigateUser = new InvestigateUser();
        for (int i = 0; i<mobilesArray.length; i++){
            users = usersService.findByMobile(mobilesArray[i]);
            investigateUser.setUsersId(users.getId());
            investigateUser.setComInvestigationId(investigateId);
            investigateUserService.insert(investigateUser);
        }
        ajaxJson.setCode(0);
        ajaxJson.setMsg("添加成功");
        return ajaxJson;
    }

    @RequestMapping(value = "/removeSales",method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson removeSales(HttpSession session , @RequestParam Integer investigateId, @RequestParam String mobiles){
        String[] mobilesArray = mobiles.split(",");
        AjaxJson ajaxJson = new AjaxJson();
        Users users = new Users();
        for (int i = 0; i<mobilesArray.length; i++) {
            users = usersService.findByMobile(mobilesArray[i]);
            investigateUserService.delete(users.getId(),investigateId);
        }
        ajaxJson.setCode(0);
        ajaxJson.setMsg("删除成功");
        return ajaxJson;
    }

}