package com.fs.company.controller.qw;

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.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.service.impl.CompanyDeptServiceImpl;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.framework.security.LoginUser;
import com.fs.framework.service.TokenService;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.domain.QwWorkTask;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.qw.param.QwWorkTaskListParam;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.service.IQwWorkTaskService;
import com.fs.qw.vo.QwWorkTaskAllListVO;
import com.fs.qw.vo.QwWorkTaskListVO;
import com.fs.qwApi.domain.QwExternalContactRemarkResult;
import com.fs.qwApi.param.QwExternalContactRemarkParam;
import com.fs.qwApi.service.QwApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 企微任务看板Controller
 *
 * @author fs
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/qw/QwWorkTaskNew")
public class QwWorkTaskNewController extends BaseController
{
    @Autowired
    private IQwWorkTaskService qwWorkTaskService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private FsCourseWatchLogMapper fsCourseWatchLogMapper;

    @Autowired
    private CompanyDeptServiceImpl companyDeptService;

    /**
     * 查询企微任务看板列表
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwWorkTaskListParam qwWorkTask)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwWorkTask.setCompanyId(loginUser.getCompany().getCompanyId());
        qwWorkTask.setCompanyUserId(loginUser.getUser().getUserId());
        List<QwWorkTaskListVO> list = qwWorkTaskService.selectQwWorkTaskListVO(qwWorkTask);

        for (QwWorkTaskListVO qwWorkTaskListVO : list) {
            qwWorkTaskListVO.setLogs(fsCourseWatchLogMapper.selectFsCourseWatchLog7DayByExtId(qwWorkTaskListVO.getExtId()));
        }
        return getDataTable(list);
    }

    /**
     * 查询企微任务部门催课看板列表
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:deptList')")
    @GetMapping("/deptList")
    public TableDataInfo deptList(QwWorkTaskListParam qwWorkTask)
    {

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

        List<Long> combinedList = new ArrayList<>();
        //本部门
        Long deptId = loginUser.getUser().getDeptId();
        if (deptId!=null){
            combinedList.add(deptId);
        }
        //本部门的下级部门
        List<Long> deptList = companyDeptService.selectCompanyDeptByParentId(deptId);
        if (!deptList.isEmpty()){
            combinedList.addAll(deptList);
        }

        qwWorkTask.setCuDeptIdList(combinedList);
        qwWorkTask.setUserType(loginUser.getUser().getUserType());


        startPage();
        List<QwWorkTaskListVO> list = qwWorkTaskService.selectQwWorkTaskListVO(qwWorkTask);
        for (QwWorkTaskListVO qwWorkTaskListVO : list) {
            qwWorkTaskListVO.setLogs(fsCourseWatchLogMapper.selectFsCourseWatchLog7DayByExtId(qwWorkTaskListVO.getExtId()));
        }
        return getDataTable(list);
    }

    @GetMapping("/glList")
    public TableDataInfo glList(QwWorkTaskListParam qwWorkTask)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwWorkTask.setCompanyId(loginUser.getCompany().getCompanyId());
        List<QwWorkTaskListVO> list = qwWorkTaskService.selectQwWorkTaskListVO(qwWorkTask);
        for (QwWorkTaskListVO qwWorkTaskListVO : list) {
            qwWorkTaskListVO.setLogs(fsCourseWatchLogMapper.selectFsCourseWatchLog7DayByExtId(qwWorkTaskListVO.getExtId()));
        }
        return getDataTable(list);
    }

    @GetMapping("/allList")
    public TableDataInfo allList(QwWorkTaskListParam qwWorkTask)
    {
        startPage();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwWorkTask.setCompanyId(loginUser.getCompany().getCompanyId());
        if (qwWorkTask.getSTime()==null||qwWorkTask.getETime()==null){
            return new TableDataInfo();
        }
        List<QwWorkTaskAllListVO> list = qwWorkTaskService.selectQwWorkTaskAllListVO(qwWorkTask);

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

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

    /**
     * 新增企微任务看板
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:add')")
    @Log(title = "企微任务看板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwWorkTask qwWorkTask)
    {
        return toAjax(qwWorkTaskService.insertQwWorkTask(qwWorkTask));
    }
    @Autowired
    QwApiService qwApiService;
    @Autowired
    IQwExternalContactService qwExternalContactService;
    @Autowired
    QwExternalContactMapper qwExternalContactMapper;
    /**
     * 修改企微任务看板
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:edit')")
    @Log(title = "企微任务看板处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwWorkTask qwWorkTask)
    {
        QwWorkTask task = new QwWorkTask();
        task.setId(qwWorkTask.getId());
        task.setStatus(1);
        task.setTrackType(qwWorkTask.getTrackType());
        task.setDescription(qwWorkTask.getDescription());
        task.setUpdateTime(new Date());
        if (task.getDescription()!=null&& !task.getDescription().isEmpty()){

            QwExternalContact qwExternalContact = qwExternalContactService.selectQwExternalContactById(qwWorkTask.getExtId());
            if (qwExternalContact!=null){
                QwExternalContactRemarkParam param = new QwExternalContactRemarkParam();
                param.setUserid(qwExternalContact.getUserId());
                param.setExternal_userid(qwExternalContact.getExternalUserId());
                param.setDescription(task.getDescription());

                QwExternalContactRemarkResult qwExternalContactRemarkResult = qwApiService.externalcontactRemark(param, qwExternalContact.getCorpId());
                logger.info("QwExternalContactRemarkResult206：" + qwExternalContactRemarkResult);
                if (qwExternalContactRemarkResult.getErrcode() == 0) {
                    QwExternalContact ext = new QwExternalContact();
                    ext.setId(qwExternalContact.getId());
                    ext.setDescription(task.getDescription());
                    qwExternalContactMapper.updateQwExternalContact(ext);
                }
            }
        }
        return toAjax(qwWorkTaskService.updateQwWorkTask(task));
    }
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:edit')")
    @Log(title = "企微任务看板处理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit2")
    public AjaxResult edit2(@RequestBody QwWorkTask qwWorkTask)
    {
        QwWorkTask task = new QwWorkTask();
        task.setId(qwWorkTask.getId());
        task.setStatus(1);
        task.setUpdateTime(new Date());
        task.setTrackType(2);
        return toAjax(qwWorkTaskService.updateQwWorkTask(task));
    }
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:edit')")
    @Log(title = "企微任务看板处理", businessType = BusinessType.UPDATE)
    @PutMapping("/edit3")
    public AjaxResult edit3(@RequestBody QwWorkTask qwWorkTask)
    {
        QwWorkTask task = new QwWorkTask();
        task.setId(qwWorkTask.getId());
        task.setStatus(1);
        task.setUpdateTime(new Date());
        task.setTrackType(3);
        return toAjax(qwWorkTaskService.updateQwWorkTask(task));
    }
    /**
     * 删除企微任务看板
     */
    @PreAuthorize("@ss.hasPermi('qw:QwWorkTaskNew:remove')")
    @Log(title = "企微任务看板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qwWorkTaskService.deleteQwWorkTaskByIds(ids));
    }
}
