package com.fs.qw.controller;

import com.alibaba.fastjson.JSONObject;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.TimeUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.qw.domain.QwInformation;
import com.fs.qw.param.QwStatisticsParam;
import com.fs.qw.service.IQwInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 信息流管理Controller
 *
 * @author fs
 * @date 2024-11-11
 */
@RestController
@RequestMapping("/qw/qwInformation")
public class QwInformationController extends BaseController
{
    @Autowired
    private IQwInformationService qwInformationService;

    /**
     * 查询信息流管理列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwInformation:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwInformation qwInformation)
    {
        startPage();
        List<QwInformation> list = qwInformationService.selectQwInformationList(qwInformation);
        return getDataTable(list);
    }
    @GetMapping("/statistics")
    public R statistics(QwStatisticsParam param)
    {
//        List<QwWayStatisticsListVO> list= qwContactWayService.QwWayStatisticsListVO(param);
        TimeUtils.TimeEntity timeEntity= TimeUtils.parseTime(param.getType().toString(),param.getStartTime(),param.getEndTime());
        timeEntity.setCompanyId(param.getId());
        Integer cycleNum = timeEntity.getCycleNum();
        Integer beginTime = timeEntity.getBeginTime();
        List<Integer> timeList = new ArrayList<>();
        for (int i = 1; i <= cycleNum; i++) {
            timeList.add(beginTime);
            beginTime = TimeUtils.formatTime(beginTime);
        }
        List<JSONObject> jsonObjectList = qwInformationService.selectQwWayStatisticsCounts(timeEntity.toMap());
        List<String> dates = jsonObjectList.stream().map(jsonObject -> jsonObject.getString("type")).collect(Collectors.toList());
        List<Integer> addNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("addNum")).collect(Collectors.toList());
        List<Integer> deleteNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("deleteNum")).collect(Collectors.toList());
        List<Integer> num = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("num")).collect(Collectors.toList());
        return R.ok().put("list",jsonObjectList).put("dates",dates).put("addNum",addNum).put("deleteNum",deleteNum).put("num",num);

    }
    /**
     * 导出信息流管理列表
     */
    @PreAuthorize("@ss.hasPermi('qw:qwInformation:export')")
    @Log(title = "信息流管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwInformation qwInformation)
    {
        List<QwInformation> list = qwInformationService.selectQwInformationList(qwInformation);
        ExcelUtil<QwInformation> util = new ExcelUtil<QwInformation>(QwInformation.class);
        return util.exportExcel(list, "信息流管理数据");
    }

    /**
     * 获取信息流管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:qwInformation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwInformationService.selectQwInformationById(id));
    }

    /**
     * 新增信息流管理
     */
    @PreAuthorize("@ss.hasPermi('qw:qwInformation:add')")
    @Log(title = "信息流管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwInformation qwInformation)
    {
        return toAjax(qwInformationService.insertQwInformation(qwInformation));
    }

    /**
     * 修改信息流管理
     */
    @PreAuthorize("@ss.hasPermi('qw:qwInformation:edit')")
    @Log(title = "信息流管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwInformation qwInformation)
    {
        qwInformation.setAddNum(null);
        qwInformation.setNum(null);
        qwInformation.setDeleteNum(null);
        return toAjax(qwInformationService.updateQwInformation(qwInformation));
    }

    /**
     * 删除信息流管理
     */
    @PreAuthorize("@ss.hasPermi('qw:qwInformation:remove')")
    @Log(title = "信息流管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwInformationService.deleteQwInformationByIds(ids));
    }
}
