CrmController.java 79 KB
Newer Older
邓实川 committed
1 2
package cn.timer.api.controller.crm;

邓实川 committed
3
import java.io.IOException;
4 5
import java.io.InputStream;
import java.net.URL;
6
import java.util.ArrayList;
邓实川 committed
7
import java.util.Comparator;
邓实川 committed
8
import java.util.Date;
9
import java.util.HashMap;
10
import java.util.List;
11 12
import java.util.Map;
import java.util.stream.Collectors;
邓实川 committed
13

邓实川 committed
14
import org.springframework.beans.factory.annotation.Autowired;
邓实川 committed
15
import org.springframework.transaction.annotation.Transactional;
16
import org.springframework.web.bind.annotation.DeleteMapping;
邓实川 committed
17 18 19 20
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestParam;
邓实川 committed
22 23
import org.springframework.web.bind.annotation.RestController;

邓实川 committed
24
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
邓实川 committed
25
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
26
import com.baomidou.mybatisplus.core.metadata.IPage;
邓实川 committed
27
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
lal committed
28
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
29
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
ilal committed
30 31
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
邓实川 committed
32

ilal committed
33
import cn.hutool.core.bean.BeanUtil;
34
import cn.hutool.core.collection.CollUtil;
35
import cn.hutool.core.date.DateUtil;
36
import cn.hutool.core.util.StrUtil;
邓实川 committed
37 38
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
39 40 41 42
import cn.timer.api.bean.clazz.ClientSourceClass;
import cn.timer.api.bean.clazz.ClientTypeClass;
import cn.timer.api.bean.clazz.IndustryClass;
import cn.timer.api.bean.clazz.SysRegion;
邓实川 committed
43 44
import cn.timer.api.bean.crm.CrmBusinessGroup;
import cn.timer.api.bean.crm.CrmBusinessGroupMember;
45 46
import cn.timer.api.bean.crm.CrmClientAssociate;
import cn.timer.api.bean.crm.CrmClientContacts;
邓实川 committed
47
import cn.timer.api.bean.crm.CrmClientData;
48 49
import cn.timer.api.bean.crm.CrmClientFollow;
import cn.timer.api.bean.crm.CrmSeaRule;
50
import cn.timer.api.bean.qyzx.QyzxEmpEntAsso;
邓实川 committed
51 52 53
import cn.timer.api.bean.yggl.YgglMainEmp;
import cn.timer.api.config.annotation.CurrentUser;
import cn.timer.api.config.annotation.UserBean;
54
import cn.timer.api.config.enums.SysRoleType;
ilal committed
55
import cn.timer.api.controller.kqgl.ClockInTool;
邓实川 committed
56
import cn.timer.api.dao.crm.CrmClientContactsMapper;
邓实川 committed
57
import cn.timer.api.dao.crm.CrmClientDataMapper;
lal committed
58
import cn.timer.api.dao.qyzx.QyzxEmpEntAssoMapper;
59
import cn.timer.api.dto.crm.CrmCartogramDto;
邓实川 committed
60
import cn.timer.api.dto.crm.CrmClientDataImportVo;
ilal committed
61
import cn.timer.api.dto.kqmk.DailyDetailsDto;
ilal committed
62
import cn.timer.api.dto.xcgl.ImportDescriptionDto;
63
import cn.timer.api.utils.CheckUtil;
邓实川 committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
import cn.timer.api.utils.Result;
import cn.timer.api.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Title: CrmController.java
 * 
 * @Description:
 * @author dsc
 * @date 2020年6月5日
 * @version 1.0
 */

@RestController
@Api(tags = "6.1客户管理")
@Transactional
@RequestMapping(value = "/crm", produces = { "application/json" })
public class CrmController {

84 85
//	@Value(value = "${config-8timer.crm-excel.realPath}")
//	private String realPath;
邓实川 committed
86

邓实川 committed
87 88
	@Autowired
	private CrmClientDataMapper crmClientDataMapper;
89

邓实川 committed
90 91
	@Autowired
	private CrmClientContactsMapper crmClientContactsMapper;
邓实川 committed
92

邓实川 committed
93 94 95 96 97 98 99 100
	private Integer getEmpNum(UserBean userBean) {
		return userBean.getEmpNum();
	}

	private Integer getOrgCode(UserBean userBean) {
		return userBean.getOrgCode();
	}

101
	private YgglMainEmp getYg(Integer orgCode, Integer empNum) {
102 103 104
		return YgglMainEmp.builder().build()
				.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getEmpNum, empNum)
						.eq(YgglMainEmp::getOrgCode, orgCode).select(YgglMainEmp::getName, YgglMainEmp::getHeadUrl));
105
	}
邓实川 committed
106

107 108 109 110
	private String getEmpName(Integer orgCode, Integer empNum) {
		return getYg(orgCode, empNum).getName();
	}

邓实川 committed
111 112 113 114 115 116
//	private Integer getQyzxManager(Integer orgCode, Integer empNum) {
//		return YgglMainEmp.builder().build()
//				.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getOrgCode, orgCode)
//						.eq(YgglMainEmp::getEmpNum, empNum).select(YgglMainEmp::getIsManager))
//				.getIsManager();
//	}
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	@GetMapping("getIndustry")
	@ApiOperation(value = "获取行业列表", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<IndustryClass>> getIndustry(@CurrentUser UserBean userBean) {
		return ResultUtil.data(IndustryClass.builder().build().selectAll());

	}

	@GetMapping("getClientType")
	@ApiOperation(value = "获取客户类型", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<ClientTypeClass>> getClientType(@CurrentUser UserBean userBean) {
		return ResultUtil.data(ClientTypeClass.builder().build().selectAll());

	}

	@GetMapping("getClientSource")
	@ApiOperation(value = "获取客户来源", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<ClientSourceClass>> getClientSource(@CurrentUser UserBean userBean) {
		return ResultUtil.data(ClientSourceClass.builder().build().selectAll());
邓实川 committed
136 137 138 139
	}

	@PostMapping("addBusinessGroup")
	@Transactional
140
	@ApiOperation(value = "新增/编辑业务组", httpMethod = "POST", notes = "接口发布说明")
邓实川 committed
141 142 143 144
	public Result<Object> addBusinessGroup(@CurrentUser UserBean userBean,
			@RequestBody CrmBusinessGroup crmBusinessGroup) {
		Integer orgCode = getOrgCode(userBean);
		Integer empNum = getEmpNum(userBean);
145 146 147 148 149 150 151 152 153 154 155
		CrmSeaRule crmSeaRule = crmBusinessGroup.getCrmSeaRules();
		Integer one = crmSeaRule.getOne();
		Integer two = crmSeaRule.getTwo();
		Integer three = crmSeaRule.getThree();
		Integer four = crmSeaRule.getFour();
		if ((one != null && one < 1) || (two != null && two < 1) || (three != null && three < 1)
				|| (four != null && four < 1))
			return ResultUtil.error("请输入正确的规则天数");
		if (three <= one)
			return ResultUtil.error("该规则天数必须大于规则1的天数");

邓实川 committed
156
		Integer gid = crmBusinessGroup.getId();
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		crmBusinessGroup.setModifyUser(empNum); // 修改人
		if (gid == null) {
			CrmBusinessGroup crmBusinessGroupOld = CrmBusinessGroup.builder().build()
					.selectOne(new QueryWrapper<CrmBusinessGroup>().lambda().eq(CrmBusinessGroup::getOrgCode, orgCode)
							.select(CrmBusinessGroup::getSort).last("limit 1").orderByDesc(CrmBusinessGroup::getId));
			if (crmBusinessGroupOld == null)
				crmBusinessGroup.setSort(1);
			else
				crmBusinessGroup.setSort(crmBusinessGroupOld.getSort() + 1);// 排序
			crmBusinessGroup.setCreateUser(empNum); // 创建人
			crmBusinessGroup.setOrgCode(orgCode);
			boolean result = crmBusinessGroup.insert();
			if (!result)
				return ResultUtil.success("新增失败");
			gid = crmBusinessGroup.getId();
			List<Integer> memberIds = crmBusinessGroup.getMemberIds();
			for (Integer empId : memberIds) {
				insertMembers(gid, empId, orgCode, 0);
			}
			List<Integer> managerIds = crmBusinessGroup.getManagerIds();
			for (Integer empId : managerIds) {
				insertMembers(gid, empId, orgCode, 1);
			}
			crmSeaRule.setGid(gid);
			crmSeaRule.insert();
邓实川 committed
182
			return ResultUtil.success("新增业务组成功");
183
		} else {
邓实川 committed
184
			crmBusinessGroup.updateById();
185 186 187 188 189 190
			List<Integer> memberIds = crmBusinessGroup.getMemberIds();
			List<Integer> managerIds = crmBusinessGroup.getManagerIds();
			// 更新组成员
			updateMembers(orgCode, gid, memberIds, 0);
			updateMembers(orgCode, gid, managerIds, 1);
			// 更新公海规则
191
			crmSeaRule.update(new LambdaQueryWrapper<CrmSeaRule>().eq(CrmSeaRule::getGid, gid));
邓实川 committed
192
			return ResultUtil.success("编辑业务组成功");
邓实川 committed
193
		}
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	}

	private void insertMembers(Integer gid, Integer empId, Integer orgCode, Integer type) {
		CrmBusinessGroupMember.builder().gid(gid).empNum(empId).orgCode(orgCode).type(type).build().insert();
	}

	private void updateMembers(Integer orgCode, Integer gid, List<Integer> memberIds, Integer type) {
		List<CrmBusinessGroupMember> oldMembers = CrmBusinessGroupMember.builder().build()
				.selectList(new LambdaQueryWrapper<CrmBusinessGroupMember>().eq(CrmBusinessGroupMember::getGid, gid)
						.eq(CrmBusinessGroupMember::getType, type).select(CrmBusinessGroupMember::getEmpNum));
		// 旧组成员idlist
		List<Integer> oldIdsList = oldMembers.stream().map(CrmBusinessGroupMember::getEmpNum)
				.collect(Collectors.toList());

		List<Integer> deList = CheckUtil.reduce(oldIdsList, memberIds); // 删
		for (Integer integer : deList) {
			CrmBusinessGroupMember.builder().build()
					.delete(new LambdaQueryWrapper<CrmBusinessGroupMember>()
							.eq(CrmBusinessGroupMember::getEmpNum, integer).eq(CrmBusinessGroupMember::getGid, gid)
							.eq(CrmBusinessGroupMember::getOrgCode, orgCode).eq(CrmBusinessGroupMember::getType, type));
215 216
		}

217 218 219 220
		List<Integer> addList = CheckUtil.reduce(memberIds, oldIdsList); // 增
		for (Integer integer : addList) {
			CrmBusinessGroupMember.builder().empNum(integer).gid(gid).orgCode(orgCode).type(type).build().insert();
		}
邓实川 committed
221 222 223
	}

	@GetMapping("getBusinessGroup")
224
	@ApiOperation(value = "获取业务组列表", httpMethod = "GET", notes = "接口发布说明")
邓实川 committed
225
	public Result<Object> getBusinessGroup(@CurrentUser UserBean userBean) {
226 227
		Integer nowEmpNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);
邓实川 committed
228

229 230 231 232 233 234 235 236 237
//		YgglMainEmp yg = YgglMainEmp.builder().build()
//				.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getOrgCode, orgCode)
//						.eq(YgglMainEmp::getEmpNum, nowEmpNum).select(YgglMainEmp::getIsManager));
		QyzxEmpEntAsso qyzxEmpEntAsso = QyzxEmpEntAsso.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxEmpEntAsso>().eq(QyzxEmpEntAsso::getOrgCode, orgCode)
						.eq(QyzxEmpEntAsso::getEmpNum, nowEmpNum));
		if (qyzxEmpEntAsso != null && qyzxEmpEntAsso.getUserType() != null
				&& (qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_ADMIN.getType()
						|| qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_C_ADMIN.getType())) {
238 239 240 241 242 243 244 245 246 247 248

			List<CrmBusinessGroup> list = CrmBusinessGroup.builder().build()
					.selectList(new QueryWrapper<CrmBusinessGroup>().lambda()
							.eq(CrmBusinessGroup::getOrgCode, getOrgCode(userBean)).select(CrmBusinessGroup::getId,
									CrmBusinessGroup::getSort, CrmBusinessGroup::getName,
									CrmBusinessGroup::getModifyTime));
			for (CrmBusinessGroup crmBusinessGroup : list) {
				Integer gid = crmBusinessGroup.getId();
				List<CrmBusinessGroupMember> crmBusinessGroupManagers = CrmBusinessGroupMember.builder().build()
						.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda()
								.eq(CrmBusinessGroupMember::getGid, gid).eq(CrmBusinessGroupMember::getType, 1)
249
								.eq(CrmBusinessGroupMember::getOrgCode, getOrgCode(userBean))
250 251
								.select(CrmBusinessGroupMember::getEmpNum));
				List<CrmBusinessGroupMember> crmBusinessGroupMembers = CrmBusinessGroupMember.builder().build()
252

253 254
						.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda()
								.eq(CrmBusinessGroupMember::getGid, gid).eq(CrmBusinessGroupMember::getType, 0)
255
								.eq(CrmBusinessGroupMember::getOrgCode, getOrgCode(userBean))
256 257 258 259
								.select(CrmBusinessGroupMember::getEmpNum));
				List<Map<String, Object>> managers = new ArrayList<Map<String, Object>>();
				for (CrmBusinessGroupMember crmBusinessGroupManager : crmBusinessGroupManagers) {
					Integer empNum = crmBusinessGroupManager.getEmpNum();
ilal committed
260 261 262 263 264 265 266 267 268 269 270
					
					YgglMainEmp emps = YgglMainEmp.builder().build()
							.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getEmpNum, empNum)
									.eq(YgglMainEmp::getOrgCode, orgCode).select(YgglMainEmp::getName, YgglMainEmp::getHeadUrl));
					if(emps != null) {
						String name = getEmpName(orgCode, empNum);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("name", name);
						map.put("empNum", empNum);
						managers.add(map);
					}
271 272 273 274 275 276
				}
				crmBusinessGroup.setManagers(managers);

				List<Map<String, Object>> members = new ArrayList<Map<String, Object>>();
				for (CrmBusinessGroupMember crmBusinessGroupManager : crmBusinessGroupMembers) {
					Integer empNum = crmBusinessGroupManager.getEmpNum();
ilal committed
277 278 279 280 281 282 283 284 285 286 287
					
					YgglMainEmp emps = YgglMainEmp.builder().build()
					.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getEmpNum, empNum)
							.eq(YgglMainEmp::getOrgCode, orgCode).select(YgglMainEmp::getName, YgglMainEmp::getHeadUrl));
					if(emps != null) {
						String name = getEmpName(orgCode, empNum);
						Map<String, Object> map = new HashMap<String, Object>();
						map.put("name", name);
						map.put("empNum", empNum);
						members.add(map);
					}
