package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.common.annotation.Log;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.company.util.OrderUtils;
import com.fs.crm.domain.CrmCustomer;
import com.fs.crm.param.*;
import com.fs.crm.service.ICrmCustomerService;
import com.fs.crm.service.ICrmCustomerUserService;
import com.fs.crm.vo.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户Controller
 *
 * @author fs
 * @date 2022-12-21
 */
@RestController
@RequestMapping("/app/crmCustomer")
public class CrmCustomerController extends AppBaseController
{
    @Autowired
    private ICrmCustomerService crmCustomerService;
    @Autowired
    ICompanyUserService companyUserService;

    @Autowired
    ICrmCustomerUserService crmCustomerUserService;

    @Login
    @ApiOperation("获取线索客户列表")
    @GetMapping("/getLineCustomerList")
    public R getLineCustomerList( CrmLineCustomerListQueryParam param)
    {
        CompanyUser user = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setCompanyId(user.getCompanyId());
        List<CrmLineCustomerListQueryVO> list = crmCustomerService.selectCrmLineCustomerListQuery(param);
        if (list != null) {
            for (CrmLineCustomerListQueryVO vo : list) {
                if(vo.getMobile()!=null){
                    vo.setMobile(vo.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }
        }
        PageInfo<CrmLineCustomerListQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    @Login
    @ApiOperation("获取我的客户列表")
    @GetMapping("/getMyCustomerList")
    public R getMyCustomerList(  CrmMyCustomerListQueryParam param){
        PageHelper.startPage(param.getPageNum(), param.getPageSize());

        CompanyUser user = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));

        param.setCompanyUserId(user.getUserId());
        param.setCompanyId(user.getCompanyId());

        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCustomerCreateTime(param.getCreateTimeRange().split("--"));
        }
        List<CrmMyCustomerListQueryVO> list = crmCustomerService.selectCrmMyCustomerListQuery(param);
        if (list != null) {
            for (CrmMyCustomerListQueryVO vo : list) {
                if(vo.getMobile()!=null){
                    vo.setMobile(vo.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
            }
        }

        PageInfo<CrmMyCustomerListQueryVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);

    }

    @Login
    @ApiOperation("获取我的客户详情")
    @GetMapping("/getCustomerDetails")
    public R getCustomerDetails1(
            HttpServletRequest request,
            @ApiParam(required = true, name = "customerId", value = "客户ID")
            @RequestParam(value = "customerId", required = false) Long customerId){

        CompanyUser user = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));

        CrmCustomer customer=crmCustomerService.selectCrmCustomerById(customerId);
        if (!user.getUserId().equals(customer.getReceiveUserId())){
            return R.error("无权限查看");
        }
        customer.setMobile(customer.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));

        return R.ok().put("customer",customer);

    }





    /**
    * 获取所有CRM里面关联了企业微信，然后未加为好友的客户列表
    */
    @Login
    @ApiOperation("加好友助手(获取关联了企微,未加好友的)")
    @PostMapping("/getFriendAssistant")
    public R getFriendAssistantList(@RequestBody CrmCustomerQueryFriendParam param){

        PageHelper.startPage(param.getPageNum(), param.getPageSize());

        CompanyUser user = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
        param.setCompanyId(user.getCompanyId());

        List<CrmCustomerListQueryFriendVO> list = crmCustomerService.getFriendAssistantListVO(param);

        if (list != null) {
            for (CrmCustomerListQueryFriendVO vo : list) {
                if(vo.getMobile()!=null){
                    vo.setMobile(vo.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
            }
        }

        PageInfo<CrmCustomerListQueryFriendVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }

    /**
    * 修改 是否添加好友
    */
    @Login
    @ApiOperation("修改")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PostMapping("/editCrmCustomer")
    public R editCrmCustomer(@RequestBody CrmCustomer param) {
        CrmCustomer customer=crmCustomerService.selectCrmCustomerById(param.getCustomerId());
        CompanyUser user = companyUserService.selectCompanyUserById(Long.parseLong(getUserId()));
        if (!customer.getReceiveUserId().equals(user.getUserId())) {
            return R.error("只能修改自己的客户");
        }
        if(crmCustomerService.updateCrmCustomer(param)>0){
            return R.ok();
        }else {
            return R.error();
        }

    }


}
