package cn.iocoder.yudao.module.inspection.controller;

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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.inspection.entity.InspectionNews;
import cn.iocoder.yudao.module.inspection.service.IInspectionNewsService;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
import cn.iocoder.yudao.module.system.api.permission.dto.RoleReqDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.iocoder.yudao.module.core.controller.BaseController;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@RestController
@RequestMapping("/admin-api/system/inspectionNews")
public class InspectionNewsController extends BaseController
{
    @Autowired
    private IInspectionNewsService inspectionNewsService;

    @Resource
    private DictDataApi dataApi;

    @Resource
    private PermissionApi permissionApi;

    @Resource
    private RoleApi roleApi;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:news:list')")
    @GetMapping("/list")
    public CommonResult list(InspectionNews inspectionNews,
                              @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                              @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<InspectionNews> page = new Page<>(pageNum, pageSize);
        IPage<InspectionNews> list = inspectionNewsService.selectInspectionNewsList(page,inspectionNews);
        return success(list);
    }

    /**
     * 新增【请填写功能名称】
     */
    @PostMapping("/add")
    public CommonResult add(@RequestBody InspectionNews inspectionNews)
    {
        return toAjax(inspectionNewsService.insertInspectionNews(inspectionNews));
    }

    @PostMapping("/edit")
    public CommonResult edit(@RequestBody InspectionNews inspectionNews)
    {
        return toAjax(inspectionNewsService.updateInspectionNews(inspectionNews));
    }

    /**
     * 删除【请填写功能名称】
     */
	@PostMapping("/del")
    public CommonResult remove(@RequestParam("idList") Long[] ids)
    {
        return toAjax(inspectionNewsService.deleteInspectionNewsByIds(ids));
    }

    /**
     * 验证当前登录用户是否可以发布通知
     *
     * @author 小李
     * @date 16:00 2024/12/18
    **/
    @GetMapping("/ifSend")
    public CommonResult<?> ifSend(){
        List<DictDataRespDTO> dataList = dataApi.getDictDataList("ins_send_news");
        List<String> values = dataList.stream().map(DictDataRespDTO::getValue).collect(Collectors.toList());
        List<Long> roleIds = permissionApi.getRoleIdsByUserId(SecurityFrameworkUtils.getLoginUserId());
        List<RoleReqDTO> roles = roleApi.getRoleList();
        List<RoleReqDTO> role = roles.stream().filter(item -> roleIds.contains(item.getId())).collect(Collectors.toList());
        List<String> codes = role.stream().map(RoleReqDTO::getCode).collect(Collectors.toList());
        List<String> collect = codes.stream().filter(values::contains).collect(Collectors.toList());
        return success(!collect.isEmpty());
    }
}
