package com.fdkankan.platform.agent.controller.manager;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.platform.agent.entity.Agent;
import com.fdkankan.platform.agent.entity.AgentFramework;
import com.fdkankan.platform.agent.request.RequestAgent;
import com.fdkankan.platform.agent.request.RequestAgentFramework;
import com.fdkankan.platform.agent.request.RequestAgentNotice;
import com.fdkankan.platform.agent.service.IAgentFrameworkService;
import com.fdkankan.platform.agent.service.IAgentNoticeService;
import com.fdkankan.platform.agent.service.IAgentService;
import com.fdkankan.platform.agent.util.AgentFrameworkTree;
import com.fdkankan.platform.agent.util.AgentFrameworkTreeUtil;
import com.fdkankan.platform.agent.vo.AgentDetailTotalAmount;
import com.fdkankan.platform.agent.vo.AgentTotalAmountVo;
import com.fdkankan.platform.agent.vo.AgentVo;
import com.fdkankan.platform.agent.vo.ResponseAgentCamera;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 后台管理-代理商管理
 * </p>
 *
 */
@RestController
@RequestMapping("/service/platform/manager/agent")
public class ManagerAgentController {

    @Autowired
    IAgentService agentService;
    @Autowired
    IAgentNoticeService agentNoticeService;
    @Autowired
    IAgentFrameworkService agentFrameworkService;

    /**
     * 获取代理商列表
     */
    @PostMapping(value = "/list")
    public Page<AgentVo> list(@RequestBody RequestAgent param){
        return  agentService.getPageList(param);
    }

    /**
     * 获取代理商详情
     */
    @PostMapping(value = "/detail")
    public AgentVo detail(@RequestBody RequestAgent param){
        Agent agent = agentService.getAgentById(param.getAgentId());
        AgentVo agentVo = new AgentVo();
        BeanUtils.copyProperties(agent, agentVo);
        return agentVo;
    }

    /**
     * 新增或修改代理商信息
     */
    @PostMapping(value = "/save")
    public String save(@RequestBody RequestAgent param) throws Exception {
        return agentService.saveAgent(param);
    }

    /**
     * 删除代理商
     */
    @PostMapping(value = "/delete")
    public String delete(@RequestBody RequestAgent param) throws Exception {
        return agentService.deleteAgent(param.getId());
    }

    /**
     * 更新状态
     */
    @PostMapping(value = "/updateStatus")
    public String updateStatus(@RequestBody RequestAgent param) throws Exception {
        return agentService.updateAgentStatus(param.getId(), param.getStatus());
    }

    /**
     * 上传设备表格
     * @return
     */
    @PostMapping(value = "/uploadFile")
    public String uploadFile(MultipartFile file) throws Exception {
        return agentService.uploadFile(file);
    }


    /**
     * 代理查询列表
     */
    @PostMapping(value = {"/searchList"})
    public Page<AgentVo> searchList(@RequestBody RequestAgent param) throws Exception {
        return agentService.searchAgentList(param);
    }

    /**
     * 代理查询列表统计
     */
    @PostMapping(value = "/searchListStatistics")
    public AgentTotalAmountVo searchListStatistics(@RequestBody RequestAgent param) throws Exception {
        return agentService.searchAgentListStatistics(param);
    }

    /**
     * 代理查询列表详情
     */
    @PostMapping(value = "/searchListDetail")
    public Page<ResponseAgentCamera> searchListDetail(@RequestBody RequestAgent param) throws Exception {
        return agentService.searchAgentListDetail(param);
    }

    /**
     * 代理查询列表详情统计
     */
    @PostMapping(value = "/searchListDetailStatistics")
    public AgentDetailTotalAmount searchListDetailStatistics(@RequestBody RequestAgent param) throws Exception {
        return agentService.searchAgentListDetailStatistics(param);
    }

    /**
     * 发布公告
     */
    @PostMapping(value = "/publishNotice")
    public String publishNotice(@RequestBody RequestAgentNotice param) throws Exception {
        return agentNoticeService.saveNotice(param.getTitle(), param.getContent());
    }

    /**
     * 新增经销商架构管理
     */
    @PostMapping(value = "/addAgentFramework")
    public String addAgentFramework(@RequestBody RequestAgentFramework param){
       return  agentFrameworkService.saveByParam(param);
    }

    /**
     * 修改经销商架构管理
     */
    @PostMapping(value = "/updateAgentFramework")
    public String updateAgentFramework(@RequestBody RequestAgentFramework param){
        return  agentFrameworkService.updateByParam(param);
    }

    /**
     * 删除经销商架构管理
     */
    @PostMapping(value = "/deleteAgentFramework")
    public String deleteAgentFramework(@RequestBody RequestAgentFramework param){
        return agentFrameworkService.deleteByParam(param);
    }

    /**
     * 查询经销商架构树结构
     * @return
     */
    @PostMapping(value = "/agentFrameworkTree")
    public List<AgentFrameworkTree> agentFrameworkTree(){
        List<AgentFramework> list = agentFrameworkService.list();
        return new AgentFrameworkTreeUtil(list).buildTree();
    }

    /**
     * 查询经销商架构所有
     */
    @PostMapping(value = "/agentFrameworkAll")
    public List<AgentFramework> agentFrameworkAll(){
        return agentFrameworkService.list();
    }
}

