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.AddOfficialDocumentReceivedDto;
import com.xjrsoft.module.oa.dto.OfficialDocumentReceivedPageDto;
import com.xjrsoft.module.oa.dto.UpdateOfficialDocumentReceivedDto;
import com.xjrsoft.module.oa.entity.OfficialDocumentReceived;
import com.xjrsoft.module.oa.service.IOfficialDocumentReceivedService;
import com.xjrsoft.module.oa.vo.OfficialDocumentReceivedPageVo;
import com.xjrsoft.module.oa.vo.OfficialDocumentReceivedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 公文收文
* @Author szs
* @Date: 2024-03-13
* @Version 1.0
*/
@RestController
@RequestMapping("/oa" + "/officialDocumentReceived")
@Api(value = "/oa"  + "/officialDocumentReceived",tags = "公文收文代码")
@AllArgsConstructor
public class OfficialDocumentReceivedController {


    private final IOfficialDocumentReceivedService officialDocumentReceivedService;

    @GetMapping(value = "/page")
    @ApiOperation(value="公文收文列表(分页)")
    @SaCheckPermission("officialdocumentreceived:detail")
    public RT<PageOutput<OfficialDocumentReceivedPageVo>> page(@Valid OfficialDocumentReceivedPageDto dto){

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

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


    @PostMapping
    @ApiOperation(value = "新增公文收文")
    @SaCheckPermission("officialdocumentreceived:add")
    public RT<Boolean> add(@Valid @RequestBody AddOfficialDocumentReceivedDto dto){
        OfficialDocumentReceived officialDocumentReceived = BeanUtil.toBean(dto, OfficialDocumentReceived.class);
        boolean isSuccess = officialDocumentReceivedService.save(officialDocumentReceived);
    return RT.ok(isSuccess);
    }

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

        OfficialDocumentReceived officialDocumentReceived = BeanUtil.toBean(dto, OfficialDocumentReceived.class);
        return RT.ok(officialDocumentReceivedService.updateById(officialDocumentReceived));

    }

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

    }

    @GetMapping(value = "/init-received-number")
    @ApiOperation(value="新增时初始化公文号")
    @SaCheckPermission("officialdocumentpost:detail")
    public RT<String> initReceivedNumber(){
        String maxReceivedNumber = officialDocumentReceivedService.getMaxReceivedNumber();
        return RT.ok(maxReceivedNumber);
    }

}