package com.ruoyi.project.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import static com.ruoyi.common.utils.LocalUtils.setLocalAreaNameBatch;

import com.ruoyi.common.utils.poi.ExcelUtil;
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.TbAgentTarget;
import com.ruoyi.project.business.domain.vo.agent.AgentTargetReq;
import com.ruoyi.project.business.domain.vo.agent.AgentTargetRes;
import com.ruoyi.project.business.service.ITbAgentTargetService;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.common.utils.LocalUtils.setLocalAreaNameBatch;

/**
 * 代理商考核Controller
 *
 * @author ruoyi
 * @date 2024-05-16
 */
@RestController
@RequestMapping("/user/target")
@RequiredArgsConstructor
public class TbAgentTargetController extends BaseController {
    private final ITbAgentTargetService tbAgentTargetService;

    /**
     * 查询代理商考核列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AgentTargetReq agentTargetReq) {
        List<AgentTargetRes> list = tbAgentTargetService.selectTbAgentTargetList(agentTargetReq, true);
        return getDataTable(list);
    }

    /**
     * 导出代理商考核列表
     */
    @Log(title = "代理商考核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AgentTargetReq agentTargetReq) {
        List<AgentTargetRes> list = tbAgentTargetService.selectTbAgentTargetList(agentTargetReq, false);
        setLocalAreaNameBatch(list, AgentTargetRes::getAreaId, AgentTargetRes::setAreaName);
        tbAgentTargetService.convertAmount(list);
        ExcelUtil<AgentTargetRes> util = new ExcelUtil<>(AgentTargetRes.class);
        util.exportExcel(response, list, "代理商考核数据");
    }

    /**
     * 获取代理商考核详细信息
     */
    @GetMapping(value = "/{targetId}")
    public AjaxResult getInfo(@PathVariable("targetId") Long targetId) {
        return success(tbAgentTargetService.getById(targetId));
    }
    
    /**
     * 获取代理商考核详细信息
     * by 考核时间... 
     */
    @GetMapping("/getInfoByQuery")
    public AjaxResult getInfoByQuery(AgentTargetReq agentTargetReq) {
        return success(tbAgentTargetService.getInfoByQuery(agentTargetReq));
    }

    /**
     * 新增代理商考核
     */
    @Log(title = "代理商考核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAgentTarget tbAgentTarget) {
        return toAjax(tbAgentTargetService.add(tbAgentTarget));
    }

    /**
     * 修改代理商考核
     */
    @Log(title = "代理商考核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAgentTarget tbAgentTarget) {
        return toAjax(tbAgentTargetService.updateById(tbAgentTarget));
    }

    /**
     * 新增或修改代理商考核
     * 
     * @param tbAgentTarget
     * @return
     */
    @Log(title = "代理商考核", businessType = BusinessType.UPDATE)
    @PostMapping("/addOrEdit")
    public AjaxResult addOrEdit(@RequestBody TbAgentTarget tbAgentTarget) {
        return toAjax(tbAgentTargetService.saveOrUpdate(tbAgentTarget));
    }

    /**
     * 删除代理商考核
     */
    @Log(title = "代理商考核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{targetIds}")
    public AjaxResult remove(@PathVariable Long[] targetIds) {
        return toAjax(tbAgentTargetService.removeByIds(Arrays.asList(targetIds)));
    }
}
