package com.xjrsoft.module.oa.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
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.FileReceivePullPageDto;
import com.xjrsoft.module.oa.dto.FileReceivePushPageDto;
import com.xjrsoft.module.oa.entity.FileReceive;
import com.xjrsoft.module.oa.entity.WfOaPull;
import com.xjrsoft.module.oa.entity.WfOaPush;
import com.xjrsoft.module.oa.service.IFileReceiveService;
import com.xjrsoft.module.oa.service.IWfOaPullService;
import com.xjrsoft.module.oa.service.IWfOaPushService;
import com.xjrsoft.module.oa.vo.FileReceivePullPageVo;
import com.xjrsoft.module.oa.vo.FileReceivePullVo;
import com.xjrsoft.module.oa.vo.FileReceivePushPageVo;
import com.xjrsoft.module.oa.vo.FileReceivePushVo;
import com.xjrsoft.module.organization.entity.Department;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.system.entity.File;
import com.xjrsoft.module.system.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * @title: 文件接收管理
 * @Author fanxp
 * @Date: 2023-11-03
 * @Version 1.0
 */
@RestController
@RequestMapping("/oa" + "/filereceive")
@Api(value = "/oa" + "/filereceive", tags = "文件接收管理代码")
@AllArgsConstructor
public class FileReceiveController {


    private final IFileReceiveService fileReceiveService;

    private final IWfOaPullService wfOaPullService;

    private final IWfOaPushService wfOaPushService;

    private final IFileService fileService;

    @GetMapping(value = "/pull-page")
    @ApiOperation(value = "收文查询(分页)")
    @SaCheckPermission("filereceive:detail")
    public RT<PageOutput<FileReceivePullPageVo>> pullPage(@Valid FileReceivePullPageDto dto) {
        //获取用户id
        Long userId =  StpUtil.getLoginIdAsLong();

        MPJLambdaWrapper<FileReceive> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .innerJoin(WfOaPull.class, WfOaPull::getId, FileReceive::getDataId)
                .leftJoin(User.class,User::getId,WfOaPull::getPersonName,ext -> ext.selectAs(User::getName, FileReceivePullPageVo::getUserNameCn))
                .eq(FileReceive::getFileType, 2)
                .eq(FileReceive::getReceiverUserId, userId)
                .select(WfOaPull::getId)
                .select(WfOaPull.class, x -> VoToColumnUtil.fieldsToColumns(FileReceivePullPageVo.class).contains(x.getProperty()));

        IPage<FileReceivePullPageVo> page = fileReceiveService.selectJoinListPage(ConventPage.getPage(dto),FileReceivePullPageVo.class, queryWrapper);
        PageOutput<FileReceivePullPageVo> pageOutput = ConventPage.getPageOutput(page);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/pull-info")
    @ApiOperation(value = "根据id查询文件接收管理信息")
    @SaCheckPermission("filereceive:detail")
    public RT<FileReceivePullVo> pullInfo(@RequestParam Long id) {
        MPJLambdaWrapper<WfOaPull> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .leftJoin(User.class,User::getId,WfOaPull::getPersonName,ext -> ext.selectAs(User::getName, FileReceivePullVo::getUserNameCn))
                .eq(WfOaPull::getId, id)
                .select(WfOaPull::getId)
                .select(WfOaPull.class, x -> VoToColumnUtil.fieldsToColumns(FileReceivePullVo.class).contains(x.getProperty()));

        FileReceivePullVo fileReceive = wfOaPullService.selectJoinOne(FileReceivePullVo.class,queryWrapper);
        if (fileReceive == null) {
            return RT.error("找不到此数据！");
        }
        // 获取文件列表
        fileReceive.setFileInfos(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId,fileReceive.getFiles())));
        return RT.ok(BeanUtil.toBean(fileReceive, FileReceivePullVo.class));
    }

    @GetMapping(value = "/push-page")
    @ApiOperation(value = "发文查询(分页)")
    @SaCheckPermission("filereceive:detail")
    public RT<PageOutput<FileReceivePushPageVo>> pushPage(@Valid FileReceivePushPageDto dto) {
        //获取用户id
        Long userId =  StpUtil.getLoginIdAsLong();

        MPJLambdaWrapper<FileReceive> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .innerJoin(WfOaPush.class, WfOaPush::getId, FileReceive::getDataId)
                .leftJoin(Department.class,Department::getId,WfOaPush::getUserName,ext -> ext.selectAs(Department::getName, FileReceivePushPageVo::getOrgName))
                .eq(FileReceive::getFileType, 1)
                .eq(FileReceive::getReceiverUserId, userId)
                .select(WfOaPush::getId)
                .select(WfOaPush.class, x -> VoToColumnUtil.fieldsToColumns(FileReceivePushPageVo.class).contains(x.getProperty()));

        IPage<FileReceivePushPageVo> page = fileReceiveService.selectJoinListPage(ConventPage.getPage(dto),FileReceivePushPageVo.class, queryWrapper);
        PageOutput<FileReceivePushPageVo> pageOutput = ConventPage.getPageOutput(page);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/push-info")
    @ApiOperation(value = "根据id查询文件发送管理信息")
    @SaCheckPermission("filereceive:detail")
    public RT<FileReceivePushVo> pushInfo(@RequestParam Long id) {
        MPJLambdaWrapper<WfOaPush> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .leftJoin(Department.class,Department::getId,WfOaPush::getUserName, ext -> ext.selectAs(Department::getName, FileReceivePushVo::getOrgName))
                .eq(WfOaPush::getId, id)
                .select(WfOaPush::getId)
                .select(WfOaPush.class, x -> VoToColumnUtil.fieldsToColumns(FileReceivePushVo.class).contains(x.getProperty()));

        FileReceivePushVo fileReceive = wfOaPushService.selectJoinOne(FileReceivePushVo.class,queryWrapper);
        if (fileReceive == null) {
            return RT.error("找不到此数据！");
        }
        // 获取文件列表
        fileReceive.setFileInfos(fileService.list(Wrappers.<File>query().lambda().eq(File::getFolderId,fileReceive.getFiles())));
        return RT.ok(BeanUtil.toBean(fileReceive, FileReceivePushVo.class));
    }

}