package com.ruoyi.project.business.controller;

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

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.annotation.DataUpdateCheck;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.domain.TcFollow;
import com.ruoyi.project.business.domain.TcFollowForList;
import com.ruoyi.project.business.service.ITcFollowService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 跟进管理Controller
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@RestController
@RequestMapping("/business/follow")
public class TcFollowController extends BaseController
{
    @Autowired
    private ITcFollowService tcFollowService;

    /**
     * 查询跟进记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TcFollow tcFollow)
    {
        startPage();
        List<TcFollowForList> list = tcFollowService.selectTcFollowList(tcFollow);
        return getDataTable(list);
    }

    /**
     * 查询跟进记录列表
     */
    @GetMapping("/listByAccount")
    public TableDataInfo selectTcFollowListByAccount(TcFollow tcFollow)
    {
        startPage();
        List<TcFollowForList> list = tcFollowService.selectTcFollowListByAccount(tcFollow);
        return getDataTable(list);
    }

    /**
     * 导出跟进记录列表
     */
    @Log(title = "跟进记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TcFollow tcFollow)
    {
        List<TcFollowForList> list = tcFollowService.selectTcFollowList(tcFollow);
        ExcelUtil<TcFollowForList> util = new ExcelUtil<TcFollowForList>(TcFollowForList.class);
        util.exportExcel(response, list, "follow record list");
    }

    /**
     * 获取跟进记录详细信息
     */
    @GetMapping(value = "/{followId}")
    public AjaxResult getInfo(@PathVariable("followId") Long followId)
    {
        return success(tcFollowService.selectTcFollowByFollowId(followId));
    }

    /**
     * 新增跟进记录
     */
    @Log(title = "跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcFollow tcFollow)
    {
        return toAjax(tcFollowService.insertTcFollow(tcFollow));
    }

    /**
     * 修改跟进记录
     */
    @Log(title = "跟进记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "follow_id", tableName ="tc_follow")
    public AjaxResult edit(@RequestBody TcFollow tcFollow)
    {
        return toAjax(tcFollowService.updateTcFollow(tcFollow));
    }

    /**
     * 删除跟进记录
     */
    @Log(title = "跟进记录", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @DataUpdateCheck(key = "follow_id", tableName ="tc_follow")
    public AjaxResult remove(@Validated @RequestBody TcFollowForList data)
    {
        return toAjax(tcFollowService.deleteTcFollowByFollowId(data.getFollowId()));
    }

    /**
     * 根据跟进类型 + 业务id 查询跟进记录
     */
    @GetMapping("/listByTypeAndBusinessId")
    public TableDataInfo listByTypeAndBusinessId(TcFollow tcFollow)
    {
        Assert.notNull(tcFollow.getBusinessId(), "业务id不能为空");
        Assert.notNull(tcFollow.getFollowType(), "跟进类型不能为空");
        startPage();
        List<TcFollow> tcFollows = tcFollowService.selectTcFollowListByTypeAndBusinessId(tcFollow);
        return getDataTable(tcFollows);
        //return success(tcFollowService.selectTcFollowListByTypeAndBusinessId(tcFollow));
        //return SpringUtils.getAopProxy(this).list(tcFollow);
    }


    /**
     * 获取数据范围内国家下拉
     */
    @GetMapping("/countries")
    public AjaxResult getCountriesByCurUser() {
        return success(tcFollowService.selectCountriesByCurUser());
    }
}
