package com.fs.course.controller;

import java.util.List;

import com.fs.common.core.domain.R;
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.param.PeriodRedPacketParam;
import com.fs.course.service.IFsUserCoursePeriodDaysService;
import com.fs.course.service.IFsUserCourseVideoRedPackageService;
import com.fs.course.vo.FsPeriodCountVO;
import com.fs.course.vo.FsUserCoursePeriodVO;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.course.domain.FsUserCoursePeriod;
import com.fs.course.service.IFsUserCoursePeriodService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 会员营期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 TableDataInfo pageList(@RequestBody FsUserCoursePeriod fsUserCoursePeriod)
    {
        startPage();
        List<FsUserCoursePeriodVO> list = fsUserCoursePeriodService.selectFsUserCoursePeriodPage(fsUserCoursePeriod);
        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("/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<PeriodRedPacketParam> 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 TableDataInfo periodCourseCount(@RequestBody PeriodCountParam param) {
        startPage();
        List<FsPeriodCountVO> list = fsUserCoursePeriodDaysService.periodCourseCount(param);
        return getDataTable(list);
    }

}
