package com.fs.company.controller.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.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.his.vo.OptionsVO;
import com.fs.qw.domain.QwContactWay;
import com.fs.qw.param.QwStatisticsParam;
import com.fs.qw.service.IQwContactWayService;
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-07-05
 */
@RestController
@RequestMapping("/qw/contactWay")
public class QwContactWayController extends BaseController
{
    @Autowired
    private IQwContactWayService qwContactWayService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IQwInformationService qwInformationService;

    /**
     * 查询企微活码列表
     */

    @GetMapping("/list")
    public TableDataInfo list(QwContactWay qwContactWay)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwContactWay.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwContactWay> list = qwContactWayService.selectQwContactWayList(qwContactWay);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('qw:contactWay:sync')")
    @GetMapping("/sync/{corpId}")
    public R sync(@PathVariable("corpId")String corpId)
    {

        return qwContactWayService.selectQwContactWaysync(corpId);
    }
    /**
     * 导出企微活码列表
     */
    @PreAuthorize("@ss.hasPermi('qw:contactWay:export')")
    @Log(title = "企微活码", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwContactWay qwContactWay)
    {

        List<QwContactWay> list = qwContactWayService.selectQwContactWayList(qwContactWay);
        ExcelUtil<QwContactWay> util = new ExcelUtil<QwContactWay>(QwContactWay.class);
        return util.exportExcel(list, "企微活码数据");
    }

    /**
     * 获取企微活码详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:contactWay:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qwContactWayService.selectQwContactWayById(id));
    }

    /**
     * 新增企微活码
     */
    @PreAuthorize("@ss.hasPermi('qw:contactWay:add')")
    @Log(title = "企微活码", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody QwContactWay qwContactWay)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwContactWay.setCompanyId(loginUser.getCompany().getCompanyId());
        return qwContactWayService.insertQwContactWay(qwContactWay);
    }

    /**
     * 修改企微活码
     */
    @PreAuthorize("@ss.hasPermi('qw:contactWay:edit')")
    @Log(title = "企微活码", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody QwContactWay qwContactWay)
    {
        return qwContactWayService.updateQwContactWay(qwContactWay);
    }

    /**
     * 删除企微活码
     */
    @PreAuthorize("@ss.hasPermi('qw:contactWay:remove')")
    @Log(title = "企微活码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids)
    {

        return qwContactWayService.deleteQwContactWayByIds(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 = qwContactWayService.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);

    }


    @GetMapping("/informationList")
    public R InformationList()
    {
        List<OptionsVO> list = qwInformationService.selectQwInformationListOptionsVO();
        return R.ok().put("data",list);
    }
}
