package cn.iocoder.yudao.module.system.controller.admin.sys;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.NewsPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.NewsDO;
import cn.iocoder.yudao.module.system.service.sys.NewsService;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 系统文章新闻")
@RestController
@RequestMapping("/system/news")
@Validated
public class NewsController {

    @Resource
    private NewsService newsService;
    @Resource
    private TenantService tenantService;

    @PostMapping("/create")
    @Operation(summary = "创建系统新闻")
    @TenantIgnore
    public CommonResult<Long> createNews(@Valid @RequestBody NewsDO newsDO) {
        newsDO.setTenantId(newsDO.getTenantId() == null ? TenantContextHolder.getTenantId() : newsDO.getTenantId());
        return success(newsService.createNews(newsDO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统文章新闻")
    @TenantIgnore
    public CommonResult<Boolean> updateNews(@Valid @RequestBody NewsDO newsDO) {
        newsService.updateNews(newsDO);
        return success(true);
    }


    @GetMapping("/updateStatus")
    @Operation(summary = "修改状态")
    @TenantIgnore
    public CommonResult<Boolean> updateStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) {
        newsService.updateStatus(id, status);
        return success(true);
    }


    @PostMapping("/delete")
    @Operation(summary = "删除系统文章新闻")
    @TenantIgnore
    public CommonResult<Boolean> deleteNews(@RequestBody Long[] ids) {
        newsService.deleteNews(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统文章新闻")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<NewsDO> getNews(@RequestParam("id") Long id) {
        NewsDO news = newsService.getNews(id);
        return success(news);
    }

    @GetMapping("/page")
    @Operation(summary = "系统新闻分页")
    @TenantIgnore
    public CommonResult<PageResult<NewsDO>> getNewsPage(@Valid NewsPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        PageResult<NewsDO> pageResult = newsService.getNewsPage(pageReqVO);
        for (NewsDO newsDO : pageResult.getList()) {
            newsDO.setTenantName(tenantService.getTenant(newsDO.getTenantId()).getName());
        }
        return success(pageResult);
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出系统文章新闻 Excel")
//    @PreAuthorize("@ss.hasPermission('system:news:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportNewsExcel(@Valid NewsPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<NewsDO> list = newsService.getNewsPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "系统文章新闻.xls", "数据", NewsRespVO.class,
//                        BeanUtils.toBean(list, NewsRespVO.class));
//    }

}