288 289 290 291 292 293
				}
				crmBusinessGroup.setMembers(members);

				CrmSeaRule crmSeaRules = CrmSeaRule.builder().build()
						.selectOne(new QueryWrapper<CrmSeaRule>().lambda().eq(CrmSeaRule::getGid, gid));
				crmBusinessGroup.setCrmSeaRules(crmSeaRules);
294
			}
295 296 297 298
			return ResultUtil.data(list, "获取成功");

		} else
			return ResultUtil.error("暂无查看权限");
299

300 301
	}

302 303 304
	@GetMapping("getBusinessGroupId")
	@ApiOperation(value = "获取业务组列表(id,name)", httpMethod = "GET", notes = "接口发布说明")
	public Result<Object> getBusinessGroupId(@CurrentUser UserBean userBean) {
邓实川 committed
305 306 307 308

		Integer nowEmpNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);

309 310 311 312 313 314 315 316 317 318
//		YgglMainEmp yg = YgglMainEmp.builder().build()
//				.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getOrgCode, orgCode)
//						.eq(YgglMainEmp::getEmpNum, nowEmpNum).select(YgglMainEmp::getIsManager));
//		if (yg != null && yg.getIsManager() != null && (yg.getIsManager() == 1 || yg.getIsManager() == 2)) {
		QyzxEmpEntAsso qyzxEmpEntAsso = QyzxEmpEntAsso.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxEmpEntAsso>().eq(QyzxEmpEntAsso::getOrgCode, orgCode)
						.eq(QyzxEmpEntAsso::getEmpNum, nowEmpNum));
		if (qyzxEmpEntAsso != null && qyzxEmpEntAsso.getUserType() != null
				&& (qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_ADMIN.getType()
						|| qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_C_ADMIN.getType())) {
邓实川 committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
			List<CrmBusinessGroup> list = CrmBusinessGroup.builder().build()
					.selectList(new LambdaQueryWrapper<CrmBusinessGroup>().eq(CrmBusinessGroup::getOrgCode, orgCode)
							.select(CrmBusinessGroup::getName, CrmBusinessGroup::getId));
			return ResultUtil.data(list, "获取成功");
		} else {
			List<CrmBusinessGroupMember> list = CrmBusinessGroupMember.builder().build()
					.selectList(new LambdaQueryWrapper<CrmBusinessGroupMember>()
							.eq(CrmBusinessGroupMember::getEmpNum, getEmpNum(userBean))
							.eq(CrmBusinessGroupMember::getOrgCode, getOrgCode(userBean))
							.select(CrmBusinessGroupMember::getGid));
			List<Integer> gids = list.stream().map(CrmBusinessGroupMember::getGid).collect(Collectors.toList()).stream()
					.distinct().collect(Collectors.toList());
			List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
			for (Integer gid : gids) {
				String name = CrmBusinessGroup.builder().build().selectOne(new LambdaQueryWrapper<CrmBusinessGroup>()
						.eq(CrmBusinessGroup::getId, gid).select(CrmBusinessGroup::getName)).getName();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", gid);
				map.put("name", name);
				result.add(map);
			}
			return ResultUtil.data(result, "获取成功");
邓实川 committed
341
		}
342 343
	}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
//	@GetMapping("getBusinessGroupMembers")
//	@ApiOperation(value = "获取业务组列表成员", httpMethod = "GET", notes = "接口发布说明")
//	public Result<Object> getBusinessGroupMembers(@CurrentUser UserBean userBean, @RequestParam Integer gid) {
//		List<CrmBusinessGroupMember> crmBusinessGroupMembers = CrmBusinessGroupMember.builder().build()
//				.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, gid)
//						.eq(CrmBusinessGroupMember::getType, 0));
//		List<YgglMainEmp> ygglMainEmps = new ArrayList<YgglMainEmp>();
//		for (CrmBusinessGroupMember crmBusinessGroupMember : crmBusinessGroupMembers) {
//			ygglMainEmps.add(getYg(getOrgCode(userBean), crmBusinessGroupMember.getEmpNum()));
//		}
//		List<CrmBusinessGroupMember> crmBusinessGroupManagers = CrmBusinessGroupMember.builder().build()
//				.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, gid)
//						.eq(CrmBusinessGroupMember::getType, 1));
//		List<YgglMainEmp> ygglMainEmps2 = new ArrayList<YgglMainEmp>();
//		for (CrmBusinessGroupMember crmBusinessGroupManager : crmBusinessGroupManagers) {
//			ygglMainEmps2.add(getYg(getOrgCode(userBean), crmBusinessGroupManager.getEmpNum()));
//		}
//		return ResultUtil.datas(ygglMainEmps2,ygglMainEmps, "获取成功");
//	}

邓实川 committed
364
//	@GetMapping("getBusinessGroupRules") 
365 366 367 368 369 370
//	@ApiOperation(value = "获取业务组公海规则", httpMethod = "GET", notes = "接口发布说明")
//	public Result<Object> getBusinessGroupRules(@RequestParam Integer gid) {
//		List<CrmSeaRule> crmSeaRules = CrmSeaRule.builder().build()
//				.selectList(new QueryWrapper<CrmSeaRule>().lambda().eq(CrmSeaRule::getGid, gid));
//		return ResultUtil.data(crmSeaRules, "获取成功");
//	}
371 372 373 374 375 376 377 378 379 380 381 382 383 384

	private SysRegion getSysRegion(Integer keyId) {
		return SysRegion.builder().build()
				.selectOne(new QueryWrapper<SysRegion>().lambda().eq(SysRegion::getKeyId, keyId).select(
						SysRegion::getRegionName, SysRegion::getKeyId, SysRegion::getZipCode,
						SysRegion::getRegionCode));
	}

	@PostMapping("addClient")
	@Transactional
	@ApiOperation(value = "新增客户", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> addClient(@CurrentUser UserBean userBean, @RequestBody CrmClientData crmClientData) {
		Integer empNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);
ilal committed
385
		crmClientData.setClientStatus(1);
386
		crmClientData.setCreateUser(empNum);
387
		SysRegion district = getSysRegion(crmClientData.getDistrictId());
388
//		crmClientData.setZipCode(district.getZipCode());
389 390 391 392 393 394 395 396 397
		if (district != null) {
			crmClientData.setDistrict(district.getRegionName());
			SysRegion city = getSysRegion(district.getRegionCode());
			crmClientData.setCity(city.getRegionName());
			crmClientData.setCityId(city.getKeyId());
			SysRegion province = getSysRegion(city.getRegionCode());
			crmClientData.setProvinceId(province.getKeyId());
			crmClientData.setProvince(province.getRegionName());
		}
398

399 400 401
		crmClientData.setBelongUserName(getEmpName(orgCode, crmClientData.getBelongUser()));
		crmClientData.setOrgCode(getOrgCode(userBean));
		Integer belongGroup = crmClientData.getBelongGroup();
402 403 404
		if (belongGroup == null)
			return ResultUtil.error("未选择业务组");

405 406
		String belongGroupName = CrmBusinessGroup.builder().id(belongGroup).build().selectById().getName();
		crmClientData.setBelongGroupName(belongGroupName);
407 408 409
		Integer clientSource = crmClientData.getClientSource();
		Integer clientType = crmClientData.getClientType();
		Integer industry = crmClientData.getIndustry();
ilal committed
410
		if (clientSource != null && clientSource > 0) {
411 412 413
			String clientSourceName = ClientSourceClass.builder().id(clientSource).build().selectById().getName(); // 客户来源
			crmClientData.setClientSourceName(clientSourceName);
		}
ilal committed
414
		if (clientType != null && clientType > 0) {
415 416 417
			String clientTypeName = ClientTypeClass.builder().id(clientType).build().selectById().getName(); // 客户类型
			crmClientData.setClientTypeName(clientTypeName);
		}
ilal committed
418
		if (industry != null && industry > 0) {
419 420 421
			String industryName = IndustryClass.builder().id(industry).build().selectById().getName();// 行业名
			crmClientData.setIndustryName(industryName);
		}
422 423 424 425
		if (!crmClientData.insert())
			return ResultUtil.error("新增失败");
		Integer crmDataId = crmClientData.getId();
		Integer[] ids = crmClientData.getIds();
ilal committed
426 427 428 429
		if(ids != null) {
			for (Integer id : ids) {
				CrmClientAssociate.builder().cid(crmDataId).associateId(id).orgCode(getOrgCode(userBean)).build().insert();
			}
430 431
		}
		List<CrmClientContacts> crmClientContacts = crmClientData.getCrmClientContacts();
lal committed
432
		int contactsnum = 0;
ilal committed
433 434 435 436 437 438 439
		if(crmClientContacts != null) {
			for (CrmClientContacts crmClientContact : crmClientContacts) {
				if(crmClientContact.getName() != null) {
					crmClientContact.setCid(crmDataId);
					crmClientContact.insert();
					contactsnum++;
				}
lal committed
440
			}
441
		}
lal committed
442
		crmClientData.setContactsNum(contactsnum);
443
		crmClientData.updateById();
444 445 446
		return ResultUtil.success("新增成功");
	}

