package com.kxs.system.biz.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxs.common.core.util.R;
import com.kxs.common.log.annotation.SysLog;
import com.kxs.common.security.annotation.Inner;
import com.kxs.system.api.dto.admin.GetByBcqtPageDTO;
import com.kxs.system.api.dto.admin.camp.CampPageLogDTO;
import com.kxs.system.api.dto.admin.camp.CampReportAuditDTO;
import com.kxs.system.api.model.*;
import com.kxs.system.api.vo.admin.camp.*;
import com.kxs.system.biz.constant.enums.CampStatusEnum;
import com.kxs.system.biz.service.*;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 系统模块(SysCamp)控制层
 *
 * @author 系统
 * @since 2024-04-08 15:44:05
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("sysCamp")
public class SysCampController {

    /**
     * 服务对象
     */
    private final KxsCampService kxsCampService;
    private final KxsCampUserService kxsCampUserService;
    private final KxsCampBonusLogService kxsCampBonusLogService;
    private final KxsBcqtTeamService kxsBcqtTeamService;
    private final KxsCampReportService kxsCampReportService;
    private final KxsCampGroupService kxsCampGroupService;


    /**
     * 训练营-列表
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 训练营-列表
     */
    @GetMapping("/page")
    public R page(Page<SysCampPageVO> page, @ParameterObject KxsCamp param) {

        return R.ok(kxsCampService.getBySysPage(page, param));
    }

    /**
     * 训练营-分组参与创客列表
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 训练营-列表
     */
    @GetMapping("/campUserPage")
//    @PreAuthorize("@pms.hasPermission('sys_camp_user_page')")
    public R campUserPage(Page<SysCampUserPageVO> page, @ParameterObject KxsCampUser param) {

        return R.ok(kxsCampUserService.getSysByPage(page, param));
    }

    /**
     * 训练营-导出分组参与创客
     *
     * @param param 查询实体
     * @return 训练营-列表
     */
    @ResponseExcel(name = "分组参与创客列表", include = { "groupName","groupId", "username","userCode","createTime","teamOpenNum","teamOrderNum","teamLeaderNum"})
    @GetMapping("/exportGroupUser")
//    @PreAuthorize("@pms.hasPermission('sys_camp_user_export')")
    public List<SysCampUserPageVO> exportGroupUser(@ParameterObject KxsCampUser param) {

        return kxsCampUserService.exportGroupUser(param);
    }

    /**
     * 组删除用户
     *
     * @param ids 参数
     * @return {@link R}
     */
    @PostMapping("/groupRemoveUsers")
//    @PreAuthorize("@pms.hasPermission('sys_camp_user_remove')")
    public R groupRemoveUsers(@RequestBody List<Integer> ids) {

        return R.ok(kxsCampService.removeByCampUserIds(ids));
    }


    /**
     * 训练营-新增
     *
     * @param param 实体对象
     * @return 训练营-新增
     */
    @SysLog("训练营-新增")
    @PostMapping("/add")
//    @PreAuthorize("@pms.hasPermission('sys_camp_add')")
    public R add(@RequestBody @Valid KxsCamp param) {

        return kxsCampService.addData(param);
    }


    /**
     * 训练营-编辑
     *
     * @param param 实体对象
     * @return 训练营-编辑
     */
    @SysLog("训练营-编辑")
    @PostMapping("/update")
//    @PreAuthorize("@pms.hasPermission('sys_camp_edit')")
    public R update(@RequestBody @Valid KxsCamp param) {

        return kxsCampService.updateData(param);
    }

    /**
     * 训练营-开启关闭活动
     *
     * @param param 实体对象
     * @return true/false
     */
    @PutMapping("/changeStatus")
//    @PreAuthorize("@pms.hasPermission('sys_camp_edit')")
    public R changeStatus(@RequestBody KxsCamp param) {

        return kxsCampService.changeStatus(param);
    }


    /**
     * 训练营-删除
     *
     * @param id 主键ID
     * @return 训练营-删除
     */
    @SysLog("删除训练营")
    @DeleteMapping("removeById/{id}")
//    @PreAuthorize("@pms.hasPermission('sys_camp_remove')")
    public R removeById(@PathVariable("id") Integer id) {

        KxsCamp kxsCamp = kxsCampService.getById(id);
        if(!Objects.equals(kxsCamp.getStatus(), CampStatusEnum.STATUS_CLOSE.getType())){
            return R.ok("该训练营状态不可删除");
        }
        return R.ok(kxsCampService.removeById(id));
    }


    /**
     * 训练营-分组-列表
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 训练营-分组-列表
     */
    @GetMapping("/list")
//    @PreAuthorize("@pms.hasPermission('sys_camp_group_list')")
    public R list(Page<KxsCampGroup> page, @ParameterObject KxsCampGroup param) {

        return R.ok(kxsCampGroupService.page(page, new QueryWrapper<>(param)));
    }


