package com.fs.qw;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.qw.domain.HyWorkTask;
import com.fs.qw.domain.QwWorkTask;
import com.fs.qw.param.QwWorkTaskListParam;
import com.fs.qw.service.IHyWorkTaskService;
import com.fs.qw.service.IQwWorkTaskService;
import com.fs.qw.vo.QwWorkTaskAllListVO;
import com.fs.qw.vo.QwWorkTaskListVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 企微任务看板Controller
 *
 * @author fs
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/qw/QwWorkTask")
public class QwQwWorkTaskController extends BaseController
{
    @Autowired
    private IHyWorkTaskService hyWorkTaskService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FsCourseWatchLogMapper fsCourseWatchLogMapper;
    /**
     * 查询企微任务看板列表
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwWorkTaskListParam qwWorkTask)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwWorkTask.setCompanyId(loginUser.getCompany().getCompanyId());
        if(ObjectUtils.isNull(qwWorkTask.getCompanyUserId())) {
            qwWorkTask.setCompanyUserId(loginUser.getUser().getUserId());
        }
        List<QwWorkTaskListVO> list = hyWorkTaskService.selectHyWorkTaskListVONew(qwWorkTask);
        for (QwWorkTaskListVO qwWorkTaskListVO : list) {
            qwWorkTaskListVO.setLogs(fsCourseWatchLogMapper.selectFsCourseWatchLog7DayByExtId(qwWorkTaskListVO.getExtId()));
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(hyWorkTaskService.selectHyWorkTaskListVONewCount(qwWorkTask));
        return rspData;
    }

    /**
     * 导出企微任务看板列表
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTask:export')")
    @Log(title = "企微任务看板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HyWorkTask qwWorkTask)
    {
        List<HyWorkTask> list = hyWorkTaskService.selectHyWorkTaskList(qwWorkTask);
        ExcelUtil<HyWorkTask> util = new ExcelUtil<HyWorkTask>(HyWorkTask.class);
        return util.exportExcel(list, "企微任务看板数据");
    }

    /**
     * 获取企微任务看板详细信息
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hyWorkTaskService.selectHyWorkTaskById(id));
    }

    /**
     * 新增企微任务看板
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTask:add')")
    @Log(title = "企微任务看板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HyWorkTask qwWorkTask)
    {
        return toAjax(hyWorkTaskService.insertHyWorkTask(qwWorkTask));
    }

    /**
     * 修改企微任务看板
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTask:edit')")
    @Log(title = "企微任务看板处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HyWorkTask qwWorkTask)
    {
        HyWorkTask task = new HyWorkTask();
        task.setId(qwWorkTask.getId());
        task.setRemark(qwWorkTask.getRemark());
        task.setStatus(1);
        task.setUpdateTime(new Date());
        return toAjax(hyWorkTaskService.updateHyWorkTask(task));
    }

    /**
     * 删除企微任务看板
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTask:remove')")
    @Log(title = "企微任务看板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hyWorkTaskService.deleteHyWorkTaskByIds(ids));
    }
}