447 448 449 450 451 452 453 454 455 456 457 458 459
	@PostMapping("editClient")
	@Transactional
	@ApiOperation(value = "编辑客户", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> editClient(@CurrentUser UserBean userBean, @RequestBody CrmClientData crmClientData) {
		Integer cid = crmClientData.getId();
		if (cid == null)
			return ResultUtil.error("请传入id再试");
		CrmClientData oldData = CrmClientData.builder().id(cid).build().selectById();
		if (oldData == null)
			return ResultUtil.error("请确认该客户数据是否存在");
		if (oldData.getClientStatus() == 5)
			return ResultUtil.error("请解封后继续编辑操作");
		List<CrmClientContacts> crmClientContacts = crmClientData.getCrmClientContacts();
邓实川 committed
460
		if (crmClientContacts != null) {
lal committed
461
			CrmClientContacts.builder().build().delete(new QueryWrapper<CrmClientContacts>().lambda().eq(CrmClientContacts::getCid, cid));
邓实川 committed
462
			for (CrmClientContacts crmClientContact : crmClientContacts) {
lal committed
463 464
//				crmClientContact.updateById();
				crmClientContact.insertOrUpdate();
邓实川 committed
465
			}
466
		}
lal committed
467 468 469
		if(crmClientContacts != null) {
			crmClientData.setContactsNum(crmClientContacts.size());//客户联系人数量
		}
lal committed
470
		
471 472 473 474 475 476 477 478 479 480 481 482 483
		List<CrmClientAssociate> oldAssociates = CrmClientAssociate.builder().build()
				.selectList(new QueryWrapper<CrmClientAssociate>().lambda().eq(CrmClientAssociate::getCid, cid));
		List<Integer> oldIdsList = oldAssociates.stream().map(CrmClientAssociate::getAssociateId)
				.collect(Collectors.toList()); // 旧协助人list
		Integer[] ids = crmClientData.getIds();
		List<Integer> newIdsList = CollUtil.newArrayList(ids); // 新协助人list
		List<Integer> delList = CheckUtil.reduce(oldIdsList, newIdsList); // 要删的协助人
		for (Integer id : delList) {
			CrmClientAssociate.builder().build().delete(new QueryWrapper<CrmClientAssociate>().lambda()
					.eq(CrmClientAssociate::getCid, cid).eq(CrmClientAssociate::getAssociateId, id));
		}
		List<Integer> addList = CheckUtil.reduce(newIdsList, oldIdsList); // 要加的协助人
		for (Integer id : addList) {
邓实川 committed
484
			CrmClientAssociate.builder().cid(cid).associateId(id).orgCode(getOrgCode(userBean)).build().insert();
485 486
		}
		crmClientData.setModifyUser(getEmpNum(userBean));
ilal committed
487
		crmClientData.setOrgCode(userBean.getOrgCode());
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
		if (crmClientData.updateById())
			return ResultUtil.success("编辑成功");
		return ResultUtil.error("编辑失败");
	}

	@GetMapping("getClientAssociate")
	@ApiOperation(value = "获取协作人列表", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<Map<String, Object>>> getClientAssociate(@CurrentUser UserBean userBean,
			@RequestParam Integer cid) {
		List<CrmClientAssociate> crmClientAssociates = CrmClientAssociate.builder().build()
				.selectList(new QueryWrapper<CrmClientAssociate>().lambda().eq(CrmClientAssociate::getCid, cid));
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (CrmClientAssociate crmClientAssociate : crmClientAssociates) {
			Integer orgCode = crmClientAssociate.getOrgCode();
			Integer empNum = crmClientAssociate.getAssociateId();
			YgglMainEmp ygglMainEmp = getYg(orgCode, empNum);
			String name = ygglMainEmp.getName();
			String headUrl = ygglMainEmp.getHeadUrl();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", name);
			map.put("empNum", empNum);
			map.put("headUrl", headUrl);
			list.add(map);
		}
		return ResultUtil.data(list, "获取成功");
	}

515
	// 归属我的客户
516
	private List<CrmClientData> getMyCilent(Integer empNum, Integer orgCode, Integer groupId, Integer status,
ilal committed
517
			String like,String startCreateTime,  String endCreateTime,  String startFollowTime, String endFollowTime,Integer typei) {
518 519 520
		return CrmClientData.builder().build()
				.selectList(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
						.eq(CrmClientData::getBelongUser, empNum)
ilal committed
521 522
						.eq(groupId != null && groupId > 0, CrmClientData::getBelongGroup, groupId)
						.eq(status != null && status > 0, CrmClientData::getClientStatus, status).eq(typei != null && typei > 0, CrmClientData::getClientType, typei)
ilal committed
523 524
						.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
						.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime)
ilal committed
525 526 527
						
						.ge(StringUtils.isNotBlank(startFollowTime), CrmClientData::getLastFollowTime, startFollowTime)
						.le(StringUtils.isNotBlank(endFollowTime), CrmClientData::getLastFollowTime, endFollowTime).like(!StrUtil.isBlank(like), CrmClientData::getBelongUserName, like).or()
528
						.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
ilal committed
529
						.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like).having("org_code = {0}", orgCode).having("belong_user = {0}", empNum)
530
						.orderByDesc(CrmClientData::getCreateTime));
531 532 533
	}

	// 我协作的客户
534
	private List<CrmClientData> getMyAssociateCilent(Integer empNum, Integer orgCode, Integer groupId, Integer status,
ilal committed
535
			String like,Integer typei,String startFollowTime, String endFollowTime,String startCreateTime,  String endCreateTime) {
536 537 538 539 540 541 542 543
		List<CrmClientAssociate> crmClientAssociates = CrmClientAssociate.builder().build().selectList(
				new QueryWrapper<CrmClientAssociate>().lambda().eq(CrmClientAssociate::getAssociateId, empNum)
						.eq(CrmClientAssociate::getOrgCode, orgCode).select(CrmClientAssociate::getCid));
		List<CrmClientData> myCrmClientAssociates = new ArrayList<CrmClientData>();
		for (CrmClientAssociate crmClientAssociate : crmClientAssociates) {
			Integer cid = crmClientAssociate.getCid();
			CrmClientData crmClientData = CrmClientData.builder().build()
					.selectOne(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getId, cid)
ilal committed
544
							.eq(groupId != null && groupId >0, CrmClientData::getBelongGroup, groupId)
ilal committed
545
							.eq(status != null && status >0, CrmClientData::getClientStatus, status).eq(typei != null && typei > 0, CrmClientData::getClientType, typei)
ilal committed
546 547
							.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
							.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime)
ilal committed
548 549 550
							
							.ge(StringUtils.isNotBlank(startFollowTime), CrmClientData::getLastFollowTime, startFollowTime)
							.le(StringUtils.isNotBlank(endFollowTime), CrmClientData::getLastFollowTime, endFollowTime)
551 552
							.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
							.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like));
邓实川 committed
553 554
			if (crmClientData != null)
				myCrmClientAssociates.add(crmClientData);
555 556 557 558 559
		}
		return myCrmClientAssociates;
	}

	// 我的全部客户
560
	private List<CrmClientData> getMyTotalCilent(Integer empNum, Integer orgCode, Integer groupId, Integer status,
ilal committed
561
			String like,Integer typei) {
ilal committed
562
		return CheckUtil.distinct(getMyCilent(empNum, orgCode, groupId, status, like,null,null,null,null,null),
ilal committed
563
				getMyAssociateCilent(empNum, orgCode, groupId, status, like,typei,null,null,null,null), true);
564 565 566
	}

	// 我关注的客户
ilal committed
567
	private List<CrmClientData> getMyStarCilent(Integer empNum, Integer orgCode, Integer groupId, Integer status,Integer typei) {
ilal committed
568
		return getMyTotalCilent(empNum, orgCode, groupId, status, null,null).stream()
569
				.filter(item -> item.getStar().equals(1)).collect(Collectors.toList());
570 571 572 573
	}

	// 七天未跟进的客户
	private List<CrmClientData> getCilentFollowOvertime(Integer empNum, Integer orgCode, Integer groupId,
ilal committed
574
			Integer status,Integer typei) {
ilal committed
575
		return getMyTotalCilent(empNum, orgCode, groupId, status, null,null).stream()
576 577 578
				.filter(item -> item.getLastFollowTime() != null
						&& item.getLastFollowTime().before(DateUtil.offsetDay(new Date(), -7)))
				.collect(Collectors.toList());
579 580
	}

581 582 583 584 585 586
	private <T> List<T> getPageList(List<T> list, int pageNumber, int pageSize, Comparator<T> comparator) {
		return list.stream().sorted(comparator).skip((pageNumber - 1) * pageSize).limit(pageSize)
				.collect(Collectors.toList());

	}

