package com.xiaoniu.subsystem.business.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoniu.common.core.result.R;
import com.xiaoniu.common.core.utils.BeanUtil;
import com.xiaoniu.common.core.utils.poi.ExcelUtil;
import com.xiaoniu.common.log.annotation.Log;
import com.xiaoniu.common.log.enums.BusinessType;
import com.xiaoniu.subsystem.business.domain.WaitDone;
import com.xiaoniu.subsystem.business.domain.dto.WaitDoneDTO;
import com.xiaoniu.subsystem.business.domain.dto.WaitDonePageDTO;
import com.xiaoniu.subsystem.business.domain.dto.WaitDoneUpdateDTO;
import com.xiaoniu.subsystem.business.domain.vo.WaitDoneVO;
import com.xiaoniu.subsystem.business.service.IWaitDoneService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static com.xiaoniu.common.security.utils.SecurityUtils.getUnitId;
import static com.xiaoniu.common.security.utils.SecurityUtils.getUserId;

/**
 * <p>
 * 待办管理表 前端控制器
 * </p>
 *
 * @author haoxuyang
 * @since 2024-12-03 01:56:51
 */
@RestController
@AllArgsConstructor
@Tag(name = "待办管理表", description = "待办管理表接口")
@RequestMapping("/wait/done")
public class WaitDoneController {

    private final IWaitDoneService waitDoneService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "详情", description = "传入WaitDone")
    public R<WaitDoneVO> detail(@Parameter(description = "主键", required = true) @RequestParam Long waitDoneId) {
        WaitDone detail = waitDoneService.getById(waitDoneId);
        return R.data(BeanUtil.copy(detail, WaitDoneVO.class));
    }

    /**
     * 分页 代办管理表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页", description = "传入WaitDone")
    public R<IPage<WaitDoneVO>> page(@Validated WaitDonePageDTO dto) {
        IPage<WaitDone> pages = waitDoneService.page(new Page<>(dto.getPageNum(), dto.getPageSize()),
                new LambdaQueryWrapper<WaitDone>()
                        .orderByDesc(WaitDone::getCreateTime)
                        .eq(WaitDone::getStatus,0)
                        .eq(WaitDone::getWaitDoneUnit,getUnitId())
                        .ne(WaitDone::getType,4)
                        .or(w -> w
                                .eq(WaitDone::getWaitDoneUser,getUserId())
                                .eq(WaitDone::getType,4)));
        return R.data(BeanUtil.copy(pages, WaitDoneVO.class));
    }


   /**
    * 修改 代办管理表状态
    */
   @PostMapping("/update")
   @ApiOperationSupport(order = 3)
   @Operation(summary = "修改代办状态", description = "传入WaitDone")
   public R update(@Valid @RequestBody WaitDoneUpdateDTO dto) {
       return R.status(waitDoneService.updateById(BeanUtil.copy(dto, WaitDoneVO.class)
               .setUpdateUser(getUserId()).setUpdateTime(new Date())));
   }


}
