package com.fs.qw.controller;

import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
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.FsUserCourseMapper;
import com.fs.course.mapper.FsUserCourseVideoMapper;
import com.fs.his.vo.OptionsVO;
import com.fs.qw.domain.QwSopUpdateStatus;
import com.fs.qw.service.IQwUserService;
import com.fs.sop.domain.QwSop;
import com.fs.sop.params.QwSopAutoTime;
import com.fs.sop.params.QwSopEditQwUserParam;
import com.fs.sop.service.IQwSopService;
import com.fs.sop.vo.SopVoiceListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 企微sopController
 *
 * @author fs
 * @date 2024-07-31
 */
@RestController
@RequestMapping("/qw/sop")
public class QwSopController extends BaseController
{
    @Autowired
    private IQwSopService qwSopService;

    @Autowired
    private IQwUserService qwUserService;
    @Autowired
    private TokenService tokenService;

    @Autowired
    private FsUserCourseMapper fsUserCourseMapper;
    @Autowired
    private FsUserCourseVideoMapper fsUserCourseVideoMapper;
    /**
     * 查询企微sop列表
     */
    @PreAuthorize("@ss.hasPermi('qw:sop:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwSop qwSop)
    {
        startPage();
        List<QwSop> list = qwSopService.selectQwSopList(qwSop);
        return getDataTable(list);
    }

    /**
     * 查询aiChatsop列表
     */
    @GetMapping("/listAiChatSop")
    public TableDataInfo listAiChatSop(QwSop qwSop)
    {
        startPage();
        List<QwSop> list = qwSopService.selectAiChatQwSopList(qwSop);
        return getDataTable(list);
    }
    @GetMapping("/courseList")
    public R courseList()
    {
        List<OptionsVO> 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);
    }
    /**
     * 查询企微sop列表-我的
     */
    @PreAuthorize("@ss.hasPermi('qw:sop:myList')")
    @GetMapping("/myList")
    public TableDataInfo myList(QwSop qwSop)
    {
        startPage();
        List<QwSop> list = qwSopService.selectQwSopMyList(qwSop);
        return getDataTable(list);
    }
    /**
     * 导出企微sop列表
     */
    @PreAuthorize("@ss.hasPermi('qw:sop:export')")
    @Log(title = "企微sop", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwSop qwSop)
    {
        List<QwSop> list = qwSopService.selectQwSopList(qwSop);
        ExcelUtil<QwSop> util = new ExcelUtil<QwSop>(QwSop.class);
        return util.exportExcel(list, "企微sop数据");
    }

    /**
     * 获取企微sop详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(qwSopService.selectQwSopById(id));
    }

    /**
     * 新增企微sop
     */
    @PreAuthorize("@ss.hasPermi('qw:sop:add')")
    @Log(title = "企微sop", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwSop qwSop)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwSop.setCreateBy(loginUser.getUser().getNickName());
        qwSop.setCreateTime(sdf.format(new Date()));

        return toAjax(qwSopService.insertQwSop(qwSop));

    }

    /**
     * 修改企微sop
     */
    @PreAuthorize("@ss.hasPermi('qw:sop:edit')")
    @Log(title = "修改企微sop", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody QwSop qwSop)
    {
        return qwSopService.updateQwSop(qwSop);
    }


    /**
     * 暂停企微sop
     */
    @Log(title = "暂停企微sop", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSopStatus")
    @RepeatSubmit
    public R  updateSopStatus(@RequestBody QwSopUpdateStatus param)
    {
        return qwSopService.updateSopStatus(param);
    }

    /**
     * 修改企微sop自动创建时间
     */
    @Log(title = "企微sop自动创建时间", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAutoSopTime")
    @RepeatSubmit
    public AjaxResult updateAutoSopTime(@RequestBody QwSopAutoTime param)
    {
        return toAjax(qwSopService.updateAutoSopTime(param));
    }

    /**
     * 删除企微sop
     */
    @PreAuthorize("@ss.hasPermi('qw:sop:remove')")
    @Log(title = "企微sop", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(qwSopService.deleteQwSopByIds(ids));
    }


    /**
     * 批量执行SOP
     */
    @GetMapping(value = "/updateStatus/{ids}")
    public R batchDoSop(@PathVariable String[] ids)
    {
        return qwSopService.updateStatusQwSopByIds(ids);
    }

    /**
     * 修改sop员工
     */
    @PostMapping( "/updateSopQwUser")
    @Log(title = "修改sop员工", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('qw:sop:updateSopQwUser')")
    public R updateSopQwUser(@RequestBody QwSopEditQwUserParam param)
    {
        return qwSopService.updateSopQwUser(param);
    }

    /**
    * 服务器图片地址 转 企业微信地址
    */
    @PostMapping(value = "/updateMediaImage")
    public R updateMediaImage(@RequestParam("url") String url) throws IOException {

        String  cropId=null;
        return qwSopService.updateMediaImage(url,cropId);
    }
    /**
    * 服务器图片地址 转 企业微信地址
    */
    @GetMapping(value = "/getSopVoiceList")
    public TableDataInfo getSopVoiceList(String id) {
        startPage();
        List<SopVoiceListVo> list = qwSopService.getSopVoiceList(id);
        return getDataTable(list);
    }
}