587 588 589
	@GetMapping("getMyCilentTotal")
	@ApiOperation(value = "获取我的客户列表(全)", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<CrmClientData>> getMyCilentTotal(@CurrentUser UserBean userBean,
ilal committed
590
			@RequestParam(required = false) Integer type,
591
			@RequestParam(required = false) Integer groupId, @RequestParam(required = false) Integer status,
592 593
			@RequestParam(required = false) String like,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
ilal committed
594 595 596 597
			@RequestParam(required = false, defaultValue = "10") int pageSize,
			@RequestParam(required = false) String startCreateTime,
			@RequestParam(required = false) String endCreateTime,
			@RequestParam(required = false) String startFollowTime,
ilal committed
598 599
			@RequestParam(required = false) String endFollowTime,
			@RequestParam(required = false) Integer typei) {
ilal committed
600
		
ilal committed
601
		if(endCreateTime != null && !("").equals(endCreateTime)) {
ilal committed
602 603
			endCreateTime = endCreateTime.substring(0, 10)+" 23:59:59";
		}
ilal committed
604
		if(endFollowTime != null && !("").equals(endFollowTime)) {
ilal committed
605 606 607
			endFollowTime = endFollowTime.substring(0, 10)+" 23:59:59";
		}
		
608 609 610
		Integer empNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);
		List<CrmClientData> crmClientDatas = null;
邓实川 committed
611
		if (type == null || type == 0) { // 全部客户(去重)
ilal committed
612
			List<CrmClientData> myClient = getMyCilent(empNum, orgCode, groupId, status, like,startCreateTime,endCreateTime,startFollowTime,endFollowTime,typei);
ilal committed
613
			List<CrmClientData> myAsso = getMyAssociateCilent(empNum, orgCode, groupId, status, like,typei,startFollowTime,endFollowTime,startCreateTime,endCreateTime);
邓实川 committed
614 615
			crmClientDatas = CheckUtil.distinct(myClient, myAsso, true).stream()
					.filter(CheckUtil.distinctByKey(CrmClientData::getId)).collect(Collectors.toList());
邓实川 committed
616
		} else if (type == 1) // 我负责的客户
ilal committed
617
			crmClientDatas = getMyCilent(empNum, orgCode, groupId, status, like,startCreateTime,endCreateTime,startFollowTime,endFollowTime,typei);
618
		else if (type == 2) // 我协作的客户
ilal committed
619
			crmClientDatas = getMyAssociateCilent(empNum, orgCode, groupId, status, like,typei,startFollowTime,endFollowTime,startCreateTime,endCreateTime);
620
		else if (type == 3) // 我关注的客户
ilal committed
621
			crmClientDatas = getMyStarCilent(empNum, orgCode, groupId, status,typei);
622
		else if (type == 4) // 七天未跟进的客户
ilal committed
623
			crmClientDatas = getCilentFollowOvertime(empNum, orgCode, groupId, status,typei);
624
		else
625
			return ResultUtil.error("查询失败");
626 627 628 629 630 631 632
//		Collections.sort(crmClientDatas, Comparator.comparing(CrmClientData::getCreateTime).reversed()); // 按时间降序排序

//		int pageNumber = 1;
//		int pageSize = 10;

		List<CrmClientData> result = getPageList(crmClientDatas, pageNumber, pageSize,
				Comparator.comparing(CrmClientData::getCreateTime).reversed());
ilal committed
633 634 635 636 637 638 639 640
		
		List<CrmClientData> resultlist = new ArrayList<CrmClientData>();
		
		for(CrmClientData crmd : result) {
			CrmClientData crmdts = CrmClientData.builder().build();
			BeanUtil.copyProperties(crmd, crmdts , "iscooperation");
			List<CrmClientAssociate> crmClientAssociates = CrmClientAssociate.builder().build()
					.selectList(new QueryWrapper<CrmClientAssociate>().lambda().eq(CrmClientAssociate::getCid, crmd.getId()));
ilal committed
641
			int u = 0;
ilal committed
642
			if(crmClientAssociates.size() > 0) {
ilal committed
643 644
				String[] collaborator = new String[crmClientAssociates.size()];
				
ilal committed
645 646
				for (CrmClientAssociate crmClientAssociate : crmClientAssociates) {
					Integer empNum1 = crmClientAssociate.getAssociateId();
ilal committed
647 648 649 650 651 652 653 654
					collaborator[u] = String.valueOf(empNum1);
					u++;
				}
				
				if(ClockInTool.contains(ClockInTool.deleteArrayNull(collaborator),String.valueOf(userBean.getEmpNum()))) {
					crmdts.setIscooperation(1);
				}else {
					crmdts.setIscooperation(0);
ilal committed
655
				}
ilal committed
656
				
ilal committed
657 658 659 660 661 662 663 664
			}else {
				crmdts.setIscooperation(0);
			}
			
			resultlist.add(crmdts);
		}
		
		return ResultUtil.pageData(resultlist, (long) crmClientDatas.size(), "查询成功");
665
//		return ResultUtil.data(crmClientDatas, "查询成功");
666
	}
邓实川 committed
667

邓实川 committed
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
	private List<Integer> MyJoinGroup(Integer empNum, Integer orgCode) {
		List<CrmBusinessGroupMember> members = CrmBusinessGroupMember.builder().build()
				.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda()
						.eq(CrmBusinessGroupMember::getEmpNum, empNum).eq(CrmBusinessGroupMember::getOrgCode, orgCode));
		return members.stream().map(CrmBusinessGroupMember::getGid).collect(Collectors.toList());
	}

	private List<Integer> MyManageGroup(Integer empNum, Integer orgCode) {
		List<CrmBusinessGroupMember> managers = CrmBusinessGroupMember.builder().build()
				.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda()
						.eq(CrmBusinessGroupMember::getEmpNum, empNum).eq(CrmBusinessGroupMember::getOrgCode, orgCode)
						.eq(CrmBusinessGroupMember::getType, 1));
		return managers.stream().map(CrmBusinessGroupMember::getGid).collect(Collectors.toList());
	}

683 684 685 686
	@GetMapping("getSeaCilentTotal")
	@ApiOperation(value = "获取公海客户列表(全)", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<CrmClientData>> getSeaCilentTotal(@CurrentUser UserBean userBean,
			@RequestParam(required = false) Integer type, @RequestParam(required = false) Integer groupId,
687 688 689
			@RequestParam(required = false) Integer status, @RequestParam(required = false) String like,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(required = false, defaultValue = "10") int pageSize) {
690 691
		Integer empNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);
邓实川 committed
692

693 694 695 696 697 698 699 700 701 702 703
//		YgglMainEmp yg = YgglMainEmp.builder().build()
//				.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getOrgCode, orgCode)
//						.eq(YgglMainEmp::getEmpNum, empNum).select(YgglMainEmp::getIsManager));
//		if (yg != null && yg.getIsManager() != null && (yg.getIsManager() == 1 || yg.getIsManager() == 2)) {

		QyzxEmpEntAsso qyzxEmpEntAsso = QyzxEmpEntAsso.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxEmpEntAsso>().eq(QyzxEmpEntAsso::getOrgCode, orgCode)
						.eq(QyzxEmpEntAsso::getEmpNum, empNum));
		if (qyzxEmpEntAsso != null && qyzxEmpEntAsso.getUserType() != null
				&& (qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_ADMIN.getType()
						|| qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_C_ADMIN.getType())) {
邓实川 committed
704 705
			List<CrmClientData> list = CrmClientData.builder().build()
					.selectList(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
706
							.eq(CrmClientData::getBelongUser, 0)
lal committed
707
							.eq(CrmClientData::getHidingcustomers, 1)
708 709 710 711
							.eq(type != null && type >= 0, CrmClientData::getClientType, type)
							.eq(groupId != null && groupId >= 0, CrmClientData::getBelongGroup, groupId)
							.eq(status != null && status >= 0, CrmClientData::getClientStatus, status)
							.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
邓实川 committed
712
							.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like));
邓实川 committed
713

714 715 716 717 718 719 720 721
//			int pageNumber = 1;
//			int pageSize = 5;
			List<CrmClientData> result = getPageList(list, pageNumber, pageSize,
					Comparator.comparing(CrmClientData::getCreateTime).reversed());

			return ResultUtil.pageData(result, (long) list.size(), "查询成功");

//			return ResultUtil.data(list, "查询成功");
邓实川 committed
722 723 724 725 726 727 728 729 730 731 732 733 734

		} else {

			// 当前用户加入的组
			List<Integer> gids1 = MyJoinGroup(empNum, orgCode);
			// 当前用户管理的组
			List<Integer> gids2 = MyManageGroup(empNum, orgCode);
			List<Integer> myGids = CheckUtil.distinct(gids1, gids2, true); // 当前用户所有业务组id
			List<CrmClientData> crmClientDatas = new ArrayList<CrmClientData>();
			for (Integer gid : myGids) {
				List<CrmClientData> datas = CrmClientData.builder().build()
						.selectList(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getBelongGroup, gid)
								.eq(CrmClientData::getBelongUser, 0)
lal committed
735
								.eq(CrmClientData::getHidingcustomers, 1)
邓实川 committed
736 737 738 739 740 741 742 743
								.eq(type != null && type >= 0, CrmClientData::getClientType, type)
								.eq(groupId != null && groupId >= 0, CrmClientData::getBelongGroup, groupId)
								.eq(status != null && status >= 0, CrmClientData::getClientStatus, status)
								.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
								.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like));
				for (CrmClientData data : datas)
					crmClientDatas.add(data);
			}
744 745 746 747 748
			List<CrmClientData> result = getPageList(crmClientDatas, pageNumber, pageSize,
					Comparator.comparing(CrmClientData::getCreateTime).reversed());

			return ResultUtil.pageData(result, (long) crmClientDatas.size(), "查询成功");
//			return ResultUtil.data(crmClientDatas, "查询成功");
邓实川 committed
749 750 751 752 753 754 755
		}
	}

	@GetMapping("getAllCilentTotal")
	@ApiOperation(value = "获取所有客户列表(全)", httpMethod = "GET", notes = "接口发布说明")
	public Result<Object> getAllCilentTotal(@CurrentUser UserBean userBean,
			@RequestParam(required = false) Integer type, @RequestParam(required = false) Integer groupId,
ilal committed
756
			@RequestParam(required = false) Integer status, @RequestParam(required = false) String like,@RequestParam(required = false) String customerowner,
邓实川 committed
757 758 759
			@RequestParam(required = false) String startCreateTime,
			@RequestParam(required = false) String endCreateTime,
			@RequestParam(required = false) String startFollowTime,
760 761 762
			@RequestParam(required = false) String endFollowTime,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(required = false, defaultValue = "10") int pageSize) {
邓实川 committed
763

邓实川 committed
764 765
		Integer empNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);
邓实川 committed
766

ilal committed
767 768 769 770 771 772 773
		if(endCreateTime != null && !("").equals(endCreateTime)) {
			endCreateTime = endCreateTime.substring(0, 10)+" 23:59:59";
		}
		if(endFollowTime != null && !("").equals(endFollowTime)) {
			endFollowTime = endFollowTime.substring(0, 10)+" 23:59:59";
		}
		
