package com.fs.course.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.SortUtils;
import com.fs.course.domain.FsUserCourseTrainingCamp;
import com.fs.course.dto.FsUserCourseTrainingCampDTO;
import com.fs.course.dto.FsUserCourseTrainingCampUpdateDTO;
import com.fs.course.service.IFsUserCourseTrainingCampService;
import com.fs.course.vo.FsUserCourseTrainingCampVO;
import com.fs.his.vo.OptionsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/course/trainingCamp")
@AllArgsConstructor
public class FsUserCourseTrainingCampController {

    private final IFsUserCourseTrainingCampService fsUserCourseTrainingCampService;

    /**
     * 查询训练营列表
     */
    @PreAuthorize("@ss.hasPermi('course:trainingCamp:list')")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam(required = false) String trainingCampName,
                           @RequestParam String scs,
                           @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(required = false, defaultValue = "10") Integer pageSize)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("trainingCampName", trainingCampName);
        params.put("scs", SortUtils.parseSort(scs));

        PageHelper.startPage(pageNum, pageSize);
        List<FsUserCourseTrainingCampVO> list = fsUserCourseTrainingCampService.selectFsUserCourseTrainingCampVOListByMap(params);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 新增训练营
     * @param params    参数
     * @return  AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('course:trainingCamp:add')")
    @Log(title = "训练营", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody FsUserCourseTrainingCampDTO params) {
        fsUserCourseTrainingCampService.add(params);
        return AjaxResult.success();
    }

    /**
     * 修改训练营
     * @param params    参数
     * @return  AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('course:trainingCamp:edit')")
    @Log(title = "训练营", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody FsUserCourseTrainingCampUpdateDTO params) {
        FsUserCourseTrainingCamp trainingCamp = fsUserCourseTrainingCampService.getById(params.getTrainingCampId());
        if (Objects.isNull(trainingCamp)) {
            return AjaxResult.error("训练营不存在");
        }

        trainingCamp.setTrainingCampName(params.getTrainingCampName());
        fsUserCourseTrainingCampService.updateById(trainingCamp);
        return AjaxResult.success();
    }

    /**
     * 删除训练营
     * @param ids    参数
     * @return  AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('course:trainingCamp:remove')")
    @Log(title = "训练营", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult del(@PathVariable Long[] ids) {
        fsUserCourseTrainingCampService.del(ids);
        return AjaxResult.success();
    }

    /**
     * 复制训练营
     * @param id    参数
     * @return  AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('course:trainingCamp:copy')")
    @Log(title = "训练营", businessType = BusinessType.INSERT)
    @PostMapping("/copy/{id}")
    public AjaxResult copy(@PathVariable Long id) {
        fsUserCourseTrainingCampService.copy(id);
        return AjaxResult.success();
    }

    @GetMapping("/getCampListLikeName")
    public R getCampListLikeName(@RequestParam(required = false) String name,
                                 @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        Map<String,Object> params = new HashMap<>();
        params.put("name", name);

        PageHelper.startPage(pageNum, pageSize);
        List<OptionsVO> campList = fsUserCourseTrainingCampService.selectCampListByMap(params);
        return R.ok().put("data", new PageInfo<>(campList));
    }

}
