package com.zy.crm.manager.controller; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.plugins.Page; import com.core.annotations.ManagerAuth; import com.core.common.BaseRes; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.R; import com.core.exception.CoolException; import com.zy.crm.common.web.BaseController; import com.zy.crm.manager.entity.Cstmr; import com.zy.crm.manager.entity.CstmrType; import com.zy.crm.manager.service.CstmrService; import com.zy.crm.manager.service.CstmrTypeService; import com.zy.crm.system.entity.Dic; import com.zy.crm.system.service.DicService; import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.*; @Slf4j @RestController public class CstmrController extends BaseController { @Autowired private CstmrService cstmrService; @Autowired private CstmrTypeService cstmrTypeService; @Autowired private DicService dicService; @RequestMapping(value = "/cstmr/{id}/auth") @ManagerAuth public R get(@PathVariable("id") String id) { return R.ok(cstmrService.selectById(String.valueOf(id))); } @RequestMapping(value = "/cstmr/list/auth") @ManagerAuth public R list(@RequestParam(defaultValue = "1")Integer curr, @RequestParam(defaultValue = "10")Integer limit, @RequestParam(required = false)String orderByField, @RequestParam(required = false)String orderByType, @RequestParam(required = false)String condition, @RequestParam Map param){ EntityWrapper wrapper = new EntityWrapper<>(); excludeTrash(param); convert(param, wrapper); hostEq(wrapper); allLike(Cstmr.class, param.keySet(), wrapper, condition); if (!Cools.isEmpty(orderByField)){wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType));} else { wrapper.orderBy("create_time", false); } if (!Cools.isEmpty(param.get("dept_id"))) { return R.ok(cstmrService.getPage(new Page<>(curr, limit) , getHostId() , String.valueOf(param.get("dept_id")) , condition) ); } else { if (Cools.isEmpty(param.get("user_id"))) { wrapper.andNew(); wrapper.eq("user_id", getUserId()); } return R.ok(cstmrService.selectPage(new Page<>(curr, limit), wrapper)); } } private void convert(Map map, EntityWrapper wrapper){ for (Map.Entry entry : map.entrySet()){ String val = String.valueOf(entry.getValue()); if (val.contains(RANGE_TIME_LINK)){ String[] dates = val.split(RANGE_TIME_LINK); wrapper.ge(entry.getKey(), DateUtils.convert(dates[0])); wrapper.le(entry.getKey(), DateUtils.convert(dates[1])); } else { wrapper.like(entry.getKey(), val); } } } @RequestMapping(value = "/cstmr/add/auth") @ManagerAuth public R add(Cstmr cstmr) { Long hostId = getHostId(); if (cstmrService.selectByUuid(hostId, cstmr.getUuid()) != null) { throw new CoolException("客户已存在"); } cstmr.setHostId(hostId); cstmr.setUuid(cstmrService.getUuid(hostId)); // 客户代号 cstmr.setDeptId(getDeptId()); // 所属部门 cstmr.setUserId(getUserId()); // 所属人员 cstmr.setCreateBy(getUserId()); cstmr.setCreateTime(new Date()); cstmr.setUpdateBy(getUserId()); cstmr.setUpdateTime(new Date()); cstmr.setStatus(1); cstmrService.insert(cstmr); return R.ok(); } @RequestMapping(value = "/cstmr/update/auth") @ManagerAuth public R update(Cstmr cstmr){ if (Cools.isEmpty(cstmr) || null==cstmr.getId()){ return R.error(); } cstmr.setUpdateBy(getUserId()); cstmr.setUpdateTime(new Date()); cstmrService.updateById(cstmr); return R.ok(); } @RequestMapping(value = "/cstmr/delete/auth") @ManagerAuth public R delete(@RequestParam(value="ids[]") Long[] ids){ for (Long id : ids){ cstmrService.deleteById(id); } return R.ok(); } @RequestMapping(value = "/cstmr/export/auth") @ManagerAuth public R export(@RequestBody JSONObject param){ EntityWrapper wrapper = new EntityWrapper<>(); List fields = JSONObject.parseArray(param.getJSONArray("fields").toJSONString(), String.class); Map map = excludeTrash(param.getJSONObject("cstmr")); convert(map, wrapper); List list = cstmrService.selectList(wrapper); return R.ok(exportSupport(list, fields)); } @RequestMapping(value = "/cstmrQuery/auth") @ManagerAuth public R query(String condition) { EntityWrapper wrapper = new EntityWrapper<>(); wrapper.like("name", condition); Page page = cstmrService.selectPage(new Page<>(0, 10), wrapper); List> result = new ArrayList<>(); for (Cstmr cstmr : page.getRecords()){ Map map = new HashMap<>(); map.put("id", cstmr.getId()); map.put("value", cstmr.getName()); result.add(map); } return R.ok(result); } @RequestMapping(value = "/cstmr/check/column/auth") @ManagerAuth public R query(@RequestBody JSONObject param) { Wrapper wrapper = new EntityWrapper().eq(humpToLine(String.valueOf(param.get("key"))), param.get("val")); if (null != cstmrService.selectOne(wrapper)){ return R.parse(BaseRes.REPEAT).add(getComment(Cstmr.class, String.valueOf(param.get("key")))); } return R.ok(); } /*************************************** 数据相关 ***********************************************/ /** * excel导入 */ @PostMapping(value = "/cstmr/excel/import/auth") @ManagerAuth(memo = "甲方单位Excel导入") // @Transactional public R cstmrExcelImport(MultipartFile file) throws IOException { InputStream inStream = file.getInputStream(); String fileMime = file.getContentType(); int excelVersion = 2007; if ("application/vnd.ms-excel".equals(fileMime)) { excelVersion = 2003; } Workbook book = null; if (excelVersion == 2003) { book = new HSSFWorkbook(inStream); } else { // 当 excel 是 2007 时 book = new XSSFWorkbook(inStream); } Sheet sheet = book.getSheetAt(0); int totalRows = sheet.getLastRowNum() + 1; // 总 Long userId = getUserId(); Long hostId = getHostId(); Long deptId = getDeptId(); Date now = new Date(); DataFormatter dataFormatter = new DataFormatter(); for (int i = 2; i < totalRows; i++) { Cstmr cstmr = new Cstmr(); Row row = sheet.getRow(i); // 客户代号 String uuid = dataFormatter.formatCellValue(row.getCell(0)); if (!Cools.isEmpty(uuid)) { if (cstmrService.selectByUuid(hostId, uuid) != null) { // throw new CoolException(uuid + "客户代号已存在,请重新导入"); continue; } else { cstmr.setUuid(uuid); } } else { cstmr.setUuid(cstmrService.getUuid(hostId)); } // 客户代号 String name = dataFormatter.formatCellValue(row.getCell(1)); if (Cools.isEmpty(name)) { throw new CoolException("第" + i + "行客户名称不能为空,请重新导入"); } cstmr.setName(name); // 客户类别 String type = dataFormatter.formatCellValue(row.getCell(2)); if (Cools.isEmpty(type)) { throw new CoolException("第" + i + "行客户类别不能为空,请重新导入"); } CstmrType cstmrType = cstmrTypeService.selectByName(hostId, type); if (null == cstmrType) { throw new CoolException("第" + i + "行客户类别不合法,请重新导入"); } cstmr.setCstmrType(cstmrType.getId()); // 客户关系 cstmr.setRela(dataFormatter.formatCellValue(row.getCell(3))); // 电话 cstmr.setTel(dataFormatter.formatCellValue(row.getCell(4))); // 客户联系人 cstmr.setContacts(dataFormatter.formatCellValue(row.getCell(5))); // 备注 cstmr.setRemarks(dataFormatter.formatCellValue(row.getCell(6))); // 省 String province = dataFormatter.formatCellValue(row.getCell(7)); Dic dicProvince = dicService.selectByName(province); if (Cools.isEmpty(dicProvince)) { throw new CoolException("第" + i + "行省名不合法,请重新导入"); } cstmr.setProvince(dicProvince.getCode()); // 市 String city = dataFormatter.formatCellValue(row.getCell(8)); Dic dicCity = dicService.selectByName(city); if (Cools.isEmpty(dicCity)) { throw new CoolException("第" + i + "行市名不合法,请重新导入"); } cstmr.setCity(dicCity.getCode()); // 区 String district = dataFormatter.formatCellValue(row.getCell(9)); Dic dicDistrict = dicService.selectByName(district); if (Cools.isEmpty(dicDistrict)) { throw new CoolException("第" + i + "行县名不合法,请重新导入"); } cstmr.setDistrict(dicDistrict.getCode()); // 详细地址 cstmr.setAddr(dataFormatter.formatCellValue(row.getCell(10))); // 区分 cstmr.setType0(dataFormatter.formatCellValue(row.getCell(11))); cstmr.setHostId(hostId); cstmr.setDeptId(deptId); cstmr.setUserId(userId); cstmr.setStatus(1); cstmr.setCreateBy(userId); cstmr.setCreateTime(now); cstmr.setUpdateBy(userId); cstmr.setUpdateTime(now); if (!cstmrService.insert(cstmr)) { throw new CoolException("保存失败,请重新导入"); } } return R.ok(); } /** * excel导出 */ @PostMapping(value = "/cstmr/excel/export/auth") @ManagerAuth public void cstmrExcelExport(HttpServletResponse response) throws IOException { } }