首页>代码>java Servlet+Mybatis+Layui实现的CRM客户关系管理系统>/src/com/crsbg/controller/ContactsController.java
package com.crsbg.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.crsbg.entity.Contacts;
import com.crsbg.entity.Customer;
import com.crsbg.entity.PageVO;
import com.crsbg.entity.User;
import com.crsbg.service.ContactsService;
import com.crsbg.service.CustomerService;
import com.crsbg.service.UserService;
import com.crsbg.service.impl.ContactsServiceImpl;
import com.crsbg.service.impl.CustomerServiceImpl;
import com.crsbg.utils.JSONUtil;
import com.crsbg.utils.ServiceFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContactsController extends HttpServlet {
	private ContactsService contactsService = null;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
	   	if("/controller/getContacts".equals(path)){
		    getContacts(request,response);
        }else if("/controller/addContacts".equals(path)){
            addContacts(request,response);
        }else if("/controller/detailContacts".equals(path)){
            detailContacts(request,response);
        }else if("/controller/updateContacts".equals(path)){
            updateContacts(request,response);
        }else if("/controller/deleteContact".equals(path)){
            deleteContact(request,response);
        }else if("/controller/deleteContacts".equals(path)){
            deleteContacts(request,response);
        }else if("/controller/removeContactsActivity".equals(path)){
            removeContactsActivity(request,response);
        }else if("/controller/addContactsActivity".equals(path)){
            addContactsActivity(request,response);
        }else if("/controller/getContactsByName".equals(path)){
            getContactsByName(request,response);
        }
	}

    private void getContactsByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入查询联系人列表getContactsByName...");
        String fullname = request.getParameter("sfullname");
        contactsService = (ContactsService) ServiceFactory.getService(new ContactsServiceImpl());
        List<Contacts> contactsList = contactsService.getContactsByName(fullname);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","请求成功");
        map.put("count",contactsList.size());
        map.put("data",contactsList);
        JSONUtil.getJSON(response,map);
    }

    private void addContactsActivity(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入关联关系addContactsActivity...");
        String cid = request.getParameter("cid");
        String[] aids = request.getParameterValues("aid");
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        boolean flag = contactsService.addContactsActivity(cid,aids);
        response.getWriter().print(flag);
    }

    private void removeContactsActivity(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入解除关联关系removeContactsActivity...");
        String id = request.getParameter("id");
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        boolean flag = contactsService.removeContactsActivity(id);
        response.getWriter().print(flag);
    }

    private void deleteContacts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入批量删除联系人deleteContacts...");
        String[] ids = request.getParameterValues("id");
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        Map<String,Object> map = contactsService.deleteContacts(ids);
        JSONUtil.getJSON(response,map);
    }

    private void deleteContact(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入删除联系人deleteContact...");
        String id = request.getParameter("id");
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        Map<String,Object> map = contactsService.deleteContact(id);
        JSONUtil.getJSON(response,map);
    }

    private void updateContacts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入修改联系人updateContacts...");
        String id = request.getParameter("id");
        String fullname = request.getParameter("fullname");
        String appellation = request.getParameter("appellation");
        String customerId = request.getParameter("customerId");
        String job = request.getParameter("job");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String birth = request.getParameter("birth");
        String source = request.getParameter("source");
        String description = request.getParameter("description");
        String contactSummary = request.getParameter("contactSummary");
        String nextContactDate = request.getParameter("nextContactDate");
        String address = request.getParameter("address");
        Contacts contacts = new Contacts();
        contacts.setId(id);
        contacts.setFullname(fullname);
        contacts.setAppellation(appellation);
        contacts.setCustomerId(customerId);
        contacts.setJob(job);
        contacts.setPhone(phone);
        contacts.setEmail(email);
        contacts.setBirth(birth);
        contacts.setSource(source);
        contacts.setDescription(description);
        contacts.setContactSummary(contactSummary);
        contacts.setNextContactDate(nextContactDate);
        contacts.setAddress(address);
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        boolean flag = contactsService.updateContacts(contacts);
        response.getWriter().print(flag);
    }

    private void detailContacts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入联系人详情detailContacts...");
        String id = request.getParameter("id");
        contactsService = (ContactsService) ServiceFactory.getService(new ContactsServiceImpl());
        Contacts contacts = contactsService.getContactsById(id);
        request.setAttribute("contacts",contacts);
        request.getRequestDispatcher("/pages/contacts-detail.jsp").forward(request,response);
    }

    private void addContacts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入添加联系人addContacts...");
        String id = IdUtil.simpleUUID();
        String fullname = request.getParameter("fullname");
        String appellation = request.getParameter("appellation");
        String customerId = request.getParameter("customerId");
        String job = request.getParameter("job");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String birth = request.getParameter("birth");
        String source = request.getParameter("source");
        String description = request.getParameter("description");
        String contactSummary = request.getParameter("contactSummary");
        String nextContactDate = request.getParameter("nextContactDate");
        String address = request.getParameter("address");
        String createBy = ((User)request.getSession().getAttribute("user")).getName();
        String createTime = DateUtil.now();
        Contacts contacts = new Contacts();
        contacts.setId(id);
        contacts.setFullname(fullname);
        contacts.setAppellation(appellation);
        contacts.setCustomerId(customerId);
        contacts.setJob(job);
        contacts.setPhone(phone);
        contacts.setEmail(email);
        contacts.setBirth(birth);
        contacts.setSource(source);
        contacts.setDescription(description);
        contacts.setContactSummary(contactSummary);
        contacts.setNextContactDate(nextContactDate);
        contacts.setAddress(address);
        contacts.setCreateBy(createBy);
        contacts.setCreateTime(createTime);
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        boolean flag = contactsService.addContacts(contacts);
        response.getWriter().print(flag);
    }

    private void getContacts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入查询联系人getContacts...");
        String fullname = request.getParameter("fullname");
        String company = request.getParameter("company");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int pageNumber = (page-1)*limit;
        int pageSize = limit;
        Map<String,Object> param = new HashMap<>();
        param.put("fullname",fullname);
        param.put("company",company);
        param.put("pageNumber",pageNumber);
        param.put("pageSize",pageSize);
        contactsService = (ContactsService)ServiceFactory.getService(new ContactsServiceImpl());
        PageVO<Contacts> vo = contactsService.getContacts(param);
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("message","请求成功");
        map.put("count",vo.getTotal());
        map.put("data",vo.getDatas());

        JSONUtil.getJSON(response,map);
    }

}
最近下载更多
haozhilang  LV8 2023年11月20日
zhaozhiqi  LV5 2023年9月25日
Anxglee  LV7 2023年7月28日
hongdongdong  LV12 2023年6月17日
a970712258  LV3 2023年6月1日
彩色天空  LV5 2023年4月11日
Joooon  LV1 2023年2月26日
adminadminsqwqe  LV7 2022年10月25日
wusiyin  LV14 2022年10月13日
neuwxr2015  LV8 2022年9月30日
最近浏览更多
李俊雄 4月30日
暂无贡献等级
m5433661  LV2 3月28日
我擦嘞 3月19日
暂无贡献等级
爱丽淇  LV5 3月18日
阿凡达  LV9 1月29日
PSSDZH  LV3 1月22日
zjyoudefeng 1月15日
暂无贡献等级
lyh1989  LV34 2023年12月14日
pangzhihui  LV12 2023年12月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友