package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.SpecialCharacterEscaper;
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.TbAgent;
import com.ruoyi.project.business.service.ITbAgentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 【代理商】Controller
 *
 * @author ruoyi
 * @date 2024-04-16
 */
@RestController
@RequestMapping("/user/agent")
public class TbAgentController extends BaseController {

    @Resource
    private ITbAgentService tbAgentService;

    /**
     * 查询【代理商】列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@Valid TbAgent tbAgent) {
        SpecialCharacterEscaper.escapeSpecialCharacters(tbAgent, TbAgent::getAgentNo, TbAgent::getAgentName, TbAgent::getParentAgentName, TbAgent::getParentSalerName, TbAgent::getPhone,TbAgent::getCity);
        startPage();
        List<TbAgent> list = tbAgentService.selectTbAgentList(tbAgent);
        return getDataTable(list);
    }

    /**
     * 查询【代理商】列表 不分页
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(@Valid TbAgent tbAgent) {
        List<TbAgent> list = tbAgentService.selectTbAgentList(tbAgent);
        return getDataTable(list);
    }


    /**
     * 导出【代理商】列表
     */
    @Log(title = "【代理商】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAgent tbAgent) {
        List<TbAgent> list = tbAgentService.selectTbAgentList(tbAgent);
        ExcelUtil<TbAgent> util = new ExcelUtil<>(TbAgent.class);
        if (!SecurityUtils.isSuperOrSystemAdmin() && !SecurityUtils.isVp()) {
            util.hideColumn("discount");
        }
        util.exportExcel(response, list, MessageUtils.message("agent.export.sheetName"));
    }

    /**
     * 获取【代理商】详细信息
     */
    @GetMapping(value = "/{agentId}")
    public AjaxResult getInfo(@PathVariable("agentId") Long agentId) {
        return success(tbAgentService.getInfo(agentId));
    }

    /**
     * 新增
     */
    @Log(title = "【新增代理商】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAgent tbAgent) {
        return toAjax(tbAgentService.insertTbAgent(tbAgent));
    }

    /**
     * 修改【代理商】
     */
    @Log(title = "【修改代理商】", businessType = BusinessType.UPDATE)
    @PutMapping
    @DataUpdateCheck(key = "agent_id", tableName = "tb_agent")
    public AjaxResult edit(@RequestBody TbAgent tbAgent) {
        return toAjax(tbAgentService.updateTbAgent(tbAgent));
    }

    /**
     * 删除【代理商】
     */
    @Log(title = "【删除代理商】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{agentIds}")
    //@DataUpdateCheck(key = "agent_id", tableName = "tb_agent")
    public AjaxResult remove(@PathVariable Long[] agentIds) {
        return toAjax(tbAgentService.deleteTbAgentByAgentIds(agentIds));
    }


    /**
     * 根据区域id获取上级销售： 所属区域的销售经理角色的user
     */
    @GetMapping("/getSalesByAreaId/{areaId}")
    public AjaxResult getSalesByAreaId(@PathVariable Long areaId) {
        return success(tbAgentService.selectSaleByAreaId(areaId));
    }


    /**
     * 根据区域id获取上级代理： 所属区域的一级代理商
     */
    @GetMapping("/getAgentByAreaId")
    public AjaxResult getAgentByAreaId(Long areaId,
                                       @RequestParam(required = false) Long agentId) {
        return success(tbAgentService.selectAgentByAreaId(areaId, agentId));
    }

    /**
     * 修改代理商状态
     */
    @Log(title = "【修改代理商状态】", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    @DataUpdateCheck(key = "agent_id", tableName = "tb_agent")
    public AjaxResult changeStatus(@RequestBody TbAgent agent) {
        //tbAgentService.checkUserAllowed(agent);

        agent.setUpdateBy(SecurityUtils.getUserId().toString());
        agent.setUpdateTime(new Date());
        return toAjax(tbAgentService.changeStatus(agent));

    }

    /**
     * 获取客户全部代理商
     */
    @GetMapping("/selectForAccount")
    public AjaxResult selectForAccount() {
        return success(tbAgentService.selectForAccount());
    }

    /**
     * 获取项目全部代理商
     */
    @GetMapping("/selectForProject")
    public AjaxResult selectForProject() {
        return success(tbAgentService.selectForProject());
    }

    /**
     * 获取项目全部代理商
     */
    @GetMapping("/selectForYear")
    public AjaxResult selectForYear(String yearParam) {
        return success(tbAgentService.selectForYear(yearParam));
    }

    /**
     * 获取指定层级及后代代理商
     *
     * @param agentId       代理商主键
     * @param isJustSubs    是否只查询子区域（不包含孙子区域） 不传默认为false
     * @param isIncludeSelf 是否包含自己  不传默认为false
     */
    @GetMapping("/getChildAreaList")
    public AjaxResult getChildAreaList(Long agentId, Boolean isJustSubs, Boolean isIncludeSelf) {
        return success(tbAgentService.selectChildAreaList(agentId, Boolean.TRUE.equals(isJustSubs), Boolean.TRUE.equals(isIncludeSelf)));
    }

    /**
     * 获取登录用户下属所有代理商
     */
    @GetMapping("/selectByOwner/{ownerId}")
    public AjaxResult selectByOwner(@PathVariable Long ownerId) {
        return success(tbAgentService.selectAgentListByOwner(ownerId));
    }
}
