package com.ruoyi.group.controller;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.group.domain.dto.CustGroupMemberTemplate;
import com.ruoyi.group.domain.dto.CustGroupQueryDTO;
import com.ruoyi.group.domain.entity.CustGroup;
import com.ruoyi.group.domain.vo.CustGroupVO;
import com.ruoyi.group.service.ICustGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 客群管理Controller
 *
 * @author ruoyi
 */
@Api(tags = "客群管理接口")
@RestController
@RequestMapping("/group/cust")
public class CustGroupController extends BaseController {

    @Resource
    private ICustGroupService custGroupService;

    /**
     * 查询客群列表
     */
    @ApiOperation("查询客群列表")
    @Log(title = "客群管理-查询客群列表")
    @GetMapping("/list")
    public TableDataInfo listCustGroup(CustGroupQueryDTO dto) {
        startPage();
        List<CustGroupVO> list = custGroupService.listCustGroup(dto);
        return getDataTable(list);
    }

    /**
     * 根据ID查询客群详情
     */
    @ApiOperation("根据ID查询客群详情")
    @Log(title = "客群管理-查询客群详情")
    @GetMapping("/{id}")
    public AjaxResult getCustGroup(@PathVariable Long id) {
        CustGroupVO custGroup = custGroupService.getCustGroup(id);
        return AjaxResult.success(custGroup);
    }

    /**
     * 异步创建客群（模板导入）
     * gridType、regionGridIds、drawGridIds 包含在 dto 中
     */
    @ApiOperation("异步创建客群（模板导入）")
    @Log(title = "客群管理-异步创建客群", businessType = BusinessType.INSERT)
    @PostMapping(value = "/createByTemplate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult createCustGroupByTemplate(
            @RequestPart("dto") @Valid String dtoJson,
            @RequestPart("file") MultipartFile file) {
        CustGroup custGroup = JSON.parseObject(dtoJson, CustGroup.class);
        return AjaxResult.success("操作成功", custGroupService.createCustGroupByTemplate(custGroup, file));
    }

    /**
     * 更新客群（模板导入）
     * gridType、regionGridIds、drawGridIds 包含在 dto 中
     * file 参数可选：不传文件则只更新客群信息，传文件则追加客户
     */
    @ApiOperation("更新客群（模板导入）")
    @Log(title = "客群管理-更新客群（模板导入）", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/updateByTemplate", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public AjaxResult updateCustGroupByTemplate(
            @RequestPart("dto") @Valid String dtoJson,
            @RequestPart(value = "file", required = false) MultipartFile file) {
        CustGroup custGroup = JSON.parseObject(dtoJson, CustGroup.class);
        return AjaxResult.success(custGroupService.updateCustGroupByTemplate(custGroup, file));
    }

    /**
     * 轮询客群创建状态
     */
    @ApiOperation("轮询客群创建状态")
    @Log(title = "客群管理-轮询客群创建状态")
    @GetMapping("/createStatus/{id}")
    public AjaxResult getCreateStatus(@PathVariable Long id) {
        String status = custGroupService.getCreateStatus(id);
        return AjaxResult.success("操作成功", status);
    }

    /**
     * 客户信息模板下载
     */
    @ApiOperation("客户信息模板")
    @Log(title = "客群管理-客户信息模板", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void download(HttpServletResponse response) {
        ExcelUtil<CustGroupMemberTemplate> util = new ExcelUtil<>(CustGroupMemberTemplate.class);
        util.exportExcel(response, null, "客户信息模板");
    }

    /**
     * 删除客群
     */
    @ApiOperation("删除客群")
    @Log(title = "客群管理-删除客群", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult deleteCustGroup(@RequestBody List<Long> idList) {
        String result = custGroupService.deleteCustGroup(idList);
        return AjaxResult.success(result);
    }

    /**
     * 获取所有已有的客群标签列表
     */
    @ApiOperation("获取所有客群标签")
    @Log(title = "客群管理-获取客群标签")
    @GetMapping("/tags")
    public AjaxResult getAllGroupTags() {
        List<String> tags = custGroupService.getAllGroupTags();
        return AjaxResult.success(tags);
    }

}
