客户信息管理模块功能

1、客户管理

8 客户信息管理

 @RestController= @Controller  交给spring容器管理
 +@ResponseBody  每一个请求返回json数据

8.1查询客户信息管理数据

1首先逆向工程5张表

CustomerController

 @RestController
 @RequestMapping("customer")
 public class CustomerController {
     @Autowired
     private ICustmoerService custmoerService;
     @GetMapping("")
     public DatagridResult list(int page,int rows){
         return custmoerService.listByPage(page,rows);
    }
 }

ICustmoerService

 public interface ICustmoerService {
     //分页查询客户信息
     DatagridResult listByPage(int page, int rows);
 }

CustomerServiceImpl

 @Service
 public class CustomerServiceImpl implements ICustmoerService {
     @Autowired
     private TCustomerMapper customerMapper;
 
     @Override
     public DatagridResult listByPage(int page, int rows) {
         //分页查询
         //1.设置分页条件
         PageHelper.startPage(page,rows);
         //2.查询数据库 list
         List<TCustomer> customerList = customerMapper.selectByExample(null);
         //3.通过list创建PageInfo对象
         PageInfo<TCustomer> pageInfo = new PageInfo<>(customerList);
         //4.根据PageInfo对象中的属性封装DatagridResult对象并返回
         DatagridResult datagridResult = new DatagridResult();
         datagridResult.setRows(pageInfo.getList());
         datagridResult.setTotal(pageInfo.getTotal());
         return datagridResult;
    }
 }

8.2显示客户信息管理中的客户等级

逆向一张新华字典表:t_datadic

DataDicController

 @RestController
 @RequestMapping("dataDic")
 public class DataDicController {
     @Autowired
     private IDataDicService dataDicService;
     @PostMapping("selectDic")
     public List<TDatadic> selectDicByName(String name){
         return dataDicService.selectDicByName(name);
    }
 }

IDataDicService

 public interface IDataDicService {
     //查询客户信息管理中的客户等级
     List<TDatadic> selectDicByName(String name);
 }

DataDicServiceImpl

 @Service
 public class DataDicServiceImpl implements IDataDicService {
     @Autowired
     private TDatadicMapper datadicMapper;
 
     @Override
     public List<TDatadic> selectDicByName(String name) {
         TDatadicExample example = new TDatadicExample();
         example.createCriteria().andDatadicnameEqualTo(name);
         return datadicMapper.selectByExample(example);
    }
 }

8.3根据搜索框查询,显示数据

CustomerController

 @RestController
 @RequestMapping("customer")
 public class CustomerController {
     @Autowired
     private ICustmoerService custmoerService;
     @GetMapping("")
     public DatagridResult list(int page, int rows, TCustomer customer){
         return custmoerService.listByPage(page,rows,customer);
    }
 }

ICustmoerService

 public interface ICustmoerService {
     //分页查询客户信息,加搜索框查询
     DatagridResult listByPage(int page, int rows, TCustomer customer);
 }

CustomerServiceImpl

 @Service
 public class CustomerServiceImpl implements ICustmoerService {
     @Autowired
     private TCustomerMapper customerMapper;
     @Override
     public DatagridResult listByPage(int page, int rows,TCustomer customer) {
         //分页查询
         //1.设置分页条件
         PageHelper.startPage(page,rows);
         //2.查询数据库 list
         //根据前台输入的条件创建条件对象
         TCustomerExample example = new TCustomerExample();
         TCustomerExample.Criteria criteria = example.createCriteria();
         if (StringUtils.isNotBlank(customer.getKhno())){
             criteria.andKhnoEqualTo(customer.getKhno());
        }
         if (StringUtils.isNotBlank(customer.getName())){
             criteria.andNameLike("%"+customer.getName()+"%");
        }
         List<TCustomer> customerList = customerMapper.selectByExample(example);
         //3.通过list创建PageInfo对象
         PageInfo<TCustomer> pageInfo = new PageInfo<>(customerList);
         //4.根据PageInfo对象中的属性封装DatagridResult对象并返回
         DatagridResult datagridResult = new DatagridResult();
         datagridResult.setRows(pageInfo.getList());
         datagridResult.setTotal(pageInfo.getTotal());
         return datagridResult;
    }
 }

8.4新增数据,并拼接客户编号

CustomerController

 @PostMapping("")
 public Map createCus(TCustomer customer){
     //赋值客户编号
     Date date = new Date();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
     String format = sdf.format(date);
     customer.setKhno("KH"+format);
     Map map = new HashMap<>();
     try {
         customerService.createCus(customer);
         map.put("success",true);
    }catch (Exception e){
         e.printStackTrace();
         map.put("success",false);
    }
     return map;
 }

ICustomerService

 public interface ICustomerService {
     //添加客户,保存
     void createCus(TCustomer customer);
 }

