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 {
CustomerServiceImpl
@Service
public class CustomerServiceImpl implements ICustmoerService {
@Autowired
private TCustomerMapper customerMapper;
@Override
public DatagridResult listByPage(int page, int rows) {
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 {
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 {
CustomerServiceImpl
@Service
public class CustomerServiceImpl implements ICustmoerService {
@Autowired
private TCustomerMapper customerMapper;
@Override
public DatagridResult listByPage(int page, int rows,TCustomer customer) {
8.4新增数据,并拼接客户编号
CustomerController
@PostMapping("")
public Map createCus(TCustomer customer){
ICustomerService
public interface ICustomerService {
CustomerServiceImpl
@Service
public class CustomerServiceImpl implements ICustomerService {
@Autowired
private TCustomerMapper customerMapper;
@Override
public DatagridResult listByPage(int page, int rows,TCustomer 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
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 {
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));
}
2、客户信息管理中的交往记录管理代码
2.1、显示客户基本信息
CustomerController
@PostMapping("findById")
public TCustomer findById(int id){
return customerService.selectById(id);
}
ICustomerService
public interface ICustomerService {
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 {
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 {
ContactServiceImpl
@Override
public void saveContact(TCustomerContact 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 {
ContactServiceImpl
@Override
public void deleteById(int id) {
contactMapper.deleteByPrimaryKey(id);
}
@Override
public void updateContact(TCustomerContact contact) {
contactMapper.updateByPrimaryKeySelective(contact);
}