    /**
     * 训练营-分组-详情
     *
     * @param id 主键ID
     * @return 训练营-分组-详情
     */
    @GetMapping("/getById")
    //    @PreAuthorize("@pms.hasPermission('sys_camp_group_list')")
    public R getById(@RequestParam("id") Integer id) {

        return R.ok(kxsCampGroupService.getById(id));
    }


    /**
     * 训练营-分组-添加
     *
     * @param param 实体对象
     * @return 训练营-分组-添加
     */
    @PostMapping("/groupAdd")
    //    @PreAuthorize("@pms.hasPermission('sys_camp_group_add')")
    public R groupAdd(@Valid @RequestBody KxsCampGroup param) {

        return R.ok(kxsCampGroupService.addData(param));
    }


    /**
     * 训练营-分组-修改
     *
     * @param param 实体对象
     * @return 训练营-分组-修改
     */
    @PutMapping("/groupUpdate")
    //    @PreAuthorize("@pms.hasPermission('sys_camp_group_edit')")
    public R groupUpdate(@Valid @RequestBody KxsCampGroup param) {

        return R.ok(kxsCampGroupService.updateById(param));
    }


    /**
     * 训练营-分组-删除
     *
     * @param groupId 主键ID结合
     * @return 训练营-分组-删除
     */
    @DeleteMapping("/groupDelete/{groupId}")
    //    @PreAuthorize("@pms.hasPermission('sys_camp_group_remove')")
    public R groupDelete(@PathVariable("groupId") Integer groupId) {

        return R.ok(kxsCampGroupService.removeByData(groupId));
    }


    /**
     * 训练营-添加创客
     *
     * @param campUserExcelVOList 导入对象
     * @return 训练营-添加创客
     */
    @PostMapping("/importUser")
//    @PreAuthorize("@pms.hasPermission('sys_camp_add_user')")
    public R importUser(@RequestExcel(ignoreEmptyRow = true) List<CampUserExcelVO> campUserExcelVOList, BindingResult bindingResult) {

        return kxsCampService.importUserData(campUserExcelVOList, bindingResult);
    }


    /**
     * 训练营-日志列表
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 训练营-扣除记录
     */
    @GetMapping("/pageLog")
//    @PreAuthorize("@pms.hasPermission('sys_camp_change_list')")
    public R pageLog(Page<CampBonusLogVO> page, @Valid @ParameterObject CampPageLogDTO param) {

        return R.ok(kxsCampBonusLogService.getSysPage(page, param));
    }

    /**
     * 训练营-导出日志列表
     *
     * @param param 查询实体
     * @return 训练营-扣除记录
     */
    @ResponseExcel(name = "训练营统计明细", exclude = {"id", "kind", "sourceId"})
    @GetMapping("/exportLog")
//    @PreAuthorize("@pms.hasPermission('sys_camp_change_list')")
    public List<CampBonusLogVO> exportLog(@Valid @ParameterObject CampPageLogDTO param) {

        return kxsCampBonusLogService.exportLog(param);
    }

    /**
     * 训练营-报备列表
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 训练营-报备列表
     */
    @GetMapping("/reportPage")
//    @PreAuthorize("@pms.hasPermission('sys_camp_report_list')")
    public R reportPage(Page<ReportCampPageVO> page, @ParameterObject KxsCampReport param) {

        return R.ok(kxsCampReportService.reportPage(page, param));
    }

    /**
     * 训练营-报备审核
     *
     * @return 训练营-报备审核
     */
    @PutMapping("/reportAudit")
//    @PreAuthorize("@pms.hasPermission('sys_camp_report_audit')")
    public R reportAudit(@Valid @RequestBody CampReportAuditDTO param) {

        return R.ok(kxsCampReportService.reportAudit(param));
    }













    /**
     * 训练营-百城千团列表
     *
     * @param page 分页对象
     * @param param 查询实体
     * @return 训练营-百城千团列表
     */
    @GetMapping("/bcqtPage")
//    @PreAuthorize("@pms.hasPermission('sys_camp_bcqt_page')")
    public R bcqtPage(Page<KxsBcqtTeam> page, @ParameterObject GetByBcqtPageDTO param) {

        return kxsBcqtTeamService.getByBcqtPage(page, param);
    }

    /**
     * 修改团名
     * @param kxsBcqtTeam  实体
     * @return
     */
    @PostMapping("/updateBcqt")
//    @PreAuthorize("@pms.hasPermission('sys_camp_bcqt_update')")
    public R updateBcqt(@RequestBody KxsBcqtTeam kxsBcqtTeam) {
        kxsBcqtTeamService.updateBcqt(kxsBcqtTeam);
        return R.ok();
    }

    /**
     * 训练营-百城千团列表-激活统计
     *
     * @param id 查询团队id
     * @return 训练营-百城千团列表
     */
    @GetMapping("/bcqtGetDetail")
//    @PreAuthorize("@pms.hasPermission('sys_camp_bcqt_detail')")
    public R bcqtGetDetail(@RequestParam("id") Long id) {

        return kxsBcqtTeamService.bcqtGetDetail(id);
    }

}

