package com.xiaoniu.subsystem.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.subsystem.business.domain.WaitDone;
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.validation.Valid;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Set;

import static com.xiaoniu.common.security.utils.SecurityUtils.*;

/**
 * <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) {
        Set<String> roles = getLoginUser().getRoles();
        String s = roles.toArray(new String[0])[0];
        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())
                        .in(CollectionUtils.isNotEmpty(dto.getTypes()),WaitDone::getType,dto.getTypes())
                        .and(CollectionUtils.isEmpty(dto.getTypes()),
                                w -> w.ne(WaitDone::getType,4)
                                        .or(w1 -> w1
                                                .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())));
   }


}
