package com.fs.course.controller;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.fs.common.core.domain.R;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.utils.ServletUtils;
import com.fs.course.config.CourseConfig;
import com.fs.course.mapper.FsUserCourseMapper;
import com.fs.course.mapper.FsUserCourseVideoMapper;
import com.fs.course.param.FsCourseRedPacketLogParam;
import com.fs.course.vo.FsCourseRedPacketLogListPVO;
import com.fs.framework.web.service.TokenService;
import com.fs.his.utils.PhoneUtil;
import com.fs.his.vo.OptionsVO;
import com.fs.system.service.ISysConfigService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
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.FsCourseRedPacketLog;
import com.fs.course.service.IFsCourseRedPacketLogService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

import static com.fs.his.utils.PhoneUtil.encryptPhone;

/**
 * 短链课程看课记录Controller
 *
 * @author fs
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/course/courseRedPacketLog")
public class FsCourseRedPacketLogController extends BaseController
{
    @Autowired
    private IFsCourseRedPacketLogService fsCourseRedPacketLogService;
    @Autowired
    FsUserCourseMapper fsUserCourseMapper;
    @Autowired
    TokenService tokenService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    FsUserCourseVideoMapper fsUserCourseVideoMapper;
    /**
     * 查询短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseRedPacketLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseRedPacketLogParam fsCourseRedPacketLog)
    {
        startPage();

        if (fsCourseRedPacketLog.getPhoneMk() != null && fsCourseRedPacketLog.getPhoneMk() != "") {
            fsCourseRedPacketLog.setPhone(encryptPhone(fsCourseRedPacketLog.getPhoneMk()));
        }

        List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {

            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
        }
        return getDataTable(list);
    }

    /**
     * 导出短链课程看课记录列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseRedPacketLog:export')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseRedPacketLogParam fsCourseRedPacketLog)
    {
        if (fsCourseRedPacketLog.getPhoneMk()!=null&&fsCourseRedPacketLog.getPhoneMk()!=""){
            fsCourseRedPacketLog.setPhone(encryptPhone(fsCourseRedPacketLog.getPhoneMk()));
        }
        List<FsCourseRedPacketLogListPVO> list = fsCourseRedPacketLogService.selectFsCourseRedPacketLogListVO(fsCourseRedPacketLog);
        for (FsCourseRedPacketLogListPVO fsCourseRedPacketLogListPVO : list) {

            fsCourseRedPacketLogListPVO.setPhone(PhoneUtil.decryptAutoPhoneMk(fsCourseRedPacketLogListPVO.getPhone()));
        }
        ExcelUtil<FsCourseRedPacketLogListPVO> util = new ExcelUtil<FsCourseRedPacketLogListPVO>(FsCourseRedPacketLogListPVO.class);
        return util.exportExcel(list, "短链课程看课记录数据");
    }

    /**
     * 获取短链课程看课记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('course:courseRedPacketLog:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId)
    {
        return AjaxResult.success(fsCourseRedPacketLogService.selectFsCourseRedPacketLogByLogId(logId));
    }

    /**
     * 新增短链课程看课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseRedPacketLog:add')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsCourseRedPacketLog fsCourseRedPacketLog)
    {
        return toAjax(fsCourseRedPacketLogService.insertFsCourseRedPacketLog(fsCourseRedPacketLog));
    }

    /**
     * 修改短链课程看课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseRedPacketLog:edit')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsCourseRedPacketLog fsCourseRedPacketLog)
    {
        return toAjax(fsCourseRedPacketLogService.updateFsCourseRedPacketLog(fsCourseRedPacketLog));
    }

    /**
     * 删除短链课程看课记录
     */
    @PreAuthorize("@ss.hasPermi('course:courseRedPacketLog:remove')")
    @Log(title = "短链课程看课记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        return toAjax(fsCourseRedPacketLogService.deleteFsCourseRedPacketLogByLogIds(logIds));
    }

    @GetMapping("/courseList")
    public R courseList()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        String json = configService.selectConfigByKey("course.config");
        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
        List<OptionsVO> optionsVOS;
        if (ObjectUtil.isNotEmpty(config.getIsBound())&&config.getIsBound()){
            optionsVOS = fsUserCourseMapper.selectFsUserCourseAllListByUserId(userId);
        }else{
            optionsVOS = fsUserCourseMapper.selectFsUserCourseAllList();
        }
        return R.ok().put("list", optionsVOS);
    }

    @GetMapping(value = "/videoList/{id}")
    public R videoList(@PathVariable("id") Long id)
    {
        List<OptionsVO> optionsVOS = fsUserCourseVideoMapper.selectFsUserCourseVodeAllList(id);
        return R.ok().put("list", optionsVOS);
    }
}
