package com.flossom.system.controller;

import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.flossom.common.core.domain.entity.SiteInfo;
import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.utils.poi.ExcelUtil;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.common.core.web.page.TableDataInfo;
import com.flossom.common.log.annotation.Log;
import com.flossom.common.log.enums.BusinessType;
import com.flossom.common.security.annotation.RequiresPermissions;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.flossom.system.service.ISiteInfoService;

/**
 * 站点管理Controller
 *
 * @author flossom
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/siteInfo")
public class SiteInfoController extends BaseController
{
    @Autowired
    private ISiteInfoService siteInfoService;

    /**
     * 查询站点管理列表
     */
    @RequiresPermissions("system:siteInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(SiteInfo siteInfo)
    {
        startPage();
        List<SiteInfo> list = siteInfoService.selectSiteInfoList(siteInfo);
        for(SiteInfo site : list) {
            if (StringUtils.isNotEmpty(site.getPushTimer())) {
                List<String> timeList = Lists.newArrayList();
                String[] pushTimer = site.getPushTimer().split(",");
                if(pushTimer.length > 0) {
                    for (int i=0;i< pushTimer.length;i++) {
                        String time = pushTimer[i];
                        if ("1".equals(time)) {
                            timeList.add("7");
                        } else {
                            int tureDate = Integer.valueOf(time) - 1 ;
                            timeList.add(String.valueOf(tureDate));
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(timeList)) {
                    String collect = timeList.stream().map(String::valueOf).collect(Collectors.joining(","));
                    site.setPushTimer(collect);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出站点管理列表
     */
    @RequiresPermissions("system:siteInfo:export")
    @Log(title = "站点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SiteInfo siteInfo)
    {
        List<SiteInfo> list = siteInfoService.selectSiteInfoList(siteInfo);
        ExcelUtil<SiteInfo> util = new ExcelUtil<SiteInfo>(SiteInfo.class);
        util.exportExcel(response, list, "站点管理数据");
    }

    /**
     * 获取站点管理详细信息
     */
    @RequiresPermissions("system:siteInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(siteInfoService.selectSiteInfoById(id));
    }

    /**
     * 新增站点管理
     */
    @RequiresPermissions("system:siteInfo:add")
    @Log(title = "站点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SiteInfo siteInfo)
    {
        return toAjax(siteInfoService.insertSiteInfo(siteInfo));
    }

    /**
     * 修改站点管理
     */
    @RequiresPermissions("system:siteInfo:edit")
    @Log(title = "站点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiteInfo siteInfo)
    {
        return toAjax(siteInfoService.updateSiteInfo(siteInfo));
    }

    /**
     * 删除站点管理
     */
    @RequiresPermissions("system:siteInfo:remove")
    @Log(title = "站点管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(siteInfoService.deleteSiteInfoByIds(ids));
    }
}
