package com.fs.course.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.domain.FsUserCoursePeriod;
import com.fs.course.domain.FsUserCoursePeriodDays;
import com.fs.course.domain.FsUserCourseVideoRedPackage;
import com.fs.course.param.CompanyRedPacketParam;
import com.fs.course.param.FsBatchPeriodRedPackageParam;
import com.fs.course.param.PeriodCountParam;
import com.fs.course.service.IFsUserCoursePeriodDaysService;
import com.fs.course.service.IFsUserCoursePeriodService;
import com.fs.course.service.IFsUserCourseVideoRedPackageService;
import com.fs.course.vo.FsPeriodCountVO;
import com.fs.course.vo.FsUserCoursePeriodVO;
import com.fs.course.vo.PeriodRedPacketVO;
import com.fs.course.vo.UpdateCourseTimeVo;
import com.fs.his.vo.OptionsVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 会员营期Controller
 *
 * @author fs
 * @date 2025-04-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/course/period")
@Slf4j
public class FsUserCoursePeriodController extends BaseController {
    private final IFsUserCoursePeriodService fsUserCoursePeriodService;
    private final IFsUserCoursePeriodDaysService fsUserCoursePeriodDaysService;
    private final IFsUserCourseVideoRedPackageService fsUserCourseVideoRedPackageService;

    /**
     * 查询会员营期列表
     */
//    @PreAuthorize("@ss.hasPermi('course:period:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsUserCoursePeriod fsUserCoursePeriod)
    {
        startPage();
        List<FsUserCoursePeriod> list = fsUserCoursePeriodService.selectFsUserCoursePeriodList(fsUserCoursePeriod);
        return getDataTable(list);
    }

    @PostMapping("/page")
    @ApiOperation("自定义查询主列表分页")
    public R pageList(@RequestBody FsUserCoursePeriod fsUserCoursePeriod)
    {
//        startPage();
        PageHelper.startPage(fsUserCoursePeriod.getPageNum(), fsUserCoursePeriod.getPageSize());
        List<FsUserCoursePeriodVO> list = fsUserCoursePeriodService.selectFsUserCoursePeriodPage(fsUserCoursePeriod);
        PageInfo<FsUserCoursePeriodVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return R.ok(result);
//        return getDataTable(list);
    }

    /**
     * 导出会员营期列表
     */
//    @PreAuthorize("@ss.hasPermi('course:period:export')")
    @Log(title = "会员营期", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsUserCoursePeriod fsUserCoursePeriod)
    {
        List<FsUserCoursePeriod> list = fsUserCoursePeriodService.selectFsUserCoursePeriodList(fsUserCoursePeriod);
        ExcelUtil<FsUserCoursePeriod> util = new ExcelUtil<FsUserCoursePeriod>(FsUserCoursePeriod.class);
        return util.exportExcel(list, "userCoursePeriod");
    }

    /**
     * 获取会员营期详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:period:query')")
    @GetMapping(value = "/{periodId}")
    public AjaxResult getInfo(@PathVariable("periodId") Long periodId)
    {
        return AjaxResult.success(fsUserCoursePeriodService.selectFsUserCoursePeriodById(periodId));
    }

    /**
     * 新增会员营期
     */
    @PreAuthorize("@ss.hasPermi('course:period:add')")
    @Log(title = "会员营期", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsUserCoursePeriod fsUserCoursePeriod)
    {
        return toAjax(fsUserCoursePeriodService.insertFsUserCoursePeriod(fsUserCoursePeriod));
    }

    /**
     * 修改会员营期
     */
    @PreAuthorize("@ss.hasPermi('course:period:edit')")
    @Log(title = "会员营期", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsUserCoursePeriod fsUserCoursePeriod)
    {
        return toAjax(fsUserCoursePeriodService.updateFsUserCoursePeriod(fsUserCoursePeriod));
    }

    /**
     * 删除会员营期
     */
    @PreAuthorize("@ss.hasPermi('course:period:remove')")
    @Log(title = "会员营期", businessType = BusinessType.DELETE)
    @DeleteMapping("/{periodIds}")
    public AjaxResult remove(@PathVariable Long[] periodIds)
    {
        return toAjax(fsUserCoursePeriodService.deleteFsUserCoursePeriodByIds(periodIds));
    }

