package com.fs.company.controller.crm;

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.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.crm.param.CrmCustomerVisitAddParam;
import com.fs.crm.param.CrmCustomerVisitListParam;
import com.fs.crm.service.ICrmCustomerVisitService;
import com.fs.crm.vo.CrmCustomerVisitExportVO;
import com.fs.crm.vo.CrmCustomerVisitListVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
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 2022-12-21
 */
@RestController
@RequestMapping("/crm/customerVisit")
public class CrmCustomerVisitController extends BaseController
{
    @Autowired
    private ICrmCustomerVisitService crmCustomerVisitService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询跟进列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CrmCustomerVisitListParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        startPage();
        param.setCompanyId(loginUser.getCompany().getCompanyId());
        if(!StringUtils.isEmpty(param.getReceiveTimeRange())){
            param.setReceiveTimeList(param.getReceiveTimeRange().split("--"));
        }
        List<CrmCustomerVisitListVO> list = crmCustomerVisitService.selectCrmCustomerVisitListVO(param);
        return getDataTable(list);
    }

    /**
     * 导出跟进列表
     */
    @PreAuthorize("@ss.hasPermi('crm:customerVisit:export')")
    @Log(title = "跟进", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CrmCustomerVisitListParam param)
    {
        List<CrmCustomerVisitExportVO> list = crmCustomerVisitService.selectCrmCustomerVisitExportListVO(param);
        ExcelUtil<CrmCustomerVisitExportVO> util = new ExcelUtil<CrmCustomerVisitExportVO>(CrmCustomerVisitExportVO.class);
        return util.exportExcel(list, "customerVisit");
    }



    /**
     * 新增跟进
     */
    @Log(title = "跟进", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody CrmCustomerVisitAddParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        return crmCustomerVisitService.addVisit(param);
    }



}