774 775 776 777 778 779 780 781 782 783 784
//		YgglMainEmp yg = YgglMainEmp.builder().build()
//				.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getOrgCode, orgCode)
//						.eq(YgglMainEmp::getEmpNum, empNum).select(YgglMainEmp::getIsManager));
//		if (yg != null && yg.getIsManager() != null && (yg.getIsManager() == 1 || yg.getIsManager() == 2)) {
		QyzxEmpEntAsso qyzxEmpEntAsso = QyzxEmpEntAsso.builder().build()
				.selectOne(new LambdaQueryWrapper<QyzxEmpEntAsso>().eq(QyzxEmpEntAsso::getOrgCode, orgCode)
						.eq(QyzxEmpEntAsso::getEmpNum, empNum));
		if (qyzxEmpEntAsso != null && qyzxEmpEntAsso.getUserType() != null
				&& (qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_ADMIN.getType()
						|| qyzxEmpEntAsso.getUserType() == SysRoleType.U_TYPE_C_ADMIN.getType())) {

785
			IPage<CrmClientData> page = new Page<CrmClientData>(pageNumber, pageSize);
lal committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
			
			
			List<CrmBusinessGroupMember> grouplis = CrmBusinessGroupMember.builder().build().selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getEmpNum, userBean.getEmpNum())
					.eq(CrmBusinessGroupMember::getOrgCode, userBean.getOrgCode()));
			
			String keywordStr = "";
			
			IPage<CrmClientData> datas;
			
			Integer count = new LambdaQueryChainWrapper<QyzxEmpEntAsso>(qyzxEmpEntAssoMapper)
					.eq(QyzxEmpEntAsso::getEmpNum, userBean.getEmpNum())
					.eq(QyzxEmpEntAsso::getOrgCode, userBean.getOrgCode())
					.in(QyzxEmpEntAsso::getUserType, "0,1")
					.count();
			if(count > 0) {
				
				datas = CrmClientData.builder().build().selectPage(page,
						new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
								.eq(groupId != null && groupId >= 0, CrmClientData::getBelongGroup, groupId)
								.eq(type != null && type >= 0, CrmClientData::getClientType, type)
ilal committed
806
								.eq(status != null && status >= 0, CrmClientData::getClientStatus, status).like(!StrUtil.isBlank(customerowner), CrmClientData::getBelongUserName, customerowner)
lal committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
								.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
								.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime)
								.ge(StringUtils.isNotBlank(startFollowTime), CrmClientData::getCreateTime, startFollowTime)
								.le(StringUtils.isNotBlank(endFollowTime), CrmClientData::getCreateTime, endFollowTime)
								.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
								.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like)
								.orderByDesc(CrmClientData::getCreateTime));
			}else {
				if(grouplis.size()>0 && grouplis != null) {
					StringBuffer sb = new StringBuffer();
					for(CrmBusinessGroupMember grp : grouplis) {
						sb.append(grp.getGid()).append(",");
					}
					keywordStr = sb.deleteCharAt(sb.length() - 1).toString();
				}
				
				datas = CrmClientData.builder().build().selectPage(page,
						new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
								.eq(groupId != null && groupId >= 0, CrmClientData::getBelongGroup, groupId)
ilal committed
826
								.eq(type != null && type >= 0, CrmClientData::getClientType, type).like(!StrUtil.isBlank(customerowner), CrmClientData::getBelongUserName, customerowner)
lal committed
827 828 829 830 831 832 833 834 835 836
								.eq(status != null && status >= 0, CrmClientData::getClientStatus, status).in(CrmClientData::getBelongGroup, keywordStr)
								.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
								.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime)
								.ge(StringUtils.isNotBlank(startFollowTime), CrmClientData::getCreateTime, startFollowTime)
								.le(StringUtils.isNotBlank(endFollowTime), CrmClientData::getCreateTime, endFollowTime)
								.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
								.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like)
								.orderByDesc(CrmClientData::getCreateTime));
			}
			
837 838

			return ResultUtil.pageData(datas.getRecords(), datas.getTotal(), "查询成功");
邓实川 committed
839 840 841 842
		} else {

			// 当前用户管理的组
			List<Integer> gids = MyManageGroup(empNum, orgCode);
843
			List<CrmClientData> crmClientDatas = new ArrayList<CrmClientData>();
邓实川 committed
844 845 846 847
			for (Integer gid : gids) {
				List<CrmClientData> datas = CrmClientData.builder().build().selectList(new QueryWrapper<CrmClientData>()
						.lambda().eq(CrmClientData::getBelongGroup, gid)
						.eq(groupId != null && groupId >= 0, CrmClientData::getBelongGroup, groupId)
ilal committed
848
						.eq(type != null && type >= 0, CrmClientData::getClientType, type).like(!StrUtil.isBlank(customerowner), CrmClientData::getBelongUserName, customerowner)
邓实川 committed
849 850 851 852 853 854 855 856 857 858
						.eq(status != null && status >= 0, CrmClientData::getClientStatus, status)
						.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
						.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime)
						.ge(StringUtils.isNotBlank(startFollowTime), CrmClientData::getCreateTime, startFollowTime)
						.le(StringUtils.isNotBlank(endFollowTime), CrmClientData::getCreateTime, endFollowTime)
						.like(!StrUtil.isBlank(like), CrmClientData::getClientName, like).or()
						.like(!StrUtil.isBlank(like), CrmClientData::getClientCellphone, like));
				for (CrmClientData data : datas)
					crmClientDatas.add(data);
			}
859 860 861 862 863 864

			List<CrmClientData> result = getPageList(crmClientDatas, pageNumber, pageSize,
					Comparator.comparing(CrmClientData::getCreateTime).reversed());

			return ResultUtil.pageData(result, (long) crmClientDatas.size(), "查询成功");
//			return ResultUtil.data(crmClientDatas, "查询成功");
邓实川 committed
865
		}
866 867
	}

868 869 870
	@PostMapping("addCilentLinkMan")
	@Transactional
	@ApiOperation(value = "新增/编辑客户联系人", httpMethod = "POST", notes = "接口发布说明")
邓实川 committed
871 872
	public Result<Void> addCilentLinkMan(@CurrentUser UserBean userBean,
			@RequestBody CrmClientContacts crmClientContacts) {
873 874
		Integer cid = crmClientContacts.getCid();

ilal committed
875
		if (crmClientContacts.getId() == null || crmClientContacts.getId().equals(0)) {
876 877 878
			if (cid == null)
				return ResultUtil.error("编辑失败,请传入客户id");

邓实川 committed
879
			Integer num = CrmClientData.builder().id(crmClientContacts.getCid()).build().selectById().getContactsNum();
880
			CrmClientData.builder().id(cid).contactsNum(++num).build().updateById();
881 882 883
		}
		if (crmClientContacts.insertOrUpdate())
			return ResultUtil.success("新增/编辑成功");
884
		return ResultUtil.error("新增/编辑失败");
885 886 887 888 889 890

	}

	@DeleteMapping("delCilentLinkMan")
	@Transactional
	@ApiOperation(value = "删除客户联系人", httpMethod = "DELETE", notes = "接口发布说明")
