package com.fs.crm.controller;

import com.fs.common.annotation.Log;
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.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.crm.domain.CrmCustomerContacts;
import com.fs.crm.service.ICrmCustomerContactsService;
import com.fs.crm.vo.CrmLineCustomerListQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 客户联系人Controller
 * 
 * @author fs
 * @date 2023-03-14
 */
@RestController
@RequestMapping("/crm/customerContacts")
public class CrmCustomerContactsController extends BaseController
{
    @Autowired
    private ICrmCustomerContactsService crmCustomerContactsService;
    @Autowired
    private TokenService tokenService;

    @GetMapping("/list")
    public TableDataInfo list(CrmCustomerContacts crmCustomerContacts)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        crmCustomerContacts.setCompanyId(loginUser.getCompany().getCompanyId());
        List<CrmCustomerContacts> list = crmCustomerContactsService.selectCrmCustomerContactsList(crmCustomerContacts);
        if (list != null) {
            for (CrmCustomerContacts vo : list) {
                if(vo.getMobile()!=null){
                    vo.setMobile(vo.getMobile().replaceAll("(\\d{3})\\d*(\\d{4})", "$1****$2"));
                }

            }
        }
        return getDataTable(list);
    }



    /**
     * 获取客户联系人详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:customerContacts:query')")
    @GetMapping(value = "/{contactsId}")
    public AjaxResult getInfo(@PathVariable("contactsId") Long contactsId)
    {
        return AjaxResult.success(crmCustomerContactsService.selectCrmCustomerContactsById(contactsId));
    }

    /**
     * 新增客户联系人
     */
    @PreAuthorize("@ss.hasPermi('crm:customerContacts:add')")
    @Log(title = "客户联系人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmCustomerContacts crmCustomerContacts)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        crmCustomerContacts.setCompanyId(loginUser.getCompany().getCompanyId());
        return toAjax(crmCustomerContactsService.insertCrmCustomerContacts(crmCustomerContacts));
    }

    /**
     * 修改客户联系人
     */
    @PreAuthorize("@ss.hasPermi('crm:customerContacts:edit')")
    @Log(title = "客户联系人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCustomerContacts crmCustomerContacts)
    {
        return toAjax(crmCustomerContactsService.updateCrmCustomerContacts(crmCustomerContacts));
    }

    /**
     * 删除客户联系人
     */
    @PreAuthorize("@ss.hasPermi('crm:customerContacts:remove')")
    @Log(title = "客户联系人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contactsIds}")
    public AjaxResult remove(@PathVariable Long[] contactsIds)
    {
        return toAjax(crmCustomerContactsService.deleteCrmCustomerContactsByIds(contactsIds));
    }
}
