package com.willalp.web.controller.clockingin;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.flow.domain.FileApproval;
import com.willalp.flow.domain.HsFlowTask;
import com.willalp.flow.domain.vo.FileApprovalVo;
import com.willalp.flow.service.HsFlowEngineService;
import com.willalp.flow.service.IFileApprovalService;
import com.willalp.msg.service.IHsAppUserMsgService;
import com.willalp.system.service.ISysConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 文件审批业务Controller
 *
 * @author willalp
 * @date 2022-11-30
 */
@RestController
@RequestMapping("/flow/accessory")
public class FileApprovalController extends BaseController {
    @Resource
    private IFileApprovalService fileApprovalService;

    @Resource
    private HsFlowEngineService flowEngineService;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private IHsAppUserMsgService userMsgService;


    /**
     * 查询文件审批业务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FileApproval fileApproval) {
        startPage();
        List<FileApproval> list = fileApprovalService.selectFileApprovalList(fileApproval);
        return getDataTable(list);
    }

    /**
     * 导出文件审批业务列表
     */
    @Log(title = "文件审批业务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FileApproval fileApproval) {
        List<FileApproval> list = fileApprovalService.selectFileApprovalList(fileApproval);
        ExcelUtil<FileApproval> util = new ExcelUtil<FileApproval>(FileApproval.class);
        return util.exportExcel(list, "文件审批业务数据");
    }

    /**
     * 获取文件审批业务详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(fileApprovalService.getById(id));
    }

    /**
     * 新增文件审批业务
     */
    @Log(title = "文件审批业务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileApprovalVo fileApprovalVo) {
        FileApproval fileApproval = new FileApproval();
        fileApproval.setFileList(JSON.toJSONString(fileApprovalVo.getFileList()));
        fileApproval.setImgList(JSON.toJSONString(fileApprovalVo.getImgList()));
        fileApproval.setParams(fileApprovalVo.getParams());
        fileApproval.setTitle(fileApprovalVo.getTitle());
        fileApproval.setApplyUserId(getUsername());
        fileApproval.setApplyUserName(getLoginUser().getUser().getNickName());
        fileApproval.setCreateBy(SecurityUtils.getUsername());
        fileApproval.setCreateTime(DateUtils.getNowDate());
        String idStr = IdWorker.getIdStr();
        fileApproval.setId(idStr);
        flowEngineService.submitTask(new HsFlowTask(idStr, idStr, sysConfigService.selectConfigByKey("file_approval_flow_run_key")), fileApproval.getParams());
        StringBuffer content = new StringBuffer();
        content.append("<p>标题：").append(fileApproval.getTitle()).append("</p>")
                .append("<p>申请人：").append(fileApproval.getApplyUserName()).append("</p>");
        //异步生成审批消息列表
        userMsgService.saveApprovalMsgAndPush("5",
                fileApproval.getTitle(), content.toString(), idStr,
                String.valueOf(fileApproval.getParams().get("candidate")), fileApproval.getApplyUserName(), fileApproval.getCreateTime());
        return toAjax(fileApprovalService.save(fileApproval));
    }

    /**f
     * 修改文件审批业务
     */
    @Log(title = "文件审批业务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileApproval fileApproval) {
        fileApproval.setUpdateBy(SecurityUtils.getUsername());
        fileApproval.setUpdateTime(DateUtils.getNowDate());
        return toAjax(fileApprovalService.updateById(fileApproval));
    }

    /**
     * 删除文件审批业务
     */
    @Log(title = "文件审批业务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(fileApprovalService.removeByIds(Arrays.asList(ids)));
    }
}
