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.StringUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.crm.domain.CrmCustomerVisit;
import com.fs.crm.param.CrmCustomerVisitListParam;
import com.fs.crm.service.ICrmCustomerVisitService;
import com.fs.crm.vo.CrmCustomerVisitListVO;
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;

    /**
     * 查询跟进列表
     */
    @PreAuthorize("@ss.hasPermi('crm:customerVisit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CrmCustomerVisitListParam param)
    {
        startPage();
        if(!StringUtils.isEmpty(param.getCreateTimeRange())){
            param.setCustomerCreateTime(param.getCreateTimeRange().split("--"));
        }
        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<CrmCustomerVisitListVO> list = crmCustomerVisitService.selectCrmCustomerVisitListVO(param);
        ExcelUtil<CrmCustomerVisitListVO> util = new ExcelUtil<CrmCustomerVisitListVO>(CrmCustomerVisitListVO.class);
        return util.exportExcel(list, "customerVisit");
    }

    /**
     * 获取跟进详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:customerVisit:query')")
    @GetMapping(value = "/{visitId}")
    public AjaxResult getInfo(@PathVariable("visitId") Long visitId)
    {
        return AjaxResult.success(crmCustomerVisitService.selectCrmCustomerVisitById(visitId));
    }

    /**
     * 新增跟进
     */
    @PreAuthorize("@ss.hasPermi('crm:customerVisit:add')")
    @Log(title = "跟进", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CrmCustomerVisit crmCustomerVisit)
    {
        return toAjax(crmCustomerVisitService.insertCrmCustomerVisit(crmCustomerVisit));
    }

    /**
     * 修改跟进
     */
    @PreAuthorize("@ss.hasPermi('crm:customerVisit:edit')")
    @Log(title = "跟进", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CrmCustomerVisit crmCustomerVisit)
    {
        return toAjax(crmCustomerVisitService.updateCrmCustomerVisit(crmCustomerVisit));
    }

    /**
     * 删除跟进
     */
    @PreAuthorize("@ss.hasPermi('crm:customerVisit:remove')")
    @Log(title = "跟进", businessType = BusinessType.DELETE)
	@DeleteMapping("/{visitIds}")
    public AjaxResult remove(@PathVariable Long[] visitIds)
    {
        return toAjax(crmCustomerVisitService.deleteCrmCustomerVisitByIds(visitIds));
    }
}
