package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.annotation.DataUpdateCheck;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TcLead;
import com.ruoyi.project.business.domain.TcLeadForRelated;
import com.ruoyi.project.business.domain.vo.lead.*;
import com.ruoyi.project.business.service.ITcLeadService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 线索管理Controller
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/lead")
public class TcLeadController extends BaseController {
    private final ITcLeadService tcLeadService;


    /**
     * 查询线索管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LeadListReq tcLead) {
        PageUtils.startPage();
        List<LeadRes> list = tcLeadService.selectTcLeadList(tcLead);
        return getDataTable(list);
    }

    /**
     * 查询线索管理列表不分页
     */
    @GetMapping("/allList")
    public AjaxResult allList(LeadListReq tcLead) {
        return AjaxResult.success(tcLeadService.selectTcLeadList(tcLead));
    }

    /**
     * 返回当前用户全部未转化客户的线索
     */
    @GetMapping("/listByCurUser")
    public TableDataInfo listByCurUser(LeadForAccountReq forAccountReq) {
        startPage();
        List<TcLead> tcLeads = tcLeadService.listByCurUser(forAccountReq);
        return getDataTable(tcLeads);
    }

    /**
     * 查询关联线索
     */
    @GetMapping("/leadlist")
    public TableDataInfo selectLeadListByLoginId() {
        List<TcLeadForRelated> list = tcLeadService.selectLeadListByLoginId();
        return getDataTable(list);
    }

    /**
     * 查询关联线索
     */
    @GetMapping("/leadData")
    public TableDataInfo selectLeadListByLeadId(LeadRelatedQueryReq tcLead) {
        List<TcLeadForRelated> list = tcLeadService.selectLeadListByLeadId(tcLead);
        return getDataTable(list);
    }

    /**
     * 查询关联线索
     */
    @GetMapping("/leadDataQuery")
    public TableDataInfo selectLeadListByQuery(LeadRelatedQueryReq tcLead) {
        List<TcLeadForRelated> list = tcLeadService.selectLeadListByQuery(tcLead);
        return getDataTable(list);
    }

    /**
     * 查询关联线索
     */
    @GetMapping("/leadDataQueryPage")
    public TableDataInfo selectLeadListByQueryPage(LeadRelatedQueryReq tcLead) {
        startPage();
        List<TcLeadForRelated> list = tcLeadService.selectLeadListByQuery(tcLead);
        return getDataTable(list);
    }


    /**
     * 查询客户关联线索
     */
    @GetMapping("/listByAccount")
    public TableDataInfo selectLeadListByAccount(LeadRelatedQueryReq tcLead) {
        startPage();
        List<TcLeadForRelated> list = tcLeadService.selectLeadListByAccount(tcLead);
        return getDataTable(list);
    }


    /**
     * 导出线索管理列表
     */
    @Log(title = "线索管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeadListReq tcLead) {
        List<LeadRes> list = tcLeadService.selectTcLeadListExport(tcLead);
        ExcelUtil<LeadRes> util = new ExcelUtil<>(LeadRes.class);
        util.exportExcel(response, list, MessageUtils.message("lead.exportSheetName"));
    }

    /**
     * 获取线索管理详细信息
     */
    @GetMapping(value = "/{leadId}")
    public AjaxResult getInfo(@PathVariable("leadId") Long leadId) {
        return success(tcLeadService.selectTcLeadByLeadId(leadId));
    }

    /**
     * 新增线索管理
     */
    @Log(title = "线索管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeadFormReq leadFormReq) {
        return toAjax(tcLeadService.insertTcLead(leadFormReq));
    }

    /**
     * 修改线索管理
     */
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "lead_id", tableName = "tc_lead")
    public AjaxResult edit(@RequestBody LeadFormReq tcLead) {
        return toAjax(tcLeadService.updateTcLead(tcLead));
    }

    /**
     * 删除线索管理
     */
    @Log(title = "线索管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{leadIds}")
    public AjaxResult remove(@PathVariable Long[] leadIds) {
        //return toAjax(tcLeadService.deleteTcLeadByLeadIds(leadIds));
        return toAjax(tcLeadService.removeByIds(Arrays.asList(leadIds)));
    }


    /**
     * 分配
     */
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @PutMapping("/allot")
    @DataUpdateCheck(key = "lead_id", tableName = "tc_lead")
    public AjaxResult allot(@RequestBody @Valid List<AllotReq> allots) {
        return toAjax(tcLeadService.allot(allots));
    }

    /**
     * 回收
     */
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @PutMapping("/recycle")
    @DataUpdateCheck(key = "lead_id", tableName = "tc_lead")
    public AjaxResult recycle(@RequestBody RecoveryReq recovery) {
        return toAjax(tcLeadService.recovery(recovery));
    }

    /**
     * 查询当前用户拥有线索的代理商下拉
     */
    @GetMapping("/curAgent")
    public AjaxResult curAgent() {
        return AjaxResult.success(tcLeadService.curAgent());
    }
    /**
     * 查询当前用户拥有线索的代理商下拉
     */
    @GetMapping("/curCountry")
    public AjaxResult curCountry() {
        return AjaxResult.success(tcLeadService.curCountry());
    }

    /**
     * 查询当前客户的可见区域下拉
     */
    @GetMapping("/areas")
    public AjaxResult getAreasByCurUser() {
        return success(tcLeadService.selectAreasByCurUser());
    }
    /**
     * 获取数据范围内国家下拉
     */
    @GetMapping("/countries")
    public AjaxResult getCountriesByCurUser() {
        return success(tcLeadService.selectCountriesByCurUser());
    }
    
    /**
     * 转化客户
     */
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @PutMapping("/convertCust")
    @DataUpdateCheck(key = "lead_id", tableName = "tc_lead")
    public AjaxResult convert(@RequestBody @Valid ConvertReq convertReq) {
        return toAjax(tcLeadService.convertCustomer(convertReq));
    }
    
    /**
     * 静默、无效
     */
    @Log(title = "线索管理", businessType = BusinessType.UPDATE)
    @PutMapping("/silentOrInvalid")
    @DataUpdateCheck(key = "lead_id", tableName = "tc_lead")
    public AjaxResult silentOrInvalid(@RequestBody @Valid SilentOrInvalidReq silentOrInvalidReq) {
        return toAjax(tcLeadService.silentOrInvalid(silentOrInvalidReq));
    }
}