邓实川 committed
891 892
	public Result<Void> delCilentLinkMan(@CurrentUser UserBean userBean, @RequestParam Integer id,
			@RequestParam Integer cid) {
893
		if (cid == null)
邓实川 committed
894
			return ResultUtil.error("删除失败,请传入客户id");
895
		Integer num = CrmClientData.builder().id(cid).build().selectById().getContactsNum();
896 897 898 899
		if (--num < 0) {
			return ResultUtil.error("删除失败,已经没有可以删的联系人了");
		}
		CrmClientData.builder().id(cid).contactsNum(num).build().updateById();
邓实川 committed
900
		if (crmClientContactsMapper.deleteById(id) > 0)
901
			return ResultUtil.success("删除成功");
邓实川 committed
902
		return ResultUtil.error("删除失败");
903 904 905 906 907 908 909 910 911 912 913

	}

	@GetMapping("getCilentLinkMan")
	@ApiOperation(value = "获取客户联系人列表", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<CrmClientContacts>> getCilentLinkMan(@CurrentUser UserBean userBean, Integer cid) {
		return ResultUtil.data(CrmClientContacts.builder().build()
				.selectList(new QueryWrapper<CrmClientContacts>().lambda().eq(CrmClientContacts::getCid, cid)), "获取成功");

	}

914 915 916
	@PostMapping("addOrCancelClientStar")
	@ApiOperation(value = "新增客户收藏/取消收藏(仅归属人)", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> addOrCancelClientStar(@CurrentUser UserBean userBean, @RequestParam Integer cid) {
917 918 919 920 921 922
		Integer empNum = getEmpNum(userBean);
		CrmClientData crmClientData = CrmClientData.builder().id(cid).build().selectById();
		if (crmClientData == null)
			return ResultUtil.error("请确认该客户是否存在");
		if (!crmClientData.getBelongUser().equals(empNum))
			return ResultUtil.error("不是当前客户归属人");
923 924 925 926
		Integer star = crmClientData.getStar();
		if (star == null || star == 0) {
			crmClientData.setStar(1);
			crmClientData.updateById();
927
			return ResultUtil.success("收藏成功");
928 929 930 931 932 933
		} else if (star == 1) {
			crmClientData.setStar(0);
			crmClientData.updateById();
			return ResultUtil.success("取消收藏成功");
		} else
			return ResultUtil.error("失败");
934 935 936 937 938 939
	}

	@PostMapping("addClientFollow")
	@Transactional
	@ApiOperation(value = "新增跟进记录", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> addClientFollow(@CurrentUser UserBean userBean, @RequestBody CrmClientFollow crmClientFollow) {
940 941 942 943 944 945 946 947 948 949

		Integer cid = crmClientFollow.getCid();
		if (cid == null)
			return ResultUtil.error("请传入id再试");
		CrmClientData oldData = CrmClientData.builder().id(cid).build().selectById();
		if (oldData == null)
			return ResultUtil.error("请确认该客户数据是否存在");
		if (oldData.getClientStatus() == 5)
			return ResultUtil.error("请解封后继续编辑操作");

950 951 952
		Integer orgCode = getOrgCode(userBean);
		Integer empNum = getEmpNum(userBean);

953 954
		CrmClientData.builder().id(crmClientFollow.getCid()).clientStatus(crmClientFollow.getClientStatus())
				.lastFollowTime(new Date()).build().updateById(); // 更新客户最近跟进时间
955 956 957 958 959 960 961 962 963 964 965 966 967 968

		crmClientFollow.setOrgCode(orgCode);
		crmClientFollow.setCreateUserName(getEmpName(orgCode, empNum));

		if (crmClientFollow.insert())
			return ResultUtil.success("新增成功");
		return ResultUtil.error("新增失败");
	}

	@GetMapping("getCilentFollow")
	@ApiOperation(value = "获取跟进记录", httpMethod = "GET", notes = "接口发布说明")
	public Result<List<CrmClientFollow>> getCilentFollow(@CurrentUser UserBean userBean, @RequestParam Integer cid) {
		Integer orgCode = getOrgCode(userBean);
		return ResultUtil.data(CrmClientFollow.builder().build().selectList(new QueryWrapper<CrmClientFollow>().lambda()
ilal committed
969
				.eq(CrmClientFollow::getOrgCode, orgCode).eq(CrmClientFollow::getCid, cid).orderByDesc(CrmClientFollow::getId)), "获取成功");
970
	}
971

邓实川 committed
972 973 974 975 976 977 978
	// 根据组id和员工号查询组管理员
	private CrmBusinessGroupMember getGroupManager(Integer groupId, Integer empNum) {
		return CrmBusinessGroupMember.builder().build().selectOne(
				new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, groupId)
						.eq(CrmBusinessGroupMember::getEmpNum, empNum).eq(CrmBusinessGroupMember::getType, 1));
	}

979 980 981 982 983 984 985
	@PostMapping("addClientArchive")
	@ApiOperation(value = "客户封档/解封", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> addClientArchive(@CurrentUser UserBean userBean, @RequestParam Integer cid) {
		Integer empNum = getEmpNum(userBean);
		CrmClientData crmClientData = CrmClientData.builder().id(cid).build().selectById();
		if (crmClientData == null)
			return ResultUtil.error("请确认该客户是否存在");
ilal committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
		
		
		QyzxEmpEntAsso ent = QyzxEmpEntAsso.builder().build().selectOne(new QueryWrapper<QyzxEmpEntAsso>().lambda().eq(QyzxEmpEntAsso::getOrgCode, userBean.getOrgCode()).eq(QyzxEmpEntAsso::getEmpNum, userBean.getEmpNum()));
		if(ent != null) {
			if(ent.getUserType() == 2) {
				if (!crmClientData.getBelongUser().equals(empNum)
						&& getGroupManager(crmClientData.getBelongGroup(), empNum) == null)
					return ResultUtil.error("不是当前客户归属人或组管理员");
			}
		}
		
//		if (!crmClientData.getBelongUser().equals(empNum)
//				&& getGroupManager(crmClientData.getBelongGroup(), empNum) == null)
//			return ResultUtil.error("不是当前客户归属人或组管理员");
		
		
1002 1003 1004 1005 1006 1007
		Integer status = crmClientData.getClientStatus();
		if (status != null && status != 5) {
			crmClientData.setClientStatus(5);
			crmClientData.updateById();
			return ResultUtil.success("封档成功");
		} else if (status == 5) {
1008
			crmClientData.setClientStatus(1); // 解封后默认潜在状态
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
			crmClientData.updateById();
			return ResultUtil.success("解封成功");
		} else
			return ResultUtil.error("失败");
	}

	@PostMapping("designateClientBelonger")
	@ApiOperation(value = "指派客户归属人", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> designateClientBelonger(@CurrentUser UserBean userBean, @RequestParam Integer cid,
			@RequestParam Integer belongUser) {
邓实川 committed
1019
		Integer empNum = getEmpNum(userBean);
1020 1021 1022 1023 1024
		if (cid == null)
			return ResultUtil.error("请传入id再试");
		CrmClientData oldData = CrmClientData.builder().id(cid).build().selectById();
		if (oldData == null)
			return ResultUtil.error("请确认该客户数据是否存在");
邓实川 committed
1025 1026
		if (!oldData.getBelongUser().equals(empNum) && getGroupManager(oldData.getBelongGroup(), empNum) == null)
			return ResultUtil.error("不是当前客户归属人或组管理员");
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
		if (oldData.getClientStatus() == 5)
			return ResultUtil.error("请解封后继续编辑操作");

		Integer belongGroup = oldData.getBelongGroup();
		CrmBusinessGroupMember crmBusinessGroupMember = CrmBusinessGroupMember.builder().build()
				.selectOne(new QueryWrapper<CrmBusinessGroupMember>().lambda()
						.eq(CrmBusinessGroupMember::getGid, belongGroup)
						.eq(CrmBusinessGroupMember::getEmpNum, belongUser));
		if (crmBusinessGroupMember == null)
			return ResultUtil.error("指派失败,该用户不是本组成员");

		if (CrmClientData.builder().id(cid).belongUser(belongUser)
				.belongUserName(getEmpName(getOrgCode(userBean), belongUser)).build().updateById())
			return ResultUtil.success("指派成功");
		return ResultUtil.error("指派失败");

	}

	@PostMapping("freeClientToSea")
	@Transactional
	@ApiOperation(value = "释放客户到公海", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> freeClientToSea(@CurrentUser UserBean userBean, @RequestParam Integer cid) {
邓实川 committed
1049
		Integer empNum = getEmpNum(userBean);
1050 1051 1052 1053 1054
		if (cid == null)
			return ResultUtil.error("请传入id再试");
		CrmClientData oldData = CrmClientData.builder().id(cid).build().selectById();
		if (oldData == null)
			return ResultUtil.error("请确认该客户数据是否存在");
邓实川 committed
1055 1056 1057 1058 1059
		if (oldData.getBelongUser() == 0)
			return ResultUtil.error("该客户已在公海列表");
		if ((!oldData.getOrgCode().equals(getOrgCode(userBean)) || !oldData.getBelongUser().equals(empNum))
				&& getGroupManager(oldData.getBelongGroup(), empNum) == null)
			return ResultUtil.error("不是当前客户归属人或组管理员");
1060 1061 1062 1063
		if (oldData.getClientStatus() == 5)
			return ResultUtil.error("请解封后继续编辑操作");
		oldData.setBelongUser(0); // 0-进入客户公海
		oldData.setBelongUserName(null);
邓实川 committed
1064 1065 1066 1067
		// 删除协作人列表
		CrmClientAssociate.builder().build()
				.delete(new QueryWrapper<CrmClientAssociate>().lambda().eq(CrmClientAssociate::getCid, cid));
		if (oldData.updateById())
1068 1069 1070 1071
			return ResultUtil.success("释放成功");
		return ResultUtil.error("释放失败");
	}

邓实川 committed
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
	@PostMapping("receiveSeaClient")
	@ApiOperation(value = "接收公海客户", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> receiveSeaClient(@CurrentUser UserBean userBean, @RequestParam Integer cid) {
		Integer empNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);
		if (cid == null)
			return ResultUtil.error("请传入id再试");
		CrmClientData oldData = CrmClientData.builder().id(cid).build().selectById();
		if (oldData == null)
			return ResultUtil.error("请确认该客户数据是否存在");
		// 当前用户所有组id
		List<Integer> myGids = CheckUtil.distinct(MyJoinGroup(empNum, orgCode), MyManageGroup(empNum, orgCode), true);
		if (!myGids.contains(oldData.getBelongGroup()))
			return ResultUtil.error("不是当前业务组成员");
		oldData.setBelongUser(empNum);
		oldData.setBelongUserName(getEmpName(orgCode, empNum));
		if (oldData.updateById())
			return ResultUtil.success("接收成功");
		return ResultUtil.error("接收失败");
	}

lal committed
1093 1094
	@Autowired
	private QyzxEmpEntAssoMapper qyzxEmpEntAssoMapper;
邓实川 committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	@Transactional
	@PostMapping("designateClientBelongers")
	@ApiOperation(value = "转移客户归属人(批量客户)", httpMethod = "POST", notes = "接口发布说明")
	public Result<Void> designateClientBelongers(@CurrentUser UserBean userBean, @RequestParam List<Integer> cids,
			@RequestParam Integer belonger) {
		// 所选的客户列表
		List<CrmClientData> crmClientDatas = crmClientDataMapper.selectBatchIds(cids);
		// 业务组id列表
		List<Integer> gids = crmClientDatas.stream().map(CrmClientData::getBelongGroup).collect(Collectors.toList());
		Integer gid = gids.get(0);
lal committed
1105 1106 1107 1108 1109 1110 1111
		
		
		Integer count = new LambdaQueryChainWrapper<QyzxEmpEntAsso>(qyzxEmpEntAssoMapper)
				.eq(QyzxEmpEntAsso::getEmpNum, userBean.getEmpNum())
				.eq(QyzxEmpEntAsso::getOrgCode, userBean.getOrgCode())
				.in(QyzxEmpEntAsso::getUserType, "0,1")
				.count();
lal committed
1112 1113 1114 1115 1116 1117 1118 1119
		
		// 当前操作人是否管理员
		CrmBusinessGroupMember nowManager = CrmBusinessGroupMember.builder().build()
				.selectOne(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, gid)
						.eq(CrmBusinessGroupMember::getEmpNum, getEmpNum(userBean))
						.eq(CrmBusinessGroupMember::getType, 1));
		//系统主管理员 或者 组管理员
		if(count > 0 || nowManager != null) {
lal committed
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
			
			// 是否有不同组的客户
			if (gids.stream().distinct().count() > 1)
				return ResultUtil.error("请确认是否同一业务组客户");
			// 转移人业务组信息
			CrmBusinessGroupMember crmBusinessGroupMember = CrmBusinessGroupMember.builder().build()
					.selectOne(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, gid)
							.eq(CrmBusinessGroupMember::getEmpNum, belonger));
			if (crmBusinessGroupMember == null)
				return ResultUtil.error("请确认转移目标是当前业务组成员");
			for (CrmClientData crmClientData : crmClientDatas) {
				crmClientData.setBelongUser(belonger);
				crmClientData.setBelongUserName(getEmpName(getOrgCode(userBean), belonger));
				crmClientData.updateById();
			}
			return ResultUtil.success("转移成功");
			
		}
		
邓实川 committed
1139
		// 当前操作人是否管理员
lal committed
1140 1141 1142 1143 1144
//		CrmBusinessGroupMember nowManager = CrmBusinessGroupMember.builder().build()
//				.selectOne(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, gid)
//						.eq(CrmBusinessGroupMember::getEmpNum, getEmpNum(userBean))
//						.eq(CrmBusinessGroupMember::getType, 1));
		
邓实川 committed
1145 1146
		if (nowManager == null)
			return ResultUtil.error("没有操作权限,请联系组管理员进行操作");
lal committed
1147 1148
		
		
lal committed
1149
		return ResultUtil.success("转移失败");
邓实川 committed
1150
	}
lal committed
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	
	@GetMapping("hidingcustomers")
	@ApiOperation(value = "隐藏客户", httpMethod = "GET", notes = "接口发布说明")
	public Result<Void> HidingCustomers(@CurrentUser UserBean userBean,@RequestParam Integer yid) {
		
		CrmClientData crda = CrmClientData.builder().id(yid).build().selectById();
		
		if(crda != null) {
			// 当前操作人是否管理员
			CrmBusinessGroupMember nowManager = CrmBusinessGroupMember.builder().build()
					.selectOne(new QueryWrapper<CrmBusinessGroupMember>().lambda().eq(CrmBusinessGroupMember::getGid, crda.getBelongGroup())
							.eq(CrmBusinessGroupMember::getEmpNum, getEmpNum(userBean))
							.eq(CrmBusinessGroupMember::getType, 1));
			
			if (nowManager == null)
				return ResultUtil.error("没有操作权限,请联系组管理员进行操作");
			
			
		  CrmClientData.builder().id(yid).hidingcustomers(0).build().updateById();
		}
		
		
		return ResultUtil.success("成功");
	}
邓实川 committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

	/**
	 * 
	 * @param userBean
	 * @param type            统计图类型(暂时不要)
	 * @param groupId         业务组id
	 * @param memberId        业务员id
	 * @param startCreateTime 创建时间-开始时间
	 * @param endCreateTime   创建时间-结束时间
	 * @return 对应客户数据count
	 */
	@GetMapping("getAllCartogram")
	@ApiOperation(value = "获取统计图(全)", httpMethod = "GET", notes = "接口发布说明")
	public Result<Object> getAllCartogram(@CurrentUser UserBean userBean,
			@RequestParam(required = false) Integer groupId, @RequestParam(required = false) Integer belonger,
			@RequestParam(required = false) String startCreateTime,
			@RequestParam(required = false) String endCreateTime) {
ilal committed
1192 1193 1194 1195 1196
		
		if(endCreateTime != null && !("").equals(endCreateTime)) {
			endCreateTime = endCreateTime.substring(0, 10)+" 23:59:59";
		}
		
邓实川 committed
1197 1198 1199 1200 1201 1202 1203
		Integer orgCode = getOrgCode(userBean);
		List<Object> listAll = new ArrayList<Object>();
		listAll.add(cartogram(orgCode, groupId, 1, belonger, startCreateTime, endCreateTime));// 图1
		listAll.add(cartogram(orgCode, groupId, 2, belonger, startCreateTime, endCreateTime));// 图2
		listAll.add(cartogram(orgCode, groupId, 3, belonger, startCreateTime, endCreateTime));// 图3
		listAll.add(cartogram(orgCode, groupId, 4, belonger, startCreateTime, endCreateTime));// 图4

1204 1205 1206 1207 1208 1209 1210 1211 1212
		List<Map<String, Object>> cartogram5 = new ArrayList<Map<String, Object>>();// 图5
		List<ClientTypeClass> typeClasses = ClientTypeClass.builder().build().selectAll();
		for (ClientTypeClass clientTypeClass : typeClasses) {
			Integer type = clientTypeClass.getId();
			Map<String, Object> map = cartogram2(orgCode, groupId, type, belonger, startCreateTime, endCreateTime);
			if ((Integer) map.get("num") > 0) {
				cartogram5.add(map);
			}
		}
邓实川 committed
1213 1214
		listAll.add(cartogram5);

1215 1216 1217 1218 1219 1220 1221 1222 1223
//		List<Map<String, Object>> cartogram6 = new ArrayList<Map<String, Object>>(); // 图6
//		for (Integer keyId : getKeyIdList()) {
//			Map<String, Object> map = cartogram3(orgCode, groupId, keyId, belonger, startCreateTime, endCreateTime);
//			if ((Integer) map.get("num") > 0) {
//				cartogram6.add(map);
//			}
//		}

		List<CrmCartogramDto> cartogram6 = getCartogram6(orgCode, belonger, groupId, startCreateTime, endCreateTime);
邓实川 committed
1224 1225 1226 1227
		listAll.add(cartogram6);
		return ResultUtil.data(listAll, "获取成功");
	}

1228
	// 获取省份id
1229 1230 1231 1232 1233
//	private List<Integer> getKeyIdList() {
//		List<SysRegion> list = SysRegion.builder().build()
//				.selectList(new LambdaQueryWrapper<SysRegion>().eq(SysRegion::getLevel, 1).select(SysRegion::getKeyId));
//		return list.stream().map(SysRegion::getKeyId).collect(Collectors.toList());
//	}
邓实川 committed
1234

邓实川 committed
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	@GetMapping("getAllBusinessMan")
	@ApiOperation(value = "获取全部业务员", httpMethod = "GET", notes = "接口发布说明")
	public Result<Object> getAllBusinessMan(@CurrentUser UserBean userBean) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Integer> empNums = getAllBusinessGroupMember(getOrgCode(userBean), null);
		for (Integer integer : empNums) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("empNum", integer);
			map.put("name", getEmpName(getOrgCode(userBean), integer));
			list.add(map);
		}
		return ResultUtil.data(list, "获取成功");
	}

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
	@GetMapping("getBusinessManByGroupId")
	@ApiOperation(value = "获取业务员ByGroupId", httpMethod = "GET", notes = "接口发布说明")
	public Result<Object> getBusinessManByGroupId(@CurrentUser UserBean userBean, Integer gid) {
		List<CrmBusinessGroupMember> list = CrmBusinessGroupMember.builder().build()
				.selectList(new LambdaQueryWrapper<CrmBusinessGroupMember>().eq(CrmBusinessGroupMember::getGid, gid)
						.select(CrmBusinessGroupMember::getEmpNum));
		List<Integer> empNums = list.stream().map(CrmBusinessGroupMember::getEmpNum).collect(Collectors.toList());
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (Integer empNum : empNums) {
			String name = getEmpName(getOrgCode(userBean), empNum);
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", name);
			map.put("empNum", empNum);
			result.add(map);
		}
		return ResultUtil.data(result, "获取成功");
	}

邓实川 committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
	// 获取公司全部业务员
	private List<Integer> getAllBusinessGroupMember(Integer orgCode, Integer groupId) {
		List<CrmBusinessGroupMember> list = CrmBusinessGroupMember.builder().build()
				.selectList(new QueryWrapper<CrmBusinessGroupMember>().lambda()
						.eq(CrmBusinessGroupMember::getOrgCode, orgCode)
						.eq(groupId != null && groupId >= 0, CrmBusinessGroupMember::getGid, groupId));
		return list.stream().map(CrmBusinessGroupMember::getEmpNum).collect(Collectors.toList()).stream().distinct()
				.collect(Collectors.toList());
//		list.stream().filter(CheckUtil.distinctByKey(CrmBusinessGroupMember::getEmpNum)).collect(Collectors.toList());
	}

	// 封装条形图数据Map
1279 1280
	private Map<String, Object> selectMap(Integer orgCode, Integer groupId, Integer belonger, Integer status,
			String startCreateTime, String endCreateTime) {
邓实川 committed
1281 1282 1283
		Map<String, Object> map = new HashMap<String, Object>();
		Integer num = CrmClientData.builder().build()
				.selectCount(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
邓实川 committed
1284 1285
						.eq(CrmClientData::getClientStatus, status)
						.eq(belonger != null, CrmClientData::getBelongUser, belonger)
1286
						.eq(groupId != null, CrmClientData::getBelongGroup, groupId)
邓实川 committed
1287 1288
						.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
						.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime));
1289
		String name = "未知";
邓实川 committed
1290
		if (belonger == 0)
1291
			name = "公海客户";
ilal committed
1292 1293 1294 1295 1296 1297 1298 1299 1300
		else if (belonger > 0) {
			YgglMainEmp emps = YgglMainEmp.builder().build()
					.selectOne(new QueryWrapper<YgglMainEmp>().lambda().eq(YgglMainEmp::getEmpNum, belonger)
							.eq(YgglMainEmp::getOrgCode, orgCode).select(YgglMainEmp::getName, YgglMainEmp::getHeadUrl));
			if(emps != null) {
				name = getEmpName(orgCode, belonger);
			}
			
		}	
邓实川 committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
		map.put("name", name);
		map.put("num", num);
		return map;
	}

	/**
	 * 统计图1,2,3,4
	 * 
	 * @param orgCode  企业id(必填)
	 * @param groupId  组id (可不填)
	 * @param status   分类 1 2 3 4 (必填)
	 * @param belonger 业务员id (可不填)
	 * @return
	 */
	private List<Map<String, Object>> cartogram(Integer orgCode, Integer groupId, Integer status, Integer belonger,
			String startCreateTime, String endCreateTime) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (belonger == null) {
			List<Integer> members = getAllBusinessGroupMember(orgCode, groupId);
			for (Integer memberId : members) {
1321
				Map<String, Object> map = selectMap(orgCode, groupId, memberId, status, startCreateTime, endCreateTime);
1322 1323 1324
				if ((Integer) map.get("num") > 0) {
					list.add(map);
				}
邓实川 committed
1325
			}
1326
			Map<String, Object> map = selectMap(orgCode, groupId, 0, status, startCreateTime, endCreateTime);
邓实川 committed
1327
			if ((Integer) map.get("num") > 0)
1328 1329
				list.add(map);

邓实川 committed
1330
		} else {
1331
			Map<String, Object> map = selectMap(orgCode, groupId, belonger, status, startCreateTime, endCreateTime);
1332 1333 1334
			if ((Integer) map.get("num") > 0) {
				list.add(map);
			}
邓实川 committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
		}
		return list;
	}

	/**
	 * 统计图5
	 * 
	 * @param orgCode  企业id(必填)
	 * @param groupId  组id (可不填)
	 * @param level    客户等级分类 (必填)
	 * @param belonger 业务员id (可不填)
	 * @return
	 */