CustomerServiceImpl

 @Service
 public class CustomerServiceImpl implements ICustomerService {
     @Autowired
     private TCustomerMapper customerMapper;
     @Override
     public DatagridResult listByPage(int page, int rows,TCustomer customer) {
         //分页查询
         //1.设置分页条件
         PageHelper.startPage(page,rows);
         //2.查询数据库 list
         //根据前台输入的条件创建条件对象
         TCustomerExample example = new TCustomerExample();
         TCustomerExample.Criteria criteria = example.createCriteria();
         if (StringUtils.isNotBlank(customer.getKhno())){
             criteria.andKhnoEqualTo(customer.getKhno());
        }
         if (StringUtils.isNotBlank(customer.getName())){
             criteria.andNameLike("%"+customer.getName()+"%");
        }
         List<TCustomer> customerList = customerMapper.selectByExample(example);
         //3.通过list创建PageInfo对象
         PageInfo<TCustomer> pageInfo = new PageInfo<>(customerList);
         //4.根据PageInfo对象中的属性封装DatagridResult对象并返回
         DatagridResult datagridResult = new DatagridResult();
         datagridResult.setRows(pageInfo.getList());
         datagridResult.setTotal(pageInfo.getTotal());
         return datagridResult;
    }
     //创建
     @Override
     public void createCus(TCustomer customer) {
         customerMapper.insertSelective(customer);
    }
 }

8.5客户信息管理修改删除,交往记录管理增删改查

8.5.1修改客户信息

CustomerController

 @PutMapping("{cusId}")
 public Map updateCus(@PathVariable int cusId, TCustomer customer){
     customer.setId(cusId);
     Map map = new HashMap<>();
     try {
         customerService.updateCus(customer);
         map.put("success",true);
    }catch (Exception e){
         e.printStackTrace();
         map.put("success",false);
    }
     return map;
 }

ICustomerService

 //修改客户信息
 void updateCus(TCustomer customer);

CustomerServiceImpl

 @Override
 public void updateCus(TCustomer customer) {
     customerMapper.updateByPrimaryKeySelective(customer);
 }

8.5.2删除客户信息(注意有外键,并可删除多条数据)

CustomerController

 @DeleteMapping("{ids}")
 public Map deleteCus(@PathVariable String ids){
     HashMap map = new HashMap<>();
     try {
         customerService.deleteCus(ids);
         map.put("success",true);
    }catch (Exception e){
         e.printStackTrace();
         map.put("success",true);
    }
     return map;
 }

ICustomerService

 public interface ICustomerService {
     //分页查询客户信息,加搜索框查询
     DatagridResult listByPage(int page, int rows, TCustomer customer);
     //添加客户,保存
     void createCus(TCustomer customer);
     //修改客户信息
     void updateCus(TCustomer customer);
     //删除客户信息
     void deleteCus(String ids);
 }

CustomerServiceImpl

 @Autowired
 private TCustomerContactMapper contactMapper;
 @Autowired
 private TCustomerLinkmanMapper linkmanMapper;
 @Autowired
 private TCustomerOrderMapper orderMapper;
 @Autowired
 private TOrderDetailsMapper orderDetailsMapper;
 @Override
 public void deleteCus(String ids) {
     String[] ids_strArr = ids.split(",");
     List<Integer> idsList = new ArrayList<>();
     for (String id : ids_strArr) {
         idsList.add(Integer.parseInt(id));
    }
     //删除交往记录contact
     //delete from t_customer_contant where cusid in(1,2,3,4)
     TCustomerContactExample contactExample = new TCustomerContactExample();
     contactExample.createCriteria().andCusidIn(idsList);
     contactMapper.deleteByExample(contactExample);
     //删除联系人linkman
     //delete from t_customer_linkman where cusid in(1,2,3,4)
     TCustomerLinkmanExample linkmanExample = new TCustomerLinkmanExample();
     linkmanExample.createCriteria().andCusidIn(idsList);
     linkmanMapper.deleteByExample(linkmanExample);
     //查询相关的订单,并把id随机封装到list集合中
     TCustomerOrderExample orderExample = new TCustomerOrderExample();
     orderExample.createCriteria().andCusidIn(idsList);
     List<TCustomerOrder> orderList = orderMapper.selectByExample(orderExample);
     List<Integer> orderIdsList = new ArrayList<>();
     for (TCustomerOrder order : orderList) {
         orderIdsList.add(order.getId());
    }
     if (orderIdsList.size() > 0){
         //删除订单子项orderDetail
         //delete from t_order_datils where orderid in(1,2,3,4)
         TOrderDetailsExample detailsExample = new TOrderDetailsExample();
         detailsExample.createCriteria().andOrderidIn(orderIdsList);
         orderDetailsMapper.deleteByExample(detailsExample);
         //删除订单order
         //delete from t_customer_order where cusid in(1,2,3,4)
         orderExample.createCriteria().andIdIn(orderIdsList);
         orderMapper.deleteByExample(orderExample);
    }
     //删除customer
     TCustomerExample example = new TCustomerExample();
     example.createCriteria().andIdIn(idsList);
     customerMapper.deleteByExample(example);
 }

