package com.xjrsoft.module.oa.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.oa.dto.AddOfficialDocumentPostDto;
import com.xjrsoft.module.oa.dto.OfficialDocumentPostPageDto;
import com.xjrsoft.module.oa.dto.UpdateOfficialDocumentPostDto;
import com.xjrsoft.module.oa.entity.OfficialDocumentPost;
import com.xjrsoft.module.oa.service.IOfficialDocumentPostService;
import com.xjrsoft.module.oa.vo.OfficialDocumentPostPageVo;
import com.xjrsoft.module.oa.vo.OfficialDocumentPostVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
* @title: 公文发文
* @Author dzx
* @Date: 2023-12-07
* @Version 1.0
*/
@RestController
@RequestMapping("/official" + "/officialDocumentPost")
@Api(value = "/official"  + "/officialDocumentPost",tags = "公文发文代码")
@AllArgsConstructor
public class OfficialDocumentPostController {


    private final IOfficialDocumentPostService officialDocumentPostService;

    @GetMapping(value = "/page")
    @ApiOperation(value="公文发文列表(分页)")
    @SaCheckPermission("officialdocumentpost:detail")
    public RT<PageOutput<OfficialDocumentPostPageVo>> page(@Valid OfficialDocumentPostPageDto dto){

        LambdaQueryWrapper<OfficialDocumentPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(OfficialDocumentPost::getId)
                .select(OfficialDocumentPost.class,x -> VoToColumnUtil.fieldsToColumns(OfficialDocumentPostPageVo.class).contains(x.getProperty()));
        IPage<OfficialDocumentPost> page = officialDocumentPostService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<OfficialDocumentPostPageVo> pageOutput = ConventPage.getPageOutput(page, OfficialDocumentPostPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询公文发文信息")
    @SaCheckPermission("officialdocumentpost:detail")
    public RT<OfficialDocumentPostVo> info(@RequestParam Long id){
        OfficialDocumentPost officialDocumentPost = officialDocumentPostService.getById(id);
        if (officialDocumentPost == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(officialDocumentPost, OfficialDocumentPostVo.class));
    }

    @GetMapping(value = "/init-post-number")
    @ApiOperation(value="新增时初始化公文号")
    @SaCheckPermission("officialdocumentpost:detail")
    public RT<String> initPostNumber(){
        String maxPostNumber = officialDocumentPostService.getMaxPostNumber();
        return RT.ok(maxPostNumber);
    }


    @PostMapping
    @ApiOperation(value = "新增公文发文")
    @SaCheckPermission("officialdocumentpost:add")
    public RT<Boolean> add(@Valid @RequestBody AddOfficialDocumentPostDto dto){
        OfficialDocumentPost officialDocumentPost = BeanUtil.toBean(dto, OfficialDocumentPost.class);
        boolean isSuccess = officialDocumentPostService.save(officialDocumentPost);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改公文发文")
    @SaCheckPermission("officialdocumentpost:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateOfficialDocumentPostDto dto){

        OfficialDocumentPost officialDocumentPost = BeanUtil.toBean(dto, OfficialDocumentPost.class);
        return RT.ok(officialDocumentPostService.updateById(officialDocumentPost));

    }

    @DeleteMapping
    @ApiOperation(value = "删除公文发文")
    @SaCheckPermission("officialdocumentpost:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(officialDocumentPostService.removeBatchByIds(ids));

    }

}