1348
	private Map<String, Object> cartogram2(Integer orgCode, Integer groupId, Integer level, Integer belonger,
邓实川 committed
1349
			String startCreateTime, String endCreateTime) {
1350 1351
		Map<String, Object> map = new HashMap<String, Object>();
		Integer num = 0;
邓实川 committed
1352 1353 1354
		if (belonger == null) {
			List<Integer> members = getAllBusinessGroupMember(orgCode, groupId);
			for (Integer memberId : members) {
1355 1356 1357
				num += CrmClientData.builder().build()
						.selectCount(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
								.eq(CrmClientData::getClientType, level).eq(CrmClientData::getBelongUser, memberId)
1358
								.eq(groupId != null, CrmClientData::getBelongGroup, groupId)
1359 1360 1361 1362
								.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime,
										startCreateTime)
								.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime,
										endCreateTime));
1363

邓实川 committed
1364
			}
1365 1366 1367 1368 1369 1370 1371 1372

			num += CrmClientData.builder().build()
					.selectCount(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
							.eq(CrmClientData::getClientType, level).eq(CrmClientData::getBelongUser, 0)
							.eq(groupId != null, CrmClientData::getBelongGroup, groupId)
							.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
							.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime));

1373 1374 1375 1376 1377
			map.put("level", level);
			String name = ClientTypeClass.builder().id(level).build().selectById().getName();
			map.put("name", name);
			map.put("num", num);

邓实川 committed
1378
		} else {
1379 1380 1381 1382 1383 1384 1385 1386 1387
			num = CrmClientData.builder().build()
					.selectCount(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
							.eq(CrmClientData::getClientType, level).eq(CrmClientData::getBelongUser, belonger)
							.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
							.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime));
			map.put("level", level);
			String name = ClientTypeClass.builder().id(level).build().selectById().getName();
			map.put("name", name);
			map.put("num", num);
邓实川 committed
1388
		}
1389
		return map;
邓实川 committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
	}

	/**
	 * 统计图6
	 * 
	 * @param orgCode  企业id(必填)
	 * @param groupId  组id (可不填)
	 * @param level    客户等级分类 (必填)
	 * @param belonger 业务员id (可不填)
	 * @return
	 */
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
//	private Map<String, Object> cartogram3(Integer orgCode, Integer groupId, Integer provinceId, Integer belonger,
//			String startCreateTime, String endCreateTime) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		Integer num = 0;
//		if (belonger == null) {
//
//			List<Integer> members = getAllBusinessGroupMember(orgCode, groupId);
//			for (Integer memberId : members) {
//				num += CrmClientData.builder().build()
//						.selectCount(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
//								.eq(CrmClientData::getProvinceId, provinceId).eq(CrmClientData::getBelongUser, memberId)
//								.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime,
//										startCreateTime)
//								.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime,
//										endCreateTime));
//			}
//			map.put("provinceId", provinceId);
//			String name = SysRegion.builder().build().selectOne(new QueryWrapper<SysRegion>().lambda()
//					.eq(SysRegion::getLevel, 1).eq(SysRegion::getKeyId, provinceId).select(SysRegion::getRegionName))
//					.getRegionName();
//			map.put("provinceName", name);
//			map.put("num", num);
//
//		} else {
//			num = CrmClientData.builder().build()
//					.selectCount(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getOrgCode, orgCode)
//							.eq(CrmClientData::getProvinceId, provinceId).eq(CrmClientData::getBelongUser, belonger)
//							.ge(StringUtils.isNotBlank(startCreateTime), CrmClientData::getCreateTime, startCreateTime)
//							.le(StringUtils.isNotBlank(endCreateTime), CrmClientData::getCreateTime, endCreateTime));
//			map.put("provinceId", provinceId);
//			String name = SysRegion.builder().build().selectOne(new QueryWrapper<SysRegion>().lambda()
//					.eq(SysRegion::getLevel, 1).eq(SysRegion::getKeyId, provinceId).select(SysRegion::getRegionName))
//					.getRegionName();
//			map.put("provinceName", name);
//			map.put("num", num);
//		}
//		return map;
//	}
1439

