package com.fs.company.controller.qw;

import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.ServletUtils;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.param.QwCrmCourseStudyDetailParams;
import com.fs.qw.param.QwCrmCourseStudyParams;
import com.fs.qw.param.QwExternalContactParam;
import com.fs.qw.service.IQwExternalContactCrmService;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.vo.QwCrmCourseStudyVO;
import com.fs.qw.vo.QwExternalContactVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 企微客户-绑CRMController
 *
 * @author fs
 * @date 2024-08-09
 */
@RestController
@RequestMapping("/qw/externalContactCrm")
public class QwExternalContactCrmController extends BaseController
{
    @Autowired
    private IQwExternalContactCrmService qwExternalContactCrmService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private IQwExternalContactService qwExternalContactService;

//    /**
//     * 查询企微客户-绑CRM列表
//     */
//    @PreAuthorize("@ss.hasPermi('qw:externalContactCrm:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(QwExternalContactCrm qwExternalContactCrm)
//    {
//        startPage();
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        qwExternalContactCrm.setCompanyId(loginUser.getCompany().getCompanyId());
//        List<QwExternalContactCrmVO> list = qwExternalContactCrmService.selectQwExternalContactCrmListVO(qwExternalContactCrm);
//        return getDataTable(list);
//    }


    /**
     * 查询企业微信客户列表
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContact:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwExternalContactParam qwExternalContact)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        qwExternalContact.setCompanyId(companyId);
        List<QwExternalContactVO> list = qwExternalContactService.selectQwExternalContactListVO(qwExternalContact);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('qw:externalContact:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(QwExternalContactParam qwExternalContact)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwExternalContact.setCompanyUserId(loginUser.getUser().getUserId());
        qwExternalContact.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwExternalContactVO> list = qwExternalContactService.selectQwExternalContactListVO(qwExternalContact);
        return getDataTable(list);
    }


    /**
     * 获取企业微信客户详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:externalContactCrm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwExternalContactService.selectQwExternalContactById(id));
    }


//    /**
//     * 获取企微客户-绑CRM详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('qw:externalContactCrm:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return AjaxResult.success(qwExternalContactCrmService.selectQwExternalContactCrmById(id));
//    }


    /**
     * 客户课程旅程 获取绑定了CRM（CRM绑定了小程序）的企业微信客户的课程列表
     */

    @GetMapping("/getQwCrmCourseStudyList")
    public TableDataInfo getQwCrmCourseStudyList(QwCrmCourseStudyParams qwFsUserParam){
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        qwFsUserParam.setCompanyId(companyId);
        List<QwCrmCourseStudyVO> list = qwExternalContactCrmService.getQwCrmCourseStudyList(qwFsUserParam);
        if (list != null) {
            for (QwCrmCourseStudyVO vo : list) {
                if(vo.getMobile()!=null){
                    vo.setMobile(vo.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }
            }
        }
        return getDataTable(list);
    }


    /**
     * 查看客户课程旅程的某个客户的详情
     */
    @GetMapping("/getQwCrmCourseStudyDetail")
    public TableDataInfo getQwCrmCourseStudyDetail(QwCrmCourseStudyDetailParams studyDetailParams){
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        studyDetailParams.setCompanyId(companyId);

        List<QwCrmCourseStudyVO> list = qwExternalContactCrmService.getQwCrmCourseStudyDetail(studyDetailParams);
        return getDataTable(list);
    }

}
