package com.ruoyi.ibs.task.controller;

import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.Page;
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.domain.R;
import com.ruoyi.common.core.page.TableDataPageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ibs.task.domain.dto.MarketTaskDTO;
import com.ruoyi.ibs.task.domain.dto.MarketTaskListDTO;
import com.ruoyi.ibs.task.domain.dto.MarketTaskListTemplate;
import com.ruoyi.ibs.task.domain.entity.MarketTask;
import com.ruoyi.ibs.task.domain.vo.MarketTaskListVO;
import com.ruoyi.ibs.task.domain.vo.MarketTaskVO;
import com.ruoyi.ibs.task.service.MarketTaskListService;
import com.ruoyi.ibs.task.service.MarketTaskService;
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;

@Api(tags = "营销任务接口")
@RestController
@RequestMapping("/market")
public class MarketTaskController extends BaseController {

    @Resource
    private MarketTaskService marketTaskService;

    @Resource
    private MarketTaskListService marketTaskListService;

    /**
     * 首次创建营销任务+导入
     */
    @ApiOperation("异步创建营销任务")
    @Log(title = "走访-创建营销任务")
    @PostMapping(value = "/createAsync", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> createMarketTaskAsync(@RequestPart("dto") @Valid String dtoJson,
                                            @RequestPart("file") MultipartFile file) {
        MarketTaskDTO marketTaskDTO = JSON.parseObject(dtoJson, MarketTaskDTO.class);
        return R.ok(marketTaskService.createMarketTaskAsync(marketTaskDTO, file));
    }

    /**
     * 编辑营销任务——包括创建失败时重新导入
     */
    @ApiOperation("编辑营销任务")
    @Log(title = "走访-编辑营销任务")
    @PostMapping(value ="/edit", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> edit(@RequestPart("dto") String dtoJson,
                          @RequestPart(value = "file", required = false) MultipartFile file) {
        MarketTask marketTask = JSON.parseObject(dtoJson, MarketTask.class);
        return R.ok(marketTaskService.editMarketTask(marketTask, file));
    }

    @ApiOperation("轮询任务创建状态")
    @Log(title = "走访-轮询任务创建状态")
    @GetMapping("/createStatus/{id}")
    public R<String> getCreateStatus(@PathVariable String id) {
        return R.ok(marketTaskService.getCreateStatus(id));
    }

    @ApiOperation("营销名单模板")
    @Log(title = "走访-营销名单模板", businessType = BusinessType.EXPORT)
    @PostMapping("/download")
    public void download(HttpServletResponse response)
    {
        ExcelUtil<MarketTaskListTemplate> util = new ExcelUtil<>(MarketTaskListTemplate.class);
        util.exportExcel(response, null, "营销名单模板");
    }

    @ApiOperation("查询营销任务")
    @Log(title = "走访-查询营销任务")
    @GetMapping("/task")
    public TableDataPageInfo<MarketTaskVO> getMarketTasks(String marketTaskName) {
        Page<Object> page = startPage();
        List<MarketTaskVO> list = marketTaskService.getMarketTasks(marketTaskName);
        return getDataTable(list, page);
    }

    @ApiOperation("查询营销名单——管理员")
    @Log(title = "走访-查询营销名单")
    @GetMapping("/allList")
    public TableDataPageInfo<MarketTaskListVO> getAllMarketLists(MarketTaskListDTO marketTaskListDTO) {
        Page<Object> page = startPage();
        List<MarketTaskListVO> list = marketTaskListService.getAllMarketLists(marketTaskListDTO);
        return getDataTable(list, page);
    }

    @ApiOperation("查询我的营销名单")
    @Log(title = "走访-查询我的营销名单")
    @GetMapping("/list")
    public TableDataPageInfo<MarketTaskListVO> getMarketLists(MarketTaskListDTO marketTaskListDTO) {
        Page<Object> page = startPage();
        List<MarketTaskListVO> list = marketTaskListService.getMarketLists(marketTaskListDTO);
        return getDataTable(list, page);
    }

    @PostMapping("/read")
    @Log(title = "走访-设置状态为已读")
    @ApiOperation("设置状态为已读")
    public AjaxResult readAlter(@RequestParam Long id) {
        return toAjax(marketTaskListService.updateReadTime(id));
    }


}
