package com.pnkx.web.controller.blog.client;

import com.pnkx.common.core.controller.BaseController;
import com.pnkx.common.core.domain.AjaxResult;
import com.pnkx.common.core.domain.entity.SysDictData;
import com.pnkx.common.core.page.TableDataInfo;
import com.pnkx.common.utils.StringUtils;
import com.pnkx.common.utils.ip.IpUtils;
import com.pnkx.domain.po.PxRegion;
import com.pnkx.domain.po.PxSubscribe;
import com.pnkx.service.IPxRegionService;
import com.pnkx.service.IPxStatisticsService;
import com.pnkx.service.IPxSubscribeService;
import com.pnkx.system.domain.SysConfig;
import com.pnkx.system.domain.SysFile;
import com.pnkx.system.domain.SysNotice;
import com.pnkx.system.domain.vo.SysNoticeVo;
import com.pnkx.system.service.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author by PHY
 * @Classname PxCustomerController
 * @date 2021-03-24 11:44
 */
@RequestMapping("/client")
@RestController
public class PxClientController extends BaseController {

    @Resource
    private IPxSubscribeService pxSubscribeService;
    @Resource
    private IPxStatisticsService pxStatisticsService;
    @Resource
    private ISysConfigService sysConfigService;
    @Resource
    private ISysDictDataService sysDictDataService;
    @Resource
    private ISysFileService sysFileService;
    @Resource
    private ISysDictTypeService dictTypeService;
    @Resource
    private ISysNoticeService noticeService;
    @Resource
    private IPxRegionService pxRegionService;

    /**
     * 获取ip
     *
     * @param request request
     * @return ip
     */
    @GetMapping("/getIpLocation")
    public AjaxResult getIp(HttpServletRequest request) {
        String ip = IpUtils.getIpAddr(request);
        return AjaxResult.success(IpUtils.getLocation(ip));
    }

    /**
     * 添加订阅
     *
     * @param pxSubscribe pxSubscribe
     * @return 订阅结果
     */
    @PostMapping("/addSubscribe")
    public AjaxResult addSubscribe(@RequestBody PxSubscribe pxSubscribe) {
        return AjaxResult.success(pxSubscribeService.insertPxSubscribe(pxSubscribe));
    }

    /**
     * 获取随机图片
     * @param sysFile 参数
     * @return 随机图片
     */
    @GetMapping("/getGalleryPicture")
    public TableDataInfo list(SysFile sysFile, Boolean isRandom) {
        sysFile.setType("tk");
        List<SysFile> list;
        if (isRandom) {
            list = sysFileService.getRandomPicture(15);
        } else {
            startPage();
            list = sysFileService.selectSysFileList(sysFile);
        }
        return getDataTable(list);
    }

    /**
     * 获取博客信息
     *
     * @return 博客信息
     */
    @GetMapping(value = "/getBlogInfo")
    public AjaxResult getBlogInfo() {
        Map<String, Object> result = pxStatisticsService.getStatistics();
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("px_article_type");
        result.put("articleTypeNumber", sysDictDataService.selectDictDataList(sysDictData).size());
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigType("blog");
        result.put("blogConfig", sysConfigService.selectConfigList(sysConfig));
        return AjaxResult.success(result);
    }

    /**
     * 文件浏览+1
     */
    @GetMapping(value = "/file/browse/{id}")
    public AjaxResult browse(@PathVariable("id") Long id) {
        return AjaxResult.success(sysFileService.browse(id));
    }

    /**
     * 文件点赞+1
     */
    @GetMapping(value = "/file/like/{id}")
    public AjaxResult like(@PathVariable("id") Long id) {
        return AjaxResult.success(sysFileService.like(id));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/dictType/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 获取博客通知公告列表
     */
    @GetMapping("/getNoticeList")
    public AjaxResult getClientNoticeList() {
        SysNotice sysNotice = new SysNotice();
        // 博客通知类型
        sysNotice.setNoticeType("bktz");
        List<SysNoticeVo> list = noticeService.selectNoticeList(sysNotice);
        return AjaxResult.success(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @GetMapping(value = "/getNotice/{noticeId}")
    public AjaxResult getInfo(HttpServletRequest request, @PathVariable Long noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(request, noticeId));
    }

    /**
     * 获取地区信息列表
     * @param pxRegion 地区信息
     * @return 地区信息列表
     */
    @GetMapping(value = "/getRegionList")
    public AjaxResult getRegionList(PxRegion pxRegion) {
        return AjaxResult.success(pxRegionService.getRegionList(pxRegion));
    }
}
