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

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.system.api.dto.admin.GetByBcqtPageDTO;
import com.kxs.system.api.model.KxsBcqtTeam;
import com.kxs.system.api.model.KxsCamp;
import com.kxs.system.api.model.KxsCampBonusLog;
import com.kxs.system.api.model.KxsCampUser;
import com.kxs.system.api.vo.admin.CampUserExcelVO;
import com.kxs.system.biz.constant.enums.CampStatusEnum;
import com.kxs.system.biz.service.KxsBcqtTeamService;
import com.kxs.system.biz.service.KxsCampBonusLogService;
import com.kxs.system.biz.service.KxsCampService;
import com.kxs.system.biz.service.KxsCampUserService;
import com.pig4cloud.plugin.excel.annotation.RequestExcel;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
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;


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

        return R.ok(kxsCampUserService.page(page, Wrappers.<KxsCampUser>lambdaQuery()
                .eq(KxsCampUser::getCampId, param.getCampId())
                .eq(StrUtil.isNotEmpty(param.getUserCode()), KxsCampUser::getUserCode, param.getUserCode())));
    }


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

        return kxsCampService.addData(param);
    }


    /**
     * 训练营-编辑
     *
     * @param param 实体对象
     * @return 训练营-编辑
     */
    @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 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 param 实体对象
     * @return 训练营-增减奖金池
     */
    @SysLog("增减奖金池")
    @PostMapping("/changeBonus")
//    @PreAuthorize("@pms.hasPermission('sys_camp_change_bonus')")
    public R changeBonus(@RequestBody @Valid KxsCampBonusLog param) {

        return kxsCampBonusLogService.changeBonus(param);
    }


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

        if(param.getCampId() == null){
            return R.failed("训练营ID不能为空");
        }
        return R.ok(kxsCampBonusLogService.page(page, new QueryWrapper<>(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);
    }

}

