package com.ruoyi.busi.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.busi.domain.BusiNoticeForm;
import com.ruoyi.busi.domain.BusiNoticeSign;
import com.ruoyi.busi.query.AppNoticeQuery;
import com.ruoyi.busi.query.BusiNoticeQuery;
import com.ruoyi.busi.service.IBusiNoticeFormService;
import com.ruoyi.busi.vo.BusiNoticeVo;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.busi.domain.BusiNotice;
import com.ruoyi.busi.service.IBusiNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 通告Controller
 *
 * @author 朱春云
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/busi/notice")
public class BusiNoticeController extends BaseController
{
    @Autowired
    private IBusiNoticeService busiNoticeService;
    @Autowired
    private IBusiNoticeFormService busiNoticeFormService;

    /**
     * 查询通告列表
     */
    @PreAuthorize("@ss.hasPermi('busi:notice:list')")
    @GetMapping("/list")
    public AjaxResult list(BusiNoticeQuery query,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.queryListPage(query,page);
        return success(list);
    }

    /**
     * 查询博主报名的通告
     * @author PQZ
     * @date 11:13 2025/3/29
     * @param userId 用户id
     * @param pageNum 分页参数
     * @param pageSize 分页参数
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/queryListByUserId")
    public AjaxResult queryListByUserId(@RequestParam(name = "userId") Long userId,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.queryListByUserId(userId,page);
        return success(list);
    }

    /**
     * 导出通告列表
     */
    @PreAuthorize("@ss.hasPermi('busi:notice:export')")
    @Log(title = "通告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusiNotice busiNotice)
    {
        List<BusiNotice> list = busiNoticeService.list();
        ExcelUtil<BusiNotice> util = new ExcelUtil<BusiNotice>(BusiNotice.class);
        util.exportExcel(response, list, "通告数据");
    }

    /**
     * 获取通告详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        //通告信息
        BusiNotice detail = busiNoticeService.getById(id);
        //再获取报名动态表单
        List<BusiNoticeForm> customForm = busiNoticeFormService.list(new LambdaQueryWrapper<BusiNoticeForm>()
                .eq(BusiNoticeForm::getNoticeId,id).orderByAsc(BusiNoticeForm::getSort));
        detail.setCustomForm(customForm.stream().map(BusiNoticeForm::getLabelTitle).collect(Collectors.toList()));
        return success(detail);
    }


    @Log(title = "通告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiNotice busiNotice) throws Exception {
        return success( busiNoticeService.saveVo(busiNotice));
    }

    /**
     * 修改通告
     */
    @Log(title = "通告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiNotice busiNotice) throws Exception {
        busiNoticeService.updateByIdVo(busiNotice);
        return success();
    }

    @Log(title = "通告", businessType = BusinessType.INSERT)
    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody BusiNotice busiNotice) throws Exception {
        return success(busiNoticeService.saveOrUpdateVo(busiNotice));
    }


    @Log(title = "通告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        busiNoticeService.removeByIdsVo(list);
        return success();
    }

    /**
     * 发布或下架通告
     * noticeId 通告主键
     * publishTakeDown 1发布0下架
     */
    @PostMapping("/publishTakeDown")
    public AjaxResult publishTakeDown(@RequestParam("noticeId") String noticeId,@RequestParam("publishTakeDown") String publishTakeDown)
    {
        busiNoticeService.publishTakeDown(noticeId,publishTakeDown);
        return success();
    }

    /**
     * 审核接口
     * BusiNoticeQuery 审核参数
     */
    @PreAuthorize("@ss.hasPermi('busi:notice:edit')")
    @PostMapping("/auditInfo")
    public AjaxResult auditInfo(@RequestBody BusiNoticeQuery query)
    {
        busiNoticeService.auditInfo(query);
        return success();
    }

    /**
     * 小程序端查询通告列表
     * @author vinjor-M
     * @date 15:39 2025/3/25
     * @param query 查询条件
     * @param pageNum TODO
     * @param pageSize TODO
     * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @GetMapping("/appList")
    @Anonymous
    public AjaxResult appList(AppNoticeQuery query,
                              @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.queryAppListPage(query,page);
        return success(list);
    }

    /**
     * 小程序端查询通告详细信息
     * @author 朱春云
     * @date 15:39 2025/3/29
     * @param noticeId 通告id
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/appGetDetail")
    @Anonymous
    public AjaxResult appGetDetail(@RequestParam(name = "noticeId") String noticeId)
    {
        JSONObject detail = busiNoticeService.appGetDetail(noticeId);
        return success(detail);
    }

    /**
     * 小程序端查询浏览通告列表
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @param pageNum 1
     * @param pageSize 10
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/appFootprintList")
    public AjaxResult appFootprintList(AppNoticeQuery query,
                              @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        List<BusiNoticeVo> list = busiNoticeService.appFootprintList(query);
        return success(list);
    }

    /**
     * 小程序端查询订阅通告列表
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @param pageNum 1
     * @param pageSize 10
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/subscribeList")
    public AjaxResult subscribeList(AppNoticeQuery query,
                                       @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.subscribeList(query,page);
        return success(list);
    }



    /**
     * 小程序端查询关注通告列表
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @param pageNum 1
     * @param pageSize 10
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/loveList")
    public AjaxResult loveList(AppNoticeQuery query,
                                    @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.loveList(query,page);
        return success(list);
    }



    /**
     * 小程序端查询我的通告
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @param pageNum 1
     * @param pageSize 10
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/myNoticeList")
    public AjaxResult myNoticeList(AppNoticeQuery query,
                               @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.myNoticeList(query,page);
        return success(list);
    }

    /**
     * 小程序端查询通告主发布的通告
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @param pageNum 1
     * @param pageSize 10
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/myPublishNoticeList")
    public AjaxResult myPublishNoticeList(AppNoticeQuery query,
                                   @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNotice> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeVo> list = busiNoticeService.myPublishNoticeList(query,page);
        return success(list);
    }


    /**
     * 查看通告报名列表
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @param pageNum 1
     * @param pageSize 10
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/reportList")
    public AjaxResult reportList(AppNoticeQuery query,
                               @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BusiNoticeSign> page = new Page<>(pageNum, pageSize);
        IPage<BusiNoticeSign> list = busiNoticeService.reportList(query,page);
        return success(list);
    }


    /**
     * 查看通告报名数量
     * @author zcy
     * @date 15:39 2025/3/29
     * @param query 查询条件
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/reportNum")
    public AjaxResult reportNum(AppNoticeQuery query)
    {
        JSONObject res = busiNoticeService.reportNum(query);
        return success(res);
    }




}
