package com.fs.app.controller;


import com.fs.app.annotation.Login;
import com.fs.app.utils.JwtUtils;
import com.fs.common.annotation.DataScope;
import com.fs.common.annotation.Log;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.OrderUtils;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.company.param.CompanySmsLogsListQueryParam;
import com.fs.company.param.CompanyVoiceLogsListQueryParam;
import com.fs.company.service.ICompanySmsLogsService;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.service.ICompanyVoiceLogsService;
import com.fs.company.vo.CompanySmsLogsListQueryVO;
import com.fs.company.vo.CompanyVoiceLogsListVO;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.domain.CrmCustomerContacts;
import com.fs.crm.domain.CrmCustomerExt;
import com.fs.crm.domain.CrmCustomerUser;
import com.fs.crm.param.*;
import com.fs.crm.service.*;
import com.fs.crm.vo.*;
import com.fs.store.param.FsCustomerStoreOrderListQueryParam;
import com.fs.store.service.IFsStoreOrderService;
import com.fs.store.vo.FsCustomerStoreOrderListQueryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import static com.fs.common.constant.Constants.PAGE_SIZE;


@Api("客户接口")
@RestController
@RequestMapping(value="/app/crm")
public class CrmController extends  AppBaseController {
	@Autowired
	private ICrmCustomerContactsService crmCustomerContactsService;
	@Autowired
	private ICrmCustomerExtService crmCustomerExtService;
	@Autowired
    JwtUtils jwtUtils;
	@Autowired
	ICrmCustomerService crmCustomerService;
	@Autowired
	ICrmCustomerUserService crmCustomerUserService;
	@Autowired
	ICompanyUserService companyUserService;
	@Autowired
	private ICrmCustomerLogsService crmCustomerLogsService;
	@Autowired
	private ICrmCustomerVisitService crmCustomerVisitService;
	@Autowired
	ICompanyVoiceLogsService companyVoiceLogsService;
	@Autowired
	ICompanySmsLogsService companySmsLogsService;
	@Autowired
	IFsStoreOrderService storeOrderService;

