package com.xjrsoft.module.assessment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
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.assessment.dto.AddAssessmentTemplateDto;
import com.xjrsoft.module.assessment.dto.AssessmentTemplatePageDto;
import com.xjrsoft.module.assessment.dto.UpdateAssessmentTemplateDto;
import com.xjrsoft.module.assessment.entity.AssessmentTemplate;
import com.xjrsoft.module.assessment.service.IAssessmentTemplateService;
import com.xjrsoft.module.assessment.vo.AssessmentTemplatePageVo;
import com.xjrsoft.module.assessment.vo.AssessmentTemplateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
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;
import java.util.List;

/**
* @title: 考核模板
* @Author dzx
* @Date: 2024-03-29
* @Version 1.0
*/
@RestController
@RequestMapping("/assessment" + "/assessmentTemplate")
@Api(value = "/assessment"  + "/assessmentTemplate",tags = "考核模板代码")
@AllArgsConstructor
public class AssessmentTemplateController {


    private final IAssessmentTemplateService assessmentTemplateService;

    @GetMapping(value = "/page")
    @ApiOperation(value="考核模板列表(分页)")
    @SaCheckPermission("assessmenttemplate:detail")
    public RT<PageOutput<AssessmentTemplatePageVo>> page(@Valid AssessmentTemplatePageDto dto){

        LambdaQueryWrapper<AssessmentTemplate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotEmpty(dto.getName()), AssessmentTemplate::getName, dto.getName())
            .orderByDesc(AssessmentTemplate::getId)
            .select(AssessmentTemplate.class,x -> VoToColumnUtil.fieldsToColumns(AssessmentTemplatePageVo.class).contains(x.getProperty()));
        IPage<AssessmentTemplate> page = assessmentTemplateService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<AssessmentTemplatePageVo> pageOutput = ConventPage.getPageOutput(page, AssessmentTemplatePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询考核模板信息")
    @SaCheckPermission("assessmenttemplate:detail")
    public RT<AssessmentTemplateVo> info(@RequestParam Long id){
        AssessmentTemplateVo assessmentTemplate = assessmentTemplateService.getById(id);
        if (assessmentTemplate == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(assessmentTemplate);
    }


    @PostMapping
    @ApiOperation(value = "新增考核模板")
    @SaCheckPermission("assessmenttemplate:add")
    public RT<Boolean> add(@Valid @RequestBody AddAssessmentTemplateDto dto){
        boolean isSuccess = assessmentTemplateService.addCustom(dto);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改考核模板")
    @SaCheckPermission("assessmenttemplate:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateAssessmentTemplateDto dto){
        return RT.ok(assessmentTemplateService.update(dto));

    }

    @DeleteMapping
    @ApiOperation(value = "删除考核模板")
    @SaCheckPermission("assessmenttemplate:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(assessmentTemplateService.delete(ids));
    }

}