2、客户信息管理中的交往记录管理代码

2.1、显示客户基本信息

CustomerController

 @PostMapping("findById")
 public TCustomer findById(int id){
     return customerService.selectById(id);
 }

ICustomerService

 public interface ICustomerService {
     //查询客户交往记录管理中的客户基本信息
     TCustomer selectById(int id);
 }

CustomerServiceImpl

 @Override
 public TCustomer selectById(int id) {
     return customerMapper.selectByPrimaryKey(id);
 }

2.2、显示交往记录信息管理

ContactController

 @Controller
 @RequestMapping("contact")
 public class ContactController {
     @Autowired
     private IContactService contactService;
     @PostMapping("list")
     public void list(Integer cusid, HttpServletResponse response) throws IOException {
         response.setContentType("text/html;charset=UTF-8");
         DatagridResult result = contactService.selectList(cusid);
         String jsonString = JSON.toJSONStringWithDateFormat(result, "yyyy-MM-dd");
         response.getWriter().print(jsonString);
    }
 }

IContactService

 public interface IContactService {
     //显示交往记录信息管理数据
     DatagridResult selectList(Integer cusid);
 }

ContactServiceImpl

 @Service
 public class ContactServiceImpl implements IContactService {
     @Autowired
     private TCustomerContactMapper contactMapper;
 
     @Override
     public DatagridResult selectList(Integer cusid) {
         TCustomerContactExample example = new TCustomerContactExample();
         example.createCriteria().andCusidEqualTo(cusid);
         List<TCustomerContact> list = contactMapper.selectByExample(example);
         DatagridResult datagridResult = new DatagridResult();
         datagridResult.setRows(list);
         return datagridResult;
    }
 }

2.3、新增交往记录信息

ContactController增删改查加上try,catch防止有异常

 @PostMapping("save")
 public void saveContact(TCustomerContact contact,HttpServletResponse response){
     try {
         contactService.saveContact(contact);
         String jsonString = JSON.toJSONStringWithDateFormat(contact, "yyyy-MM-dd");
         response.getWriter().print(jsonString);
    }catch (Exception e){
         e.printStackTrace();
    }
 }

IContactService

 public interface IContactService {
     //新增保存交往记录信息管理数据
     void saveContact(TCustomerContact contact);
 }

ContactServiceImpl

 @Override
 public void saveContact(TCustomerContact contact) {
     //注意:这里并没有把插入进去的id自增进去,所以去mapper加代码
     contactMapper.insertSelective(contact);
 }

写到这id不会自增,去mapper加selectKey

TCustomerContactMapper.xml中加入2-4行使加入的id自增

 <insert id="insertSelective" parameterType="com.galaxy.crm.bean.TCustomerContact" >
   <selectKey keyProperty="id" order="AFTER" resultType="int">
    select last_insert_id()
   </selectKey>
  insert into t_customer_contact
   <trim prefix="(" suffix=")" suffixOverrides="," >

写到这基本添加就完成了,但是会报400错误,我用的是@controller,springMVC默认不接收“-”的日期格式,所以去TCustomerContact类中加个

 @DateTimeFormat(pattern = "yyyy-MM-dd")
 private Date contacttime;

到这新增保存功能完成

2.4、修改删除交往记录信息管理

ContactController

 @DeleteMapping("delete")
 @ResponseBody
 public Map delectContactById(int id){
     Map map = new HashMap();
     try {
         contactService.deleteById(id);
         map.put("success",true);
    }catch (Exception e){
         e.printStackTrace();
         map.put("success",false);
    }
     return map;
 }
 @PutMapping("update")
 public void updateContact(TCustomerContact contact,HttpServletResponse response){
     try {
         response.setContentType("text/json;charset=UTF-8");
         contactService.updateContact(contact);
         String jsonString = JSON.toJSONStringWithDateFormat(contact, "yyyy-MM-dd");
         response.getWriter().print(jsonString);
    }catch (Exception e){
         e.printStackTrace();
    }
 }

IContactService

 public interface IContactService {
     //删除交往记录信息
     void deleteById(int id);
     //更新交往记录信息
     void updateContact(TCustomerContact contact);
 }

ContactServiceImpl

 @Override
 public void deleteById(int id) {
     contactMapper.deleteByPrimaryKey(id);
 }
 
 @Override
 public void updateContact(TCustomerContact contact) {
     contactMapper.updateByPrimaryKeySelective(contact);
 }
 
posted @ 2022-06-13 18:44  为了她  阅读(70)  评论(0编辑  收藏  举报