1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
	/**
	 * 统计图6-籍贯
	 * 
	 * @param orgCode
	 * @param belongGroup
	 * @param createTime
	 * @return
	 */
	private List<CrmCartogramDto> getCartogram6(Integer orgCode, Integer belongUser, Integer belongGroup,
			String startCreateTime, String endCreateTime) {
		return crmClientDataMapper.crmCartogram(orgCode, belongUser, belongGroup, startCreateTime, endCreateTime);
邓实川 committed
1451
	}
邓实川 committed
1452

邓实川 committed
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
	/**
	 * 导入
	 * 
	 * @param url 上传路径
	 * @return
	 */
	@PostMapping("importClient")
	@Transactional
	@ApiOperation(value = "导入客户", httpMethod = "POST", notes = "接口发布说明")
	public Result<Object> importClient(@CurrentUser UserBean userBean, @RequestParam String url) {
ilal committed
1463 1464 1465 1466 1467 1468 1469
		
		int toinsert = 0;//新增
		int toupdate = 0;//更新
		int failure = 0;//失败
		List<Map<String, String>> reasons = Lists.newArrayList();
		
		
邓实川 committed
1470 1471 1472
		Integer empNum = getEmpNum(userBean);
		Integer orgCode = getOrgCode(userBean);

1473 1474 1475 1476 1477 1478 1479 1480
//		String filePath = null;
//		String time = null;
//		try {
//			time = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
//			filePath = FileHelper.downLoadFromUrl(url, time + ".xls", realPath);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
邓实川 committed
1481 1482 1483
		// 从文件中读取Excel为ExcelReader
		ExcelReader reader;
		// 从流中读取Excel为ExcelReader(比如从ClassPath中读取Excel文件)
1484 1485 1486
//		reader = ExcelUtil.getReader(ResourceUtil.getStream(filePath));
		// 从网络流OSS中读取excel资源
		reader = getReaderByUrl(url);
邓实川 committed
1487 1488 1489
		reader.addHeaderAlias("*客户名称(必填)", "clientName");
		reader.addHeaderAlias("*所属分组(必填)", "belongGroupName");
		reader.addHeaderAlias("行业", "industryName");
ilal committed
1490 1491
		reader.addHeaderAlias("微信", "wechat");
		reader.addHeaderAlias("产品", "product");
邓实川 committed
1492 1493 1494 1495 1496 1497 1498
		reader.addHeaderAlias("客户类型", "clientTypeName");
		reader.addHeaderAlias("手机号码(客户)", "clientCellphone");
		reader.addHeaderAlias("座机号码", "clientTelephone");
		reader.addHeaderAlias("官网", "clientUrl");
		reader.addHeaderAlias("邮箱", "clientEmail");
		reader.addHeaderAlias("地址", "address");
		reader.addHeaderAlias("联系人姓名", "linkName");
ilal committed
1499
		reader.addHeaderAlias("手机号码", "cellphone");
邓实川 committed
1500 1501 1502 1503 1504 1505
		reader.addHeaderAlias("所属公司", "company");
		reader.addHeaderAlias("性别", "gender");
		reader.addHeaderAlias("职位", "position");
		reader.addHeaderAlias("办公电话", "telephone");
		reader.addHeaderAlias("个人邮箱", "email");
		reader.addHeaderAlias("个人地址", "linkAddress");
ilal committed
1506
		reader.addHeaderAlias("跟进记录", "followuprecord");
邓实川 committed
1507 1508
		List<CrmClientDataImportVo> list = reader.read(0, 1, CrmClientDataImportVo.class);
		for (CrmClientDataImportVo crmClientDataImportVo : list) {
ilal committed
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
			
			String name = crmClientDataImportVo.getClientName();
			
			Map<String, String> map = Maps.newHashMap();
			
			try {
				CrmClientData crmd = CrmClientData.builder().build().selectOne(new QueryWrapper<CrmClientData>().lambda().eq(CrmClientData::getBelongUser, empNum).eq(CrmClientData::getClientName, crmClientDataImportVo.getClientName())
						.eq(CrmClientData::getClientCellphone, crmClientDataImportVo.getClientCellphone()));
				if(crmd == null) {
					String clientName = crmClientDataImportVo.getClientName(); // 客户名称(必填)
					String belongGroupName = crmClientDataImportVo.getBelongGroupName(); // 分组名(必填)
					String industryName = crmClientDataImportVo.getIndustryName(); // 行业名
					String clientTypeName = crmClientDataImportVo.getClientTypeName(); // 客户类型名
					String clientCellphone = crmClientDataImportVo.getClientCellphone(); // 客户手机号
					String clientTelephone = crmClientDataImportVo.getClientTelephone(); // 客户座机号
					String clientUrl = crmClientDataImportVo.getClientUrl(); // 官网
					String clientEmail = crmClientDataImportVo.getClientEmail(); // 邮箱
					String clientAddress = crmClientDataImportVo.getAddress(); // 地址
					String wechat = crmClientDataImportVo.getWechat(); // 微信
					String product = crmClientDataImportVo.getProduct(); // 产品
					String linkname = crmClientDataImportVo.getLinkName(); // 联系人姓名
					String linkcellphone = crmClientDataImportVo.getCellphone(); // 手机号码
					String linkcompany = crmClientDataImportVo.getCompany(); // 所属公司
					String linkgender = crmClientDataImportVo.getGender(); // 性别
					String linkposition = crmClientDataImportVo.getPosition(); // 职位
					String linktelephone = crmClientDataImportVo.getTelephone(); // 办公电话
					String linkemail = crmClientDataImportVo.getEmail(); // 个人邮箱
					String linkaddress = crmClientDataImportVo.getLinkAddress(); // 个人地址
					
					String followuprecord = crmClientDataImportVo.getFollowuprecord(); // 跟进记录

					CrmClientData crmClientData = new CrmClientData();

					// 业务组
					CrmBusinessGroup crmBusinessGroup = CrmBusinessGroup.builder().build()
							.selectOne(new LambdaQueryWrapper<CrmBusinessGroup>().eq(CrmBusinessGroup::getOrgCode, orgCode)
									.eq(CrmBusinessGroup::getName, belongGroupName).select(CrmBusinessGroup::getId));
					if (crmBusinessGroup == null)
						return ResultUtil.error("没有匹配的业务组名称");
lal committed
1548 1549
					
					int industryid = 0;
ilal committed
1550 1551 1552 1553 1554
					if (StrUtil.isNotBlank(industryName)) {
						// 行业字典
						IndustryClass industryClass = IndustryClass.builder().build()
								.selectOne(new LambdaQueryWrapper<IndustryClass>().eq(IndustryClass::getName, industryName)
										.select(IndustryClass::getId));
lal committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
						if (industryClass == null) {
							
							IndustryClass incls = IndustryClass.builder().build();
							incls.setName(industryName);
							incls.insert();
							industryid = incls.getId();
//							return ResultUtil.error("没有匹配的行业名称");
						}else {
							industryid = industryClass.getId();
						}
						crmClientData.setIndustry(industryid);
ilal committed
1566
					}
邓实川 committed
1567

ilal committed
1568 1569 1570 1571 1572 1573 1574 1575 1576
					if (StrUtil.isNotBlank(clientTypeName)) {
						// 客户类型
						ClientTypeClass clientTypeClass = ClientTypeClass.builder().build()
								.selectOne(new LambdaQueryWrapper<ClientTypeClass>()
										.eq(ClientTypeClass::getName, clientTypeName).select(ClientTypeClass::getId));
						if (clientTypeClass == null)
							return ResultUtil.error("没有匹配的客户类型名称");
						crmClientData.setClientType(clientTypeClass.getId());
					}
邓实川 committed
1577

ilal committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
					crmClientData.setClientStatus(1);
					if(!("").equals(linkname) && linkname != null || !("").equals(linkcellphone) && linkcellphone != null) {
						crmClientData.setContactsNum(1);
					}
					crmClientData.setBelongUser(empNum);
					crmClientData.setBelongUserName(getEmpName(orgCode, empNum));
					crmClientData.setClientName(clientName);
					crmClientData.setBelongGroupName(belongGroupName);
					crmClientData.setBelongGroup(crmBusinessGroup.getId());
					crmClientData.setIndustryName(industryName);
					
					crmClientData.setWechat(wechat);
					crmClientData.setProduct(product);
邓实川 committed
1591

ilal committed
1592
					crmClientData.setClientTypeName(clientTypeName);
邓实川 committed
1593

ilal committed
1594 1595 1596 1597 1598 1599 1600 1601
					crmClientData.setClientCellphone(clientCellphone);
					crmClientData.setClientTelephone(clientTelephone);
					crmClientData.setClientUrl(clientUrl);
					crmClientData.setClientEmail(clientEmail);
					crmClientData.setAddress(clientAddress);
					crmClientData.setOrgCode(orgCode);
					crmClientData.setFollowuprecord(followuprecord);//跟进记录
					crmClientData.insert();
邓实川 committed
1602

ilal committed
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
					Integer cid = crmClientData.getId();
					
					if(!("").equals(linkname) && linkname != null || !("").equals(linkcellphone) && linkcellphone != null) {
						CrmClientContacts contacts = new CrmClientContacts();
						contacts.setName(linkname);
						contacts.setCellphone(linkcellphone);
						contacts.setCompany(linkcompany);
						if (("女").equals(linkgender))
							contacts.setGender(0);
						else if (("男").equals(linkgender))
							contacts.setGender(1);
						else
							contacts.setGender(null);
						contacts.setPosition(linkposition);
						contacts.setTelephone(linktelephone);
						contacts.setEmail(linkemail);
						contacts.setAddress(linkaddress);
						if (!(linkname == null && linkcellphone == null && linkcompany == null && linkgender == null
								&& linkposition == null && linktelephone == null && linkemail == null && linkaddress == null)) {
							contacts.setCid(cid);
							contacts.insert();
						}
					}
					toinsert++;
				}else {
					failure++;
					map.put("name", name);
					map.put("msg", "该客户已存在");
					reasons.add(map);
ilal committed
1632
				}
ilal committed
1633 1634 1635 1636 1637
			} catch (Exception e) {
				failure++;
				map.put("name", name);
				map.put("msg", StringUtils.isBlank(e.getMessage()) ? "必填数据出现空值" : e.getMessage());
				reasons.add(map);
邓实川 committed
1638
			}
ilal committed
1639
			
邓实川 committed
1640
		}
ilal committed
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
		
		ImportDescriptionDto imds = new ImportDescriptionDto();
		imds.setSuccess(toinsert + toupdate);//成功
		imds.setToinsert(toinsert);
		imds.setToupdate(toupdate);//更新
		imds.setFailure(failure);//失败
		imds.setReasons(reasons);
		
		return ResultUtil.data(imds,"成功");
//		return ResultUtil.success("导入成功");
邓实川 committed
1651
	}
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

	private ExcelReader getReaderByUrl(String url) {
		InputStream is = null;
		try {
			is = new URL(url).openConnection().getInputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ExcelUtil.getReader(is);
	}
邓实川 committed
1662
}