package com.xjrsoft.module.evaluate.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.evaluate.dto.AddEvaluateSubmitRecordDto;
import com.xjrsoft.module.evaluate.dto.UpdateEvaluateSubmitRecordDto;
import cn.dev33.satoken.annotation.SaCheckPermission;

import com.xjrsoft.module.evaluate.dto.EvaluateSubmitRecordPageDto;
import com.xjrsoft.module.evaluate.entity.EvaluateSubmitRecord;
import com.xjrsoft.module.evaluate.service.IEvaluateSubmitRecordService;
import com.xjrsoft.module.evaluate.vo.EvaluateSubmitRecordPageVo;

import com.xjrsoft.module.evaluate.vo.EvaluateSubmitRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

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

/**
* @title: 参评人提交记录表
* @Author szs
* @Date: 2024-02-02
* @Version 1.0
*/
@RestController
@RequestMapping("/evaluate" + "/evaluateSubmitRecord")
@Api(value = "/evaluate"  + "/evaluateSubmitRecord",tags = "参评人提交记录表代码")
@AllArgsConstructor
public class EvaluateSubmitRecordController {


    private final IEvaluateSubmitRecordService evaluateSubmitRecordService;

    @GetMapping(value = "/page")
    @ApiOperation(value="参评人提交记录表列表(分页)")
    @SaCheckPermission("evaluatesubmitrecord:detail")
    public RT<PageOutput<EvaluateSubmitRecordPageVo>> page(@Valid EvaluateSubmitRecordPageDto dto){

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

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询参评人提交记录表信息")
    @SaCheckPermission("evaluatesubmitrecord:detail")
    public RT<EvaluateSubmitRecordVo> info(@RequestParam Long id){
        EvaluateSubmitRecord evaluateSubmitRecord = evaluateSubmitRecordService.getById(id);
        if (evaluateSubmitRecord == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(evaluateSubmitRecord, EvaluateSubmitRecordVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增参评人提交记录表")
    @SaCheckPermission("evaluatesubmitrecord:add")
    public RT<Boolean> add(@Valid @RequestBody AddEvaluateSubmitRecordDto dto){
        EvaluateSubmitRecord evaluateSubmitRecord = BeanUtil.toBean(dto, EvaluateSubmitRecord.class);
        boolean isSuccess = evaluateSubmitRecordService.save(evaluateSubmitRecord);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改参评人提交记录表")
    @SaCheckPermission("evaluatesubmitrecord:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateEvaluateSubmitRecordDto dto){

        EvaluateSubmitRecord evaluateSubmitRecord = BeanUtil.toBean(dto, EvaluateSubmitRecord.class);
        return RT.ok(evaluateSubmitRecordService.updateById(evaluateSubmitRecord));

    }

    @DeleteMapping
    @ApiOperation(value = "删除参评人提交记录表")
    @SaCheckPermission("evaluatesubmitrecord:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(evaluateSubmitRecordService.removeBatchByIds(ids));

    }

}