package com.crsbg.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.crsbg.entity.Customer;
import com.crsbg.entity.PageVO;
import com.crsbg.entity.User;
import com.crsbg.service.CustomerService;
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 CustomerController extends HttpServlet {
    private CustomerService customerService = null;
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/controller/getCustomer".equals(path)){
            getCustomer(request,response);
        }else if("/controller/addCustomer".equals(path)){
            addCustomer(request,response);
        }else if("/controller/detailCustomer".equals(path)){
            detailCustomer(request,response);
        }else if("/controller/updateCustomer".equals(path)){
            updateCustomer(request,response);
        }else if("/controller/deleteCustomer".equals(path)){
            deleteCustomer(request,response);
        }else if("/controller/deleteCustomers".equals(path)){
            deleteCustomers(request,response);
        }else if("/controller/getCustomerName".equals(path)){
            getCustomerName(request,response);
        }else if("/controller/getCustomerByName".equals(path)){
            getCustomerByName(request,response);
        }
    }
    private void getCustomerByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入获取客户名称getCustomerByName");
        String name = request.getParameter("keywords");
        List<Customer> customerList = null;
        if(name!=null&&!"".equals(name)){
            customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
            customerList = customerService.getCustomerByName(name);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("content",customerList);
        map.put("type","success");
        JSONUtil.getJSON(response,map);
    }
    private void getCustomerName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入获取客户名称getCustomerName");
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        List<Customer> customerList = customerService.getCustomerName();
        JSONUtil.getJSON(response,customerList);
    }
    private void deleteCustomers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入批量删除客户deleteCustomers...");
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        String[] ids = request.getParameterValues("id");
        Map<String,Object> map = customerService.deleteCustomers(ids);
        JSONUtil.getJSON(response,map);
    }
    private void deleteCustomer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入删除客户deleteCustomer...");
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        String id = request.getParameter("id");
        Map<String,Object> map = customerService.deleteCustomer(id);
        JSONUtil.getJSON(response,map);
    }
    private void updateCustomer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入修改客户updateCustomer...");
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        String id = request.getParameter("id");
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String website = request.getParameter("website");
        String tel = request.getParameter("tel");
        String contactSummary = request.getParameter("contactSummary");
        String nextContactDate =  request.getParameter("nextContactDate");
        String description = request.getParameter("description");
        String address = request.getParameter("address");
        String editBy = ((User)request.getSession().getAttribute("user")).getName();
        String editTime = DateUtil.now();
        Customer customer = new Customer();
        customer.setId(id);
        customer.setOwner(owner);
        customer.setName(name);
        customer.setWebsite(website);
        customer.setTel(tel);
        customer.setNextContactDate(nextContactDate);
        customer.setAddress(address);
        customer.setDescription(description);
        customer.setContactSummary(contactSummary);
        customer.setEditBy(editBy);
        customer.setEditTime(editTime);
        boolean flag = customerService.updateCustomer(customer);
        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        map.put("editBy",editBy);
        map.put("editTime",editTime);
        JSONUtil.getJSON(response,map);
    }
    private void detailCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入客户详情detailCustomer...");
        String id = request.getParameter("id");
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        Customer customer = customerService.getCustomerById(id);
        request.setAttribute("customer",customer);
        request.getRequestDispatcher("/pages/customer-detail.jsp").forward(request,response);
    }
    private void addCustomer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入添加客户addCustomer...");
        String id = IdUtil.simpleUUID();
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String website = request.getParameter("website");
        String tel = request.getParameter("tel");
        String nextContactDate = request.getParameter("nextContactDate");
        String address = request.getParameter("address");
        String contactSummary = request.getParameter("contactSummary");
        String description = request.getParameter("description");
        String createBy = ((User)request.getSession().getAttribute("user")).getName();
        String createTime = DateUtil.now();
        Customer customer = new Customer();
        customer.setId(id);
        customer.setOwner(owner);
        customer.setName(name);
        customer.setWebsite(website);
        customer.setTel(tel);
        customer.setNextContactDate(nextContactDate);
        customer.setAddress(address);
        customer.setDescription(description);
        customer.setContactSummary(contactSummary);
        customer.setCreateBy(createBy);
        customer.setCreateTime(createTime);
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        boolean flag = customerService.addCustomer(customer);
        response.getWriter().print(flag);
    }
    private void getCustomer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("进入查询客户getCustomer...");
        String name = request.getParameter("name");
        String owner = request.getParameter("owner");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int pageNumber = (page-1)*limit;
        int pageSize = limit;
        customerService = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        Map<String,Object> param = new HashMap<>();
        param.put("name",name);
        param.put("owner",owner);
        param.put("pageNumber",pageNumber);
        param.put("pageSize",pageSize);
        PageVO<Customer> vo = customerService.getCustomer(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);
    }
}
 最近下载更多
最近下载更多
                
                yifeng868     LV9
                6月11日
            
            
        
                微信网友_7290996505972736     LV4
                2024年12月11日
            
            
        
                255921158     LV5
                2024年9月8日
            
            
        
                TY0165     LV20
                2024年6月21日
            
            
        
                sunlea     LV20
                2024年5月12日
            
            
        
                haozhilang     LV9
                2023年11月20日
            
            
        
                zhaozhiqi     LV5
                2023年9月25日
            
            
        
                Anxglee     LV7
                2023年7月28日
            
            
        
                hongdongdong     LV14
                2023年6月17日
            
            
        
                a970712258     LV4
                2023年6月1日
            
            
         最近浏览更多
最近浏览更多
                
                刘孟飞     LV22
                8月27日
            
            
        
                yifeng868     LV9
                6月11日
            
            
        
                zhangyue2025    
                3月15日
            
            
                    暂无贡献等级
            
        
                微信网友_7290996505972736     LV4
                2024年12月11日
            
            
        
                17600446733     LV21
                2024年12月10日
            
            
        
                255921158     LV5
                2024年9月8日
            
            
        
                TY0165     LV20
                2024年6月21日
            
            
        
                15816705316    
                2024年5月31日
            
            
                    暂无贡献等级
            
        
                sunlea     LV20
                2024年5月12日
            
            
        
                李俊雄     LV3
                2024年4月30日
            
            
        
 
                 
     
                