package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.MessageUtils;
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.TcAccount;
import com.ruoyi.project.business.domain.TcAccountForRelated;
import com.ruoyi.project.business.domain.vo.account.CancelMergeReq;
import com.ruoyi.project.business.domain.vo.selectParam.AccountParam;
import com.ruoyi.project.business.service.ITcAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 客户管理Controller
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/business/account")
public class TcAccountController extends BaseController {
    @Autowired
    private ITcAccountService tcAccountService;

    /**
     * 查询客户管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AccountParam tcAccount) {
        startPage();
        List<TcAccount> list = tcAccountService.selectTcAccountList(tcAccount);
        return getDataTable(list);
    }

    /**
     * 查询客户管理列表(不分页)
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(AccountParam tcAccount)
    {
        List<TcAccount> list = tcAccountService.selectTcAccountList(tcAccount);
        return getDataTable(list);
    }

    /**
     * 远程查询客户列表(不分页)
     */
    @GetMapping("/listRemote/{query}")
    public AjaxResult listRemote(@PathVariable("query") String query)
    {
        return success(tcAccountService.listRemote(query));
    }
    @GetMapping("/listRemote")
    public TableDataInfo listRemotePage(AccountParam tcAccount) {
        startPage();
        List<TcAccount> list = tcAccountService.listRemote(tcAccount.getAccountNo());
        return getDataTable(list);
    }


    /**
     * 查询关联客户
     */
    @GetMapping("/accountlist")
    public TableDataInfo selectAccountListByLoginId() {
        List<TcAccountForRelated> list = tcAccountService.selectAccountListByLoginId();
        return getDataTable(list);
    }
    /**
     * 查询关联客户
     */
    @GetMapping("/accountData")
    public TableDataInfo selectAccountListByAccountId(TcAccount tcAccount) {
        List<TcAccountForRelated> list = tcAccountService.selectAccountListByAccountId(tcAccount);
        return getDataTable(list);
    }
    /**
     * 查询关联客户
     */
    @GetMapping("/accountDataQuery")
    public TableDataInfo selectAccountListByQuery(TcAccount tcAccount) {
        List<TcAccountForRelated> list = tcAccountService.selectAccountListByQuery(tcAccount);
        return getDataTable(list);
    }
    /**
     * 查询关联客户
     */
    @GetMapping("/accountDataQueryPage")
    public TableDataInfo selectAccountListByQueryPage(TcAccount tcAccount) {
        startPage();
        List<TcAccountForRelated> list = tcAccountService.selectAccountListByQuery(tcAccount);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AccountParam tcAccount) {
        List<TcAccount> list = tcAccountService.selectTcAccountListExport(tcAccount);
        ExcelUtil<TcAccount> util = new ExcelUtil<TcAccount>(TcAccount.class);
        util.exportExcel(response, list, MessageUtils.message("account.exportSheetName"));
    }

    /**
     * 获取客户管理详细信息
     */
    @GetMapping(value = "/{accountId}")
    public AjaxResult getInfo(@PathVariable("accountId") Long accountId) {
        return success(tcAccountService.selectTcAccountByAccountId(accountId));
    }

    /**
     * 新增客户管理
     */
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcAccount tcAccount) {
        return toAjax(tcAccountService.insertTcAccount(tcAccount));
    }

    /**
     * 修改客户管理
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "account_id", tableName = "tc_account")
    public AjaxResult edit(@RequestBody TcAccount tcAccount) {
        return toAjax(tcAccountService.updateTcAccount(tcAccount));
    }

    /**
     * 删除客户管理
     */
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public AjaxResult remove(@PathVariable Long[] accountIds) {
        return toAjax(tcAccountService.deleteTcAccountByAccountIds(accountIds));
    }

    /**
     * 客户合并
     */
    @Log(title = "客户合并", businessType = BusinessType.INSERT)
    @PostMapping("/merge")
    public AjaxResult merge(@RequestBody List<TcAccount> tcAccountList) {
        return toAjax(tcAccountService.merge(tcAccountList));
    }
    /**
     * 客户取消合并
     */
    @Log(title = "客户取消合并", businessType = BusinessType.INSERT)
    @PostMapping("/cancelMerge")
    public AjaxResult cancelMerge(@RequestBody CancelMergeReq req) {
        return toAjax(tcAccountService.cancelMerge(req));
    }

    /**
     * 查询当前客户的可见区域下拉
     */
    @GetMapping("/areas")
    public AjaxResult getAreasByCurUser() {
        return success(tcAccountService.selectAreasByCurUser());
    }
    /**
     * 查询当前客户的可见国家下拉
     */
    @GetMapping("/countries")
    public AjaxResult getCountriesByCurUser() {
        return success(tcAccountService.selectCountriesByCurUser());
    }

    /**
     * changeOwner 变更负责人
     */
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeOwner")
    @DataUpdateCheck(key = "account_id", tableName = "tc_account")
    public AjaxResult changeOwner(@RequestBody TcAccount tcAccount) {
        return toAjax(tcAccountService.changeOwner(tcAccount));
    }
    @GetMapping("/getAccountByAgent/{ownerAgent}")
    public AjaxResult getAccountByAgent(@PathVariable("ownerAgent") Long ownerAgent) {
        return success(tcAccountService.getAccountByAgent(ownerAgent));
    }

}
