package org.dromara.ygpassport.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.MPJWrappers;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.ygpassport.domain.CareerSkill;
import org.dromara.ygpassport.domain.Scholarship;
import org.dromara.ygpassport.domain.StudentList;
import org.dromara.ygpassport.domain.dto.StuCareerSkillDto;
import org.dromara.ygpassport.domain.dto.StuScholarshipDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ygpassport.domain.vo.CareerSkillVo;
import org.dromara.ygpassport.domain.bo.CareerSkillBo;
import org.dromara.ygpassport.service.ICareerSkillService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 职业技能证书
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/careerSkill")
public class CareerSkillController extends BaseController {

    private final ICareerSkillService careerSkillService;

    /**
     * 查询职业技能证书列表 old
     */
    @SaCheckPermission("ygpassport:careerSkill:list")
    @GetMapping("/list")
    public TableDataInfo<CareerSkillVo> list(CareerSkillBo bo, PageQuery pageQuery) {
        return careerSkillService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询职业技能证书列表 new
     */
    @SaCheckPermission("ygpassport:careerSkill:list")
    @GetMapping("/listNew")
    public IPage<StuCareerSkillDto> listNew(PageQuery pageQuery, StuCareerSkillDto stuCareerSkillDto) {
        return careerSkillService.selectJoinListPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()),StuCareerSkillDto.class,
            MPJWrappers.<CareerSkill>lambdaJoin()
                .selectAll(CareerSkill.class)
                .select(StudentList::getStuName)
                .leftJoin(StudentList.class, StudentList :: getStuNo, CareerSkill:: getStuNo));
    }

    /**
     * 导出职业技能证书列表
     */
    @SaCheckPermission("ygpassport:careerSkill:export")
    @Log(title = "职业技能证书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CareerSkillBo bo, HttpServletResponse response) {
        List<CareerSkillVo> list = careerSkillService.queryList(bo);
        ExcelUtil.exportExcel(list, "职业技能证书", CareerSkillVo.class, response);
    }

    /**
     * 获取职业技能证书详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:careerSkill:query")
    @GetMapping("/{id}")
    public R<CareerSkillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(careerSkillService.queryById(id));
    }

    /**
     * 新增职业技能证书
     */
    @SaCheckPermission("ygpassport:careerSkill:add")
    @Log(title = "职业技能证书", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CareerSkillBo bo) {
        return toAjax(careerSkillService.insertByBo(bo));
    }

    /**
     * 修改职业技能证书
     */
    @SaCheckPermission("ygpassport:careerSkill:edit")
    @Log(title = "职业技能证书", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CareerSkillBo bo) {
        return toAjax(careerSkillService.updateByBo(bo));
    }

    /**
     * 删除职业技能证书
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ygpassport:careerSkill:remove")
    @Log(title = "职业技能证书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(careerSkillService.deleteWithValidByIds(List.of(ids), true));
    }
}