	@Login
	@ApiOperation("获取公海客户列表")
	@GetMapping("/getFullCustomerList")
	@DataScope(deptAlias = "c",userAlias = "c")
	public R getFullCustomerList(CrmFullCustomerListQueryParam param){
		PageHelper.startPage(param.getPage(), param.getPageSize());
		param.setCompanyId(getCompanyId());
		if(getCompanyId()==116){   // 河北湘银信息咨询服务有限公司（JZ-1）客户假删除不显示
			param.setCompanyId(0L);
		}
		List<CrmFullCustomerListQueryVO> list = crmCustomerService.selectCrmFullCustomerListQuery(param);
		PageInfo<CrmFullCustomerListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@Login
	@ApiOperation("获取我的客户列表")
	@GetMapping("/getMyCustomerList")
	public R getMyCustomerList(CrmMyCustomerListQueryParam param){
		PageHelper.startPage(param.getPage(), param.getPageSize());
		param.setCompanyId(getCompanyId());
		if(getCompanyId()==116){   // 河北湘银信息咨询服务有限公司（JZ-1）客户假删除不显示
			param.setCompanyId(0L);
		}
		param.setCompanyUserId(Long.parseLong(getUserId()));
		List<CrmMyCustomerListQueryVO> list = crmCustomerService.selectCrmMyCustomerListQuery(param);
		PageInfo<CrmMyCustomerListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@Login
	@ApiOperation("获取客户详情")
	@GetMapping("/getCustomerDetails")
	public R getMyCustomerDetails(
			HttpServletRequest request,
			@ApiParam(required = true, name = "customerId", value = "customerId")
			@RequestParam(value = "customerId", required = false) Long customerId){
		CrmCustomer customer=crmCustomerService.selectCrmCustomerById(customerId);
		Boolean isReceive=false;
		if(customer.getIsReceive()!=null&&customer.getIsReceive()==1&&customer.getReceiveUserId()!=null&&getUserId().equals(customer.getReceiveUserId().toString())){
			isReceive=true;
		}
		return R.ok().put("data",customer).put("isReceive",isReceive);
	}
	/**
	 * 修改客户
	 */
	@Login
	@ApiOperation("编辑客户")
	@PostMapping("/edit")
	public R edit(@RequestBody CrmCustomer crmCustomer)
	{
		if(crmCustomerService.updateCrmCustomer(crmCustomer)>0){
			return R.ok();
		}
		else{
			return R.error();
		}
	}
	@Login
	@ApiOperation("添加客户")
	@PostMapping("/add")
	public R add(@RequestBody CrmCustomer crmCustomer)
	{
		CompanyUser companyUser=companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
		crmCustomer.setDeptId(companyUser.getDeptId());
		crmCustomer.setCustomerCode(OrderUtils.getOrderNo());
		crmCustomer.setIsLine(0);
		crmCustomer.setIsDel(0);
		crmCustomer.setIsReceive(0);
		crmCustomer.setStatus(1);
		crmCustomer.setDeptId(companyUser.getDeptId());
		crmCustomer.setCompanyId(getCompanyId());
		crmCustomer.setCreateUserId(Long.parseLong(getUserId()));
		if(crmCustomerService.insertCrmCustomer(crmCustomer)>0){
			CrmCustomeReceiveParam param=new CrmCustomeReceiveParam();
			String operName = companyUser.getNickName();
			param.setCompanyId(companyUser.getCompanyId());
			param.setCompanyUserId(companyUser.getUserId());
			param.setCustomerId(crmCustomer.getCustomerId());
			crmCustomerService.receive(param,operName);
			return R.ok();
		}
		else{
			return R.error();
		}
	}

	//认领
	@Login
	@ApiOperation("认领")
	@PostMapping("/receive")
	public R receive(@RequestBody CrmCustomeReceiveParam param)
	{
		CompanyUser companyUser=companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
		String operName = companyUser.getNickName();
		param.setCompanyId(companyUser.getCompanyId());
		param.setCompanyUserId(companyUser.getUserId());
		return crmCustomerService.receive(param,operName);

	}
	//回收
	@Login
	@PostMapping("/recover")
	@ApiOperation("回收公海")
	public R recover(@RequestBody CrmCustomeRecoverParam param)
	{
		CompanyUser companyUser=companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
		String operName = companyUser.getNickName();
		param.setCompanyId(companyUser.getCompanyId());
		param.setCompanyUserId(companyUser.getUserId());
		return crmCustomerService.recover(param,operName);

	}

	@Login
	@PostMapping("/assignUser")
	@ApiOperation("转移用户")
	public R assignUser(@RequestBody CrmCustomeAssignUserParam param)
	{
		CompanyUser companyUser=companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
		String operName = companyUser.getNickName();
		return crmCustomerService.assignUser(param,operName);

	}

	@GetMapping("/getCustomerLogsList")
	@ApiOperation("获取客户日志")
	public R getCustomerLogsList(CrmCustomerLogsListQueryParam param)
	{
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		List<CrmCustomerLogsListQueryVO> list = crmCustomerLogsService.selectCrmCustomerLogsListQuery(param);
		PageInfo<CrmCustomerLogsListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@GetMapping("/getCustomerVisitList")
	@ApiOperation("获取客户跟进记录")
	public R getCustomerVisitList(CrmCustomerVisitListQueryParam param)
	{
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		param.setCompanyId(getCompanyId());
		List<CrmCustomerVisitListVO> list = crmCustomerVisitService.selectCrmCustomerVisitListQuery(param);
		PageInfo<CrmCustomerVisitListVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}
	@Login
	@ApiOperation("提交跟进")
	@PostMapping("/addVisit")
	public R addVisit(@RequestBody CrmCustomerVisitAddParam param) {
		param.setCompanyUserId(Long.parseLong(getUserId()));
		return crmCustomerVisitService.addVisit(param);
	}


	@Login
	@GetMapping("/getCustomerVoiceLogsList")
	@ApiOperation("获取客户通话记录")
	public R getCustomerVoiceLogsList(CompanyVoiceLogsListQueryParam param) {
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		param.setCompanyId(getCompanyId());
		List<CompanyVoiceLogsListVO> list = companyVoiceLogsService.selectCompanyVoiceLogsListQuery(param);
		PageInfo<CompanyVoiceLogsListVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@Login
	@GetMapping("/getCustomerSmsLogsList")
	@ApiOperation("获取客户短信记录")
	public R getCustomerSmsLogsList(CompanySmsLogsListQueryParam param) {
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		param.setCompanyId(getCompanyId());
		List<CompanySmsLogsListQueryVO> list = companySmsLogsService.selectCompanySmsLogsListQuery(param);
		PageInfo<CompanySmsLogsListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@Login
	@GetMapping("/getCustomerOrderList")
	@ApiOperation("获取订单列表")
	public R getCustomerOrderList(FsCustomerStoreOrderListQueryParam param) {
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		param.setCompanyId(getCompanyId());
		List<FsCustomerStoreOrderListQueryVO> list = storeOrderService.selectFsCustomerStoreOrderListQuery(param);
		PageInfo<FsCustomerStoreOrderListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}

	@Login
	@PostMapping("/editTags")
	@ApiOperation("修改标签")
	public R editTags(@RequestBody CrmCustomerEditTagsParam param) {
		return crmCustomerService.editTags(param);
	}


	@Login
	@ApiOperation("获取自定义字段列表")
	@GetMapping("/getCustomerExt")
	public R getCustomerExt(CrmCustomerExt crmCustomerExt) {
		CrmCustomerExt map=new CrmCustomerExt();
		map.setStatus(1);
		map.setCompanyId(getCompanyId());
		List<CrmCustomerExt> list = crmCustomerExtService.selectCrmCustomerExtList(map);
		return R.ok().put("data",list);
	}



 	@Login
	@ApiOperation("获取客户联系人")
	@GetMapping("/getCustomerContacts")
	public R getCustomerContacts(CrmCustomerContactsListQueryParam param) {
		PageHelper.startPage(param.getPage(), PAGE_SIZE);
		param.setCompanyId(getCompanyId());
		List<CrmCustomerContactsListQueryVO> list = crmCustomerContactsService.selectCrmCustomerContactsListQuery(param);
		PageInfo<CrmCustomerContactsListQueryVO> listPageInfo=new PageInfo<>(list);
		return R.ok().put("data",listPageInfo);
	}


	@Login
	@ApiOperation("获取客户联系人详情")
	@GetMapping(value = "/getCustomerContactsDetails")
	public R getCustomerContactsDetails(@ApiParam(required = true, name = "contactsId", value = "contactsId") @RequestParam(value = "contactsId", required = false) Long contactsId)
	{
		CrmCustomerContacts contacts=crmCustomerContactsService.selectCrmCustomerContactsById(contactsId);
		return R.ok().put("data",contacts);
	}


	@Login
	@ApiOperation("新增联系人")
	@PostMapping("/addContacts")
	public R addContacts(@RequestBody CrmCustomerContacts crmCustomerContacts)
	{
		crmCustomerContacts.setCompanyId(getCompanyId());
		if(crmCustomerContactsService.insertCrmCustomerContacts(crmCustomerContacts)>0){
			return R.ok();
		}
		else{
			return R.error();
		}
	}

	@Login
	@ApiOperation("修改联系人")
	@PostMapping("/editContacts")
	public R editContacts(@RequestBody CrmCustomerContacts crmCustomerContacts)
	{
		if(crmCustomerContactsService.updateCrmCustomerContacts(crmCustomerContacts)>0){
			return R.ok();
		}
		else{
			return R.error();
		}
	}
	@Login
	@ApiOperation("删除联系人")
	@PostMapping("/delContacts")
	public R delContacts(@RequestBody CrmCustomerContacts crmCustomerContacts)
	{
		if(crmCustomerContactsService.deleteCrmCustomerContactsById(crmCustomerContacts.getContactsId())>0){
			return R.ok();
		}
		else{
			return R.error();
		}
	}

	@Login
	@ApiOperation("查询客户")
	@GetMapping("/getCustomerListBySearch")
	public R getCustomerListBySearch(CrmCustomerSearchParam param){
		param.setCompanyId(getCompanyId());
		List<CrmCustomer> list = crmCustomerService.selectCrmCustomerListBySearch(param);
		return R.ok().put("data",list);
	}


}
