package com.playlet.web.controller.system;

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

import cn.hutool.core.collection.CollectionUtil;
import com.playlet.common.utils.StringUtils;
import com.playlet.system.domain.PlayletPublicAccount;
import com.playlet.system.domain.PublicDetailComment;
import com.playlet.system.service.IPlayletItemService;
import com.playlet.system.service.IPlayletPublicAccountService;
import com.playlet.system.service.IPublicDetailShareService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.playlet.common.annotation.Log;
import com.playlet.common.enums.BusinessType;
import com.playlet.system.domain.PlayletPublicDetail;
import com.playlet.system.service.IPlayletPublicDetailService;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.AjaxResult;
import com.playlet.common.utils.poi.ExcelUtil;
import com.playlet.common.core.page.TableDataInfo;

/**
 * 公众号文章详情Controller
 *
 * @author ruoyi
 * @date 2024-04-07
 */
@Controller
@RequestMapping("/system/playlet/detail")
public class PlayletPublicDetailController extends BaseController
{
    private String prefix = "system/playlet/detail";

    @Autowired
    private IPlayletPublicDetailService playletPublicDetailService;

    @Autowired
    private IPlayletPublicAccountService playletPublicAccountService;

    @Autowired
    private IPlayletItemService playletItemService;

    @Autowired
    private IPublicDetailShareService publicDetailShareService;


    @GetMapping("/wxQrcode/{id}")
    public String wxQrcode(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/wxQrcode";
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(PlayletPublicDetail playletPublicDetail)
    {
        // 查询文章
        PlayletPublicDetail model = playletPublicDetailService.getById(playletPublicDetail.getId());
        model.setTopStatus(playletPublicDetail.getTopStatus());
        playletPublicDetailService.updateById(model);
        return AjaxResult.success();
    }

    @PostMapping("/checkExist")
    @ResponseBody
    public AjaxResult checkExist(@RequestBody Long id) {
        PlayletPublicDetail detail = playletPublicDetailService.getById(id);
        if(detail!=null){
            String url;
            if("01".equals(detail.getType())){
                 url = "https://www.mungerfans.com/article/info/" + id;
            }else {
                 url = "https://www.mungerfans.com/article/file/" + id;
            }
            return AjaxResult.success("操作成功", url);
        }else {
            return AjaxResult.error();
        }
    }

    @RequiresPermissions("playlet:detail:view")
    @GetMapping()
    public String detail()
    {
        return prefix + "/detail";
    }

    /**
     * 查询公众号文章详情列表
     */
    @RequiresPermissions("playlet:detail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlayletPublicDetail playletPublicDetail)
    {
        if(!this.getSysUser().isAdmin()){
            List<PlayletPublicAccount> accounts = playletPublicAccountService.lambdaQuery().select(PlayletPublicAccount::getId)
                    .eq(PlayletPublicAccount::getManagerId, this.getUserId())
                    .list();
            if(CollectionUtil.isEmpty(accounts)){
                return getDataTable(new ArrayList<>());
            }
            List<Long> ids = accounts.stream().map(PlayletPublicAccount::getId).collect(Collectors.toList());
            playletPublicDetail.setPublicIds(ids);
        }
        startPage();
        List<PlayletPublicDetail> list = playletPublicDetailService.selectPlayletPublicDetailList(playletPublicDetail);
        list.forEach(model->{
            PlayletPublicAccount account = playletPublicAccountService.getById(model.getPublicId());
            if(account != null){
                model.setPublicName(account.getName());
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出公众号文章详情列表
     */
    @RequiresPermissions("playlet:detail:export")
    @Log(title = "公众号文章详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlayletPublicDetail playletPublicDetail)
    {
        List<PlayletPublicDetail> list = playletPublicDetailService.selectPlayletPublicDetailList(playletPublicDetail);
        ExcelUtil<PlayletPublicDetail> util = new ExcelUtil<PlayletPublicDetail>(PlayletPublicDetail.class);
        return util.exportExcel(list, "公众号文章详情数据");
    }

    /**
     * 新增公众号文章详情
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        modelMap.put("playletItems", playletItemService.lambdaQuery().list());
        if(!this.getSysUser().isAdmin()){
            List<PlayletPublicAccount> accounts = playletPublicAccountService.lambdaQuery()
                    .eq(PlayletPublicAccount::getManagerId, this.getUserId())
                    .list();
            modelMap.put("publicAccounts", accounts);
        }else {
            modelMap.put("publicAccounts", playletPublicAccountService.selectPlayletPublicAccountList(new PlayletPublicAccount()));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存公众号文章详情
     */
    @RequiresPermissions("playlet:detail:add")
    @Log(title = "公众号文章详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PlayletPublicDetail playletPublicDetail)
    {
        if("02".equals(playletPublicDetail.getType())){
            playletPublicDetail.setContent(playletPublicDetail.getPdfUrl());
        }
        return toAjax(playletPublicDetailService.insertPlayletPublicDetail(playletPublicDetail));
    }

    /**
     * 修改公众号文章详情
     */
    @RequiresPermissions("playlet:detail:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PlayletPublicDetail playletPublicDetail = playletPublicDetailService.selectPlayletPublicDetailById(id);
        if(!this.getSysUser().isAdmin()){
            List<PlayletPublicAccount> accounts = playletPublicAccountService.lambdaQuery()
                    .eq(PlayletPublicAccount::getManagerId, this.getUserId())
                    .list();
            mmap.put("publicAccounts", accounts);
        }else {
            mmap.put("publicAccounts", playletPublicAccountService.selectPlayletPublicAccountList(new PlayletPublicAccount()));
        }
        mmap.put("playletPublicDetail", playletPublicDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存公众号文章详情
     */
    @RequiresPermissions("playlet:detail:edit")
    @Log(title = "公众号文章详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlayletPublicDetail playletPublicDetail)
    {
        if("02".equals(playletPublicDetail.getType()) && StringUtils.isNotEmpty(playletPublicDetail.getPdfUrl())){
            playletPublicDetail.setContent(playletPublicDetail.getPdfUrl());
        }
        return toAjax(playletPublicDetailService.updatePlayletPublicDetail(playletPublicDetail));
    }

    /**
     * 删除公众号文章详情
     */
    @RequiresPermissions("playlet:detail:remove")
    @Log(title = "公众号文章详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(playletPublicDetailService.deletePlayletPublicDetailByIds(ids));
    }
}
