package com.fs.company.controller.course;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.fs.common.annotation.Log;
import com.fs.common.constant.HttpStatus;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.course.config.CourseConfig;
import com.fs.course.param.FsCourseAnswerLogsParam;
import com.fs.course.service.IFsCourseAnswerLogsService;
import com.fs.course.vo.FsCourseAnswerLogsListVO;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

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

/**
 * 答题日志Controller
 *
 * @author fs
 * @date 2024-10-26
 */
@RestController
@RequestMapping("/course/courseAnswerLog")
public class FsCourseAnswerLogsController extends BaseController
{
    @Autowired
    private IFsCourseAnswerLogsService fsCourseAnswerLogsService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysConfigService configService;
    /**
     * 查询答题日志列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsCourseAnswerLogsParam param)
    {
        if (param.getPhoneMk() != null && param.getPhoneMk() != "") {
            param.setPhone(param.getPhoneMk());
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        String json = configService.selectConfigByKey("course.config");
        CourseConfig config = JSONUtil.toBean(json, CourseConfig.class);
        if (ObjectUtil.isNotEmpty(config.getIsBound())&&config.getIsBound()){
            param.setCompanyId( loginUser.getCompany().getCompanyId());
        }
        List<FsCourseAnswerLogsListVO> list = fsCourseAnswerLogsService.selectFsCourseAnswerLogsListVONew(param);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(fsCourseAnswerLogsService.selectFsCourseAnswerLogsListVONewCount(param));

        return rspData;
    }

    /**
     * 查询答题日志列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(FsCourseAnswerLogsParam param)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        //param.setCompanyUserId(loginUser.getUser().getUserId());
        if (param.getPhoneMk() != null && param.getPhoneMk() != "") {
            param.setPhone(param.getPhoneMk());
        }

        List<FsCourseAnswerLogsListVO> list = fsCourseAnswerLogsService.selectFsCourseAnswerLogsListVONew(param);

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(fsCourseAnswerLogsService.selectFsCourseAnswerLogsListVONewCount(param));

        return rspData;
    }
    /**
     * 导出答题日志列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:export')")
    @Log(title = "答题日志", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsCourseAnswerLogsParam param)
    {

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());

        if (param.getPhoneMk() != null && param.getPhoneMk() != "") {
            param.setPhone(param.getPhoneMk());
        }
        List<FsCourseAnswerLogsListVO> list = fsCourseAnswerLogsService.selectFsCourseAnswerLogsListVONew(param);
        ExcelUtil<FsCourseAnswerLogsListVO> util = new ExcelUtil<FsCourseAnswerLogsListVO>(FsCourseAnswerLogsListVO.class);
        return util.exportExcel(list, "答题记录数据");
    }

    /**
     * 导出我的答题日志列表
     */
    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:myExport')")
    @Log(title = "我的答题日志", businessType = BusinessType.EXPORT)
    @GetMapping("/myExport")
    public AjaxResult myExport(FsCourseAnswerLogsParam param)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.setCompanyId( loginUser.getCompany().getCompanyId());
        param.setCompanyUserId(loginUser.getUser().getUserId());
        if (param.getPhoneMk() != null && param.getPhoneMk() != "") {
            param.setPhone(param.getPhoneMk());
        }
        List<FsCourseAnswerLogsListVO> list = fsCourseAnswerLogsService.selectFsCourseAnswerLogsListVONew(param);
        ExcelUtil<FsCourseAnswerLogsListVO> util = new ExcelUtil<FsCourseAnswerLogsListVO>(FsCourseAnswerLogsListVO.class);
        return util.exportExcel(list, "我的答题记录数据");
    }

//    /**
//     * 获取答题日志详细信息
//     */
//    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:query')")
//    @GetMapping(value = "/{logId}")
//    public AjaxResult getInfo(@PathVariable("logId") Long logId)
//    {
//        return AjaxResult.success(fsCourseAnswerLogsService.selectFsCourseAnswerLogsByLogId(logId));
//    }

//    /**
//     * 新增答题日志
//     */
//    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:add')")
//    @Log(title = "答题日志", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody FsCourseAnswerLogs fsCourseAnswerLogs)
//    {
//        return toAjax(fsCourseAnswerLogsService.insertFsCourseAnswerLogs(fsCourseAnswerLogs));
//    }
//
//    /**
//     * 修改答题日志
//     */
//    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:edit')")
//    @Log(title = "答题日志", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody FsCourseAnswerLogs fsCourseAnswerLogs)
//    {
//        return toAjax(fsCourseAnswerLogsService.updateFsCourseAnswerLogs(fsCourseAnswerLogs));
//    }
//
//    /**
//     * 删除答题日志
//     */
//    @PreAuthorize("@ss.hasPermi('course:courseAnswerLog:remove')")
//    @Log(title = "答题日志", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{logIds}")
//    public AjaxResult remove(@PathVariable Long[] logIds)
//    {
//        return toAjax(fsCourseAnswerLogsService.deleteFsCourseAnswerLogsByLogIds(logIds));
//    }
}
