package com.fs.qw;

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.ServletUtils;
import com.fs.common.utils.TimeUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.qw.domain.QwDrainageLink;
import com.fs.qw.param.QwStatisticsParam;
import com.fs.qw.service.IQwDrainageLinkService;
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-07-11
 */
@RestController
@RequestMapping("/qw/drainageLink")
public class QwDrainageLinkController extends BaseController
{
    @Autowired
    private IQwDrainageLinkService qwDrainageLinkService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询引流链接列表
     */
    @PreAuthorize("@ss.hasPermi('qw:drainageLink:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwDrainageLink qwDrainageLink)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        qwDrainageLink.setCompanyId(companyId);
        List<QwDrainageLink> list = qwDrainageLinkService.selectQwDrainageLinkList(qwDrainageLink);
        return getDataTable(list);
    }

    /**
     * 导出引流链接列表
     */
    @PreAuthorize("@ss.hasPermi('qw:drainageLink:export')")
    @Log(title = "引流链接", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwDrainageLink qwDrainageLink)
    {
        List<QwDrainageLink> list = qwDrainageLinkService.selectQwDrainageLinkList(qwDrainageLink);
        ExcelUtil<QwDrainageLink> util = new ExcelUtil<QwDrainageLink>(QwDrainageLink.class);
        return util.exportExcel(list, "引流链接数据");
    }

    /**
     * 获取引流链接详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:drainageLink:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwDrainageLinkService.selectQwDrainageLinkById(id));
    }

    /**
     * 新增引流链接
     */
    @PreAuthorize("@ss.hasPermi('qw:drainageLink:add')")
    @Log(title = "引流链接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwDrainageLink qwDrainageLink)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long companyId = loginUser.getCompany().getCompanyId();
        qwDrainageLink.setCompanyId(companyId);
        qwDrainageLink.setCreateBy(loginUser.getUser().getNickName());
        return toAjax(qwDrainageLinkService.insertQwDrainageLink(qwDrainageLink));
    }

    /**
     * 修改引流链接
     */
    @PreAuthorize("@ss.hasPermi('qw:drainageLink:edit')")
    @Log(title = "引流链接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwDrainageLink qwDrainageLink)
    {
        return toAjax(qwDrainageLinkService.updateQwDrainageLink(qwDrainageLink));
    }

    /**
     * 删除引流链接
     */
    @PreAuthorize("@ss.hasPermi('qw:drainageLink:remove')")
    @Log(title = "引流链接", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwDrainageLinkService.deleteQwDrainageLinkByIds(ids));
    }

    @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.getWayId());
        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 = qwDrainageLinkService.selectQwDrainageLinkStatisticsCounts(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> clickNum = jsonObjectList.stream().map(jsonObject -> jsonObject.getInteger("clickNum")).collect(Collectors.toList());
        return R.ok().put("list",jsonObjectList).put("dates",dates).put("addNum",addNum).put("clickNum",clickNum);

    }


}