    @GetMapping("/getDays")
    public TableDataInfo getDays(FsUserCoursePeriodDays fsUserCoursePeriodDays){
        startPage();
        List<FsUserCoursePeriodDays> list = fsUserCoursePeriodDaysService.selectFsUserCoursePeriodDaysList(fsUserCoursePeriodDays);
        return getDataTable(list);
    }

    @PostMapping("/addCourse")
    public R addCourse(@RequestBody FsUserCoursePeriodDays entity){
        return fsUserCoursePeriodDaysService.addCourse(entity);
    }
    @PostMapping("/updateCourseTime")
    public R updateCourseTime(@RequestBody UpdateCourseTimeVo vo){
        return fsUserCoursePeriodDaysService.updateCourseTime(vo);
    }
    @PostMapping("/updateCourseDate")
    public R updateCourseDate(@RequestBody UpdateCourseTimeVo vo){
        return fsUserCoursePeriodDaysService.updateCourseDate(vo);
    }
    @PostMapping("/updateListCourseData")
    public R updateListCourseData(@RequestBody List<FsUserCoursePeriodDays> entity){
        return fsUserCoursePeriodDaysService.updateListCourseData(entity);
    }

    @ApiOperation("根据营期id获取公司红包金额列表(暂废弃)")
    @GetMapping("/companyList")
    public R getCompanyByPeriod(Long periodId, Long videoId) {
        List<CompanyRedPacketParam> companyList = fsUserCoursePeriodDaysService.getCompanyByPeriod(periodId, videoId);
        return R.ok().put("data", companyList);
    }

    @ApiOperation("获取设置红包金额列表")
    @GetMapping("/redPacketList")
    public R getPeriodRedPacketList(Long periodId, Long companyId) {
        List<PeriodRedPacketVO> periodRedPacketList = fsUserCoursePeriodDaysService.getPeriodRedPacketList(periodId, companyId);
        return R.ok().put("data", periodRedPacketList);
    }

    @ApiOperation("按课程批量保存设置红包金额")
    @PostMapping("/batchRedPacket")
    public R batchRedPacketMoney(@RequestBody List<FsUserCourseVideoRedPackage> videoRedPackageList) {
        try {
            fsUserCourseVideoRedPackageService.batchSaveCompanyRedPackage(videoRedPackageList);
        } catch (Exception e) {
            logger.error("按课程批量保存设置红包金额-失败！，营期id：{}", videoRedPackageList.get(0).getPeriodId());
            return R.error("保存失败！");
        }
        return R.ok();
    }

    @ApiOperation("按营期批量保存设置红包金额")
    @PostMapping("/batchRedPacket/byPeriod")
    public R batchRedPacketByPeriod(@RequestBody List<FsBatchPeriodRedPackageParam> periodRedPackageList) {
        try {
            fsUserCourseVideoRedPackageService.batchRedPacketByPeriod(periodRedPackageList);
        } catch (Exception e) {
            logger.error("按营期批量保存设置红包金额-失败！，入参：{}", periodRedPackageList);
            return R.error("保存失败！");
        }
        return R.ok();
    }

    @PostMapping("/periodCount")
    @ApiOperation("营期统计")
    public R periodCourseCount(@RequestBody PeriodCountParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<FsPeriodCountVO> list = fsUserCoursePeriodDaysService.periodCourseCount(param);
        PageInfo<FsPeriodCountVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return R.ok(result);
    }

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

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

    @ApiOperation("营期课程-上移/下移")
    @PutMapping("/courseMove")
    public R periodCourseMove(Long id, Long targetId) {
        return fsUserCoursePeriodDaysService.periodCourseMove(id, targetId);
    }

    @ApiOperation("结束营期")
    @PostMapping("/closePeriod")
    public R closePeriod(Long id) {
        fsUserCoursePeriodService.closePeriod(id);
        return R.ok();
    }

}
