package com.linkwechat.controller; import cn.hutool.core.collection.ListUtil; import com.alibaba.fastjson.JSONObject; import com.linkwechat.common.annotation.Log; import com.linkwechat.common.annotation.PhoneEncryptMethod; import com.linkwechat.common.constant.Constants; import com.linkwechat.common.constant.SynchRecordConstants; import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.core.controller.BaseController; import com.linkwechat.common.core.domain.AjaxResult; import com.linkwechat.common.core.page.TableDataInfo; import com.linkwechat.common.core.page.TableSupport; import com.linkwechat.common.enums.BusinessType; import com.linkwechat.common.exception.CustomException; import com.linkwechat.common.utils.ServletUtils; import com.linkwechat.common.utils.poi.LwExcelUtil; import com.linkwechat.domain.WeCustomer; import com.linkwechat.domain.WeCustomerSeas; import com.linkwechat.domain.customer.WeBacthMakeCustomerTag; import com.linkwechat.domain.customer.WeMakeCustomerTag; import com.linkwechat.domain.customer.query.WeCustomersQuery; import com.linkwechat.domain.customer.query.WeOnTheJobCustomerQuery; import com.linkwechat.domain.customer.vo.WeCustomerDetailInfoVo; import com.linkwechat.domain.customer.vo.WeCustomersVo; import com.linkwechat.service.IWeCustomerService; import com.linkwechat.service.IWeSynchRecordService; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; /** * 企业微信客户Controller * * @author ruoyi * @date 2020-09-13 */ @Slf4j @RestController @RequestMapping("/customer") public class WeCustomerController extends BaseController { @Autowired private IWeCustomerService weCustomerService; @Autowired private IWeSynchRecordService iWeSynchRecordService; /** * 查询企业微信客户列表(分页),索引优化客户列表,未使用默认分页 */ @GetMapping("/findWeCustomerList") public TableDataInfo findWeCustomerList(WeCustomersQuery weCustomersQuery) { weCustomersQuery.setDelFlag(null == weCustomersQuery.getDelFlag() ? Constants.COMMON_STATE : weCustomersQuery.getDelFlag()); List list = weCustomerService.findWeCustomerList(weCustomersQuery, TableSupport.buildPageRequest()); TableDataInfo dataTable = getDataTable(list); //设置总条数 dataTable.setTotal( weCustomerService.countWeCustomerList(weCustomersQuery) ); dataTable.setLastSyncTime( iWeSynchRecordService.findUpdateLatestTime(SynchRecordConstants.SYNCH_CUSTOMER) );//最近同步时间 dataTable.setNoRepeatCustomerTotal( weCustomerService.noRepeatCountCustomer(weCustomersQuery) );//去重客户数 return dataTable; } /** * 导出企业客户 */ @GetMapping("/exprotWeCustomerList") public void exprotWeCustomerList(WeCustomersQuery weCustomersQuery) { weCustomersQuery.setDelFlag(null == weCustomersQuery.getDelFlag() ? Constants.COMMON_STATE : weCustomersQuery.getDelFlag()); List list = weCustomerService.findWeCustomerList(weCustomersQuery, null); LwExcelUtil.exprotForWeb( ServletUtils.getResponse(), WeCustomersVo .class, list,"企业客户" ); } /** * 移动应用客户列表(分页) * @param weCustomersQuery * @return */ @GetMapping("/findWeCustomerListByApp") public TableDataInfo> findWeCustomerListByApp(WeCustomersQuery weCustomersQuery){ return weCustomerService.findWeCustomerListByApp( weCustomersQuery,TableSupport.buildPageRequest() ); } /** * 查询企业微信客户列表(不分页) */ @GetMapping("/findAllWeCustomerList") public AjaxResult findAllWeCustomerList(WeCustomersQuery weCustomersQuery) { weCustomersQuery.setDelFlag(null == weCustomersQuery.getDelFlag() ? Constants.COMMON_STATE : weCustomersQuery.getDelFlag()); return AjaxResult.success( weCustomerService.findWeCustomerList(weCustomersQuery,null) ); } /** * 客户同步接口 * @return */ @Log(title = "企业微信客户同步接口", businessType = BusinessType.DELETE) @GetMapping("/synchWeCustomer") public AjaxResult synchWeCustomer() { try { weCustomerService.synchWeCustomer(); }catch (CustomException e){ log.error("客户同步失败:ex:{}",e.getMessage(),e); return AjaxResult.error(e.getMessage()); } return AjaxResult.success(WeConstans.SYNCH_TIP); } /** * 客户打标签 * @param weMakeCustomerTag * @return */ @Log(title = "客户打标签", businessType = BusinessType.UPDATE) @PostMapping("/makeLabel") public AjaxResult makeLabel(@RequestBody WeMakeCustomerTag weMakeCustomerTag){ weCustomerService.makeLabel(weMakeCustomerTag); return AjaxResult.success(); } /** * 在职继承 * @param weOnTheJobCustomerQuery * @return */ @Log(title="在职继承",businessType = BusinessType.UPDATE) @PostMapping("/jobExtends") public AjaxResult jobExtends(@RequestBody WeOnTheJobCustomerQuery weOnTheJobCustomerQuery){ try { weCustomerService.allocateOnTheJobCustomer( weOnTheJobCustomerQuery ); }catch (Exception e){ e.printStackTrace(); return AjaxResult.error(e.getMessage()); } return AjaxResult.success(); } /********************************************************************************** **************************************客户详情************************************* *********************************************************************************/ /** * 客户详情基础(基础信息+社交关系) * @param externalUserid * @param userId * @param * @return */ @GetMapping("/findWeCustomerBaseInfo") public AjaxResult findWeCustomerBaseInfo(String externalUserid, String userId, @RequestParam(defaultValue = "0") Integer delFlag){ return AjaxResult.success( weCustomerService.findWeCustomerDetail(externalUserid,userId,null) ); } /** * 客户画像汇总 * @param externalUserid * @return */ @GetMapping("/findWeCustomerInfoSummary") public AjaxResult findWeCustomerInfoSummary(String externalUserid,@RequestParam(defaultValue = "0") Integer delFlag){ return AjaxResult.success( weCustomerService.findWeCustomerInfoSummary(externalUserid,null,null) ); } /** * 单个跟进人客户 * @param externalUserid * @param weUserId * @return */ @GetMapping("/findWeCustomerInfoByUserId") public AjaxResult findWeCustomerInfoByUserId(String externalUserid,String weUserId,@RequestParam(defaultValue = "0") Integer delFlag){ return AjaxResult.success( weCustomerService.findWeCustomerInfoByUserId(externalUserid,weUserId,null) ); } // /** // * 跟进记录 // * @param externalUserid // * @param userId // * @return // */ // @GetMapping("/followUpRecord") // public TableDataInfo followUpRecord(String externalUserid,String userId){ // // startPage(); // // return getDataTable( iWeCustomerTrackRecordService.list(new LambdaQueryWrapper() // .eq(WeCustomerTrackRecord::getExternalUserid,externalUserid) // .eq(WeCustomerTrackRecord::getWeUserId,userId))); // } // /** // * 客户轨迹 // * @param externalUserid // * @param weUserId // * @param trajectoryType 轨迹类型:1:信息动态;2:社交动态;3:活动规则;4:待办动态 // * @return // */ // @GetMapping("/findTrajectory") // public AjaxResult findTrajectory(String externalUserid,String weUserId,Integer trajectoryType){ // // List weCustomerTrajectories = iWeCustomerTrajectoryService // .list(new LambdaQueryWrapper() // .eq(StringUtils.isNotEmpty(externalUserid),WeCustomerTrajectory::getExternalUseridOrChatid, externalUserid) // .eq(StringUtils.isNotEmpty(weUserId),WeCustomerTrajectory::getWeUserId, weUserId) // .eq(trajectoryType !=null,WeCustomerTrajectory::getTrajectoryType,trajectoryType) // ); // // return AjaxResult.success(weCustomerTrajectories); // } @ApiOperation(value = "通过条件校验客户数据", httpMethod = "POST") @PostMapping("/checkByCondition") public AjaxResult checkByCondition(@RequestBody WeCustomersQuery query){ log.info("checkByCondition 接口入参:query:{}", JSONObject.toJSONString(query)); List customerList = weCustomerService.getCustomerListByCondition(query); Set externalUserIdSet = Optional.ofNullable(customerList).orElseGet(ArrayList::new) .stream().map(WeCustomer::getExternalUserid).collect(Collectors.toSet()); Set userNum = Optional.ofNullable(customerList).orElseGet(ArrayList::new) .stream().map(WeCustomer::getAddUserId).collect(Collectors.toSet()); JSONObject result = new JSONObject(); result.put("customerNum",externalUserIdSet.size()); result.put("userNum",userNum.size()); return AjaxResult.success(result); } /** * 批量添加或删除客户标签 * @param makeCustomerTags * @return */ @Log(title = "批量添加或删除客户标签", businessType = BusinessType.UPDATE) @PostMapping("/batchMakeLabel") public AjaxResult batchMakeLabel(@RequestBody WeBacthMakeCustomerTag makeCustomerTags){ weCustomerService.batchMakeLabel(makeCustomerTags); return AjaxResult.success(); } }