package org.dromara.wflow.workflow.controller;

import cn.hutool.core.collection.CollectionUtil;
import org.dromara.biz.domain.BizTask;
import org.dromara.biz.domain.BizTaskWork;
import org.dromara.biz.domain.bo.BizTaskBo;
import org.dromara.biz.domain.bo.BizTaskFeedbackBo;
import org.dromara.biz.domain.bo.BizTaskWorkBo;
import org.dromara.biz.domain.vo.BizTaskFeedbackVo;
import org.dromara.biz.domain.vo.BizTaskWorkVo;
import org.dromara.biz.service.IBizTaskFeedbackService;
import org.dromara.biz.service.IBizTaskWorkService;
import org.dromara.common.core.constant.WflowContants;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.service.UserService;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.dromara.wflow.mapper.WflowSysUserMapper;
import org.dromara.wflow.minio.util.FileUtil;
import org.dromara.wflow.workflow.bean.bo.RuansiTaskFeedbackBo;
import org.dromara.wflow.workflow.bean.bo.RuansiTaskXiaFaBo;
import org.dromara.wflow.workflow.bean.bo.RuansiTaskXiaFaMJBo;
import org.dromara.wflow.workflow.bean.vo.RuansiMissionTaskVo;
import org.dromara.wflow.workflow.service.RuanseeBizTaskWorkService;
import org.dromara.wflow.workflow.service.RuansiMissionTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 待我处理任务列表控制器
 */
@RestController
@RequestMapping("wflow/mission/task")
public class RuansiMissionTaskController {

    @Autowired
    RuansiMissionTaskService ruansiMissionTaskService;

    @Autowired
    WflowSysUserMapper wflowSysUserMapper;

    @Autowired
    private IBizTaskWorkService iBizTaskWorkService;

    @Autowired
    private RuanseeBizTaskWorkService ruansiBizTaskWorkService;

    @Autowired
    private IBizTaskFeedbackService iBizTaskFeedbackService;

    @Autowired
    private ISysOssService sysOssService;

    public static final String default_code = WflowContants.NORMAL_MISSION_DEFAULT_CODE;

    /**
     * 待我审批列表
     * @param pageSize
     * @return
     */
    @GetMapping("todoList")
    public TableDataInfo<RuansiMissionTaskVo> getUserTodoList(@RequestParam(defaultValue = "20") Integer pageSize,
                                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                                              @RequestParam(required = false ) String status,
                                                              @RequestParam(required = false) String title,
                                                              @RequestParam(required = false) String taskType,
                                                              @RequestParam(required = false) String startUserName,
                                                              @RequestParam(required = false) String deptId,
                                                              @RequestParam(required = false) String[] startTimes,
                                                              @RequestParam(required = false) String level) {
        //警号或姓名模糊搜索
        List<Long> userIds = null;
        if(StringUtils.isNotBlank(startUserName)){
            userIds = wflowSysUserMapper.selectUsersLike(startUserName);
            if(CollectionUtil.isEmpty(userIds)){
                TableDataInfo<RuansiMissionTaskVo> build = TableDataInfo.build();
                build.setMsg("未找到该发起人，请重新设置搜索条件");
                build.setRows(Collections.emptyList());
                return build;
            }
        }
        return ruansiMissionTaskService.getUserTodoList2(pageSize, pageNum, userIds,default_code,startTimes, status, title, taskType,deptId, level);
    }

    /**
     * 我已审批列表
     * @param pageSize
     * @return
     */
    @GetMapping("idoList")
    public TableDataInfo<RuansiMissionTaskVo> getUserITodoList(@RequestParam(defaultValue = "20") Integer pageSize,
                                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                                              @RequestParam(required = false ) String status,
                                                              @RequestParam(required = false) String title,
                                                              @RequestParam(required = false) String taskType,
                                                              @RequestParam(required = false) String startUserName,
                                                              @RequestParam(required = false) String deptId,
                                                              @RequestParam(required = false) String[] startTimes,
                                                               @RequestParam(required = false) String level) {
        //警号或姓名模糊搜索
        List<Long> userIds = null;
        if(StringUtils.isNotBlank(startUserName)){
            userIds = wflowSysUserMapper.selectUsersLike(startUserName);
            if(CollectionUtil.isEmpty(userIds)){
                TableDataInfo<RuansiMissionTaskVo> build = TableDataInfo.build();
                build.setMsg("未找到该发起人，请重新设置搜索条件");
                build.setRows(Collections.emptyList());
                return build;
            }
        }
        return ruansiMissionTaskService.getUserITodoList(pageSize, pageNum, userIds,default_code,startTimes, status, title, taskType,deptId, level);
    }


    /**
     * 待我办理列表
     * @param pageSize          每页显示条数
     * @param pageNum           页数
     * @param taskType          任务类型 1案件 2警情 3专项 4其他
     * @param startUserName     发起人
     * @param deptId            发起单位
     * @param title             任务标题
     * @param startTimes        发起时间
     * @param level             任务级别
     */
    @GetMapping("toHandleList")
    public TableDataInfo<BizTask> getToHandleList(@RequestParam(defaultValue = "20") Integer pageSize,
                                                  @RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(required = false ) String taskType,
                                                  @RequestParam(required = false ) String startUserName,
                                                  @RequestParam(required = false) String deptId,
                                                  @RequestParam(required = false) String title,
                                                  @RequestParam(required = false) String[] startTimes,
                                                  @RequestParam(required = false) String level){
        return ruansiMissionTaskService.getToHandleList(pageSize, pageNum, taskType, startUserName, deptId, title, startTimes, level);
    }

    /**
     * 已办理列表
     * @param pageSize          每页显示条数
     * @param pageNum           页数
     * @param taskType          任务类型 1案件 2警情 3专项 4其他
     * @param startUserName     发起人
     * @param deptId            发起单位
     * @param title             任务标题
     * @param startTimes        发起时间
     */
    @GetMapping("alreadyHandledList")
    public TableDataInfo<BizTask> alreadyHandledList(@RequestParam(defaultValue = "20") Integer pageSize,
                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false ) String taskType,
                                                   @RequestParam(required = false ) String startUserName,
                                                   @RequestParam(required = false) String deptId,
                                                   @RequestParam(required = false) String title,
                                                   @RequestParam(required = false) String[] startTimes,
                                                   @RequestParam(required = false) String level){
        return ruansiMissionTaskService.alreadyHandledList(pageSize, pageNum, taskType, startUserName, deptId, title, startTimes, level);
    }


    /**
     * 中心下发任务
     * @param bo
     * @return
     */
    @PostMapping("/xiafa")
    private R<Void> xiafa(@RequestBody RuansiTaskXiaFaBo bo){
        return ruansiBizTaskWorkService.xiafa(bo) ? R.ok() : R.fail();
    }

    /**
     * 情指中下发任务，线索核查
     * @param bo
     * @return
     */
    @PostMapping("/xiafaByQingZhi")
    private R<Void> xiafaByQingZhi(@RequestBody RuansiTaskXiaFaBo bo){

        return ruansiBizTaskWorkService.xiafaByQingZhi(bo) ? R.ok() : R.fail();
    }

    /**
     * 民警签收任务
     * @param
     * @return
     */
    @PostMapping("/qianshou")
    private R<Void> qianshou(@RequestBody BizTaskBo bizTaskBo){
        String taskNo = bizTaskBo.getTaskNo();
        if(StringUtils.isBlank(taskNo)){
            return R.fail("任务编号不能为空");
        }
        return ruansiBizTaskWorkService.qianshou(taskNo) ? R.ok() : R.fail();
    }

    /**
     * 民警反馈任务
     * @param bo
     * @return
     */
    @PostMapping("/feedback")
    private R<Void> feedback(@RequestBody RuansiTaskFeedbackBo bo){

        return ruansiBizTaskWorkService.feedback(bo) ? R.ok() : R.fail();
    }

    /**
     * 下发任务给民警
     * @return
     */
    @PostMapping("/xiafaMJ")
    private R<Void> xiafaMJ(@RequestBody RuansiTaskXiaFaMJBo bo){
        return ruansiBizTaskWorkService.xiafaMJ(bo) ? R.ok() : R.fail();
    }

    /**
     * 获取任务详情
     * @param workId
     * @return
     */
    @GetMapping("/bizWork/{workId}")
    public R<BizTaskWorkVo> getTaskWorkInfo(@PathVariable String workId){
        BizTaskWorkVo bizTaskWorkVo = iBizTaskWorkService.queryById(Long.valueOf(workId));
        String ossId = bizTaskWorkVo.getOssId();
        if(StringUtils.isNotBlank(ossId)){
            String[] split = ossId.split(",");
            List<String> fileUrls = new ArrayList<>();
            List<String> imageUrls = new ArrayList<>();
            for (String s : split) {
                SysOssVo sysOss = sysOssService.getById(Long.valueOf(s));
                if(FileUtil.isImage(sysOss.getOriginalName())){
                    imageUrls.add(sysOss.getUrl());
                }else{
                    fileUrls.add(sysOss.getUrl());
                }
            }
            bizTaskWorkVo.setImageUrls(imageUrls);
            bizTaskWorkVo.setFileUrls(fileUrls);
        }
        BizTaskFeedbackBo bizTaskFeedbackBo = new BizTaskFeedbackBo();
        bizTaskFeedbackBo.setWorkId(Long.valueOf(workId));
        List<BizTaskFeedbackVo> bizTaskFeedbackVos = iBizTaskFeedbackService.queryList(bizTaskFeedbackBo);
        if(CollectionUtil.isEmpty(bizTaskFeedbackVos)){
            bizTaskFeedbackVos = Collections.emptyList();
        }
        bizTaskWorkVo.setFeedbackList(bizTaskFeedbackVos);
        return R.ok(bizTaskWorkVo);
    }

    /**
     * 中心签收反馈单
     * @return
     */
    @PostMapping("/qianshouFeedBack")
    private R<Void> qianshouFeedBack(@RequestBody BizTaskWorkBo bizTaskWorkBo){
        Long workId = bizTaskWorkBo.getId();
        if(Objects.isNull(workId)){
            return R.fail("协作单id不能为空");
        }
        return ruansiBizTaskWorkService.qianshouFeedBack(bizTaskWorkBo) ? R.ok() : R.fail();
    }

    /**
     * 中心拒绝反馈单
     * @return
     */
    @PostMapping("/refuseFeedBack")
    private R<Void> refuseFeedBack(@RequestBody BizTaskWorkBo bizTaskWorkBo){
        Long workId = bizTaskWorkBo.getId();
        if(Objects.isNull(workId)){
            return R.fail("协作单id不能为空");
        }
        return ruansiBizTaskWorkService.refuseFeedBack(bizTaskWorkBo) ? R.ok() : R.fail();
    }

    /**
     * 办结
     * @return
     */
    @PostMapping("/allDone")
    private R<Void> allDone(@RequestBody BizTaskBo bizTaskBo){
        String taskNo = bizTaskBo.getTaskNo();
        if(StringUtils.isBlank(taskNo)){
            return R.fail("流程id不能为空");
        }
        return ruansiBizTaskWorkService.allDone(taskNo) ? R.ok() : R.fail();
    }

    @PostMapping("/getWorkList")
    public R<List<BizTaskWorkVo>> getWorkList(@RequestBody BizTaskWorkBo bizTaskWorkBo){
        String taskNo = bizTaskWorkBo.getTaskNo();
        if(StringUtils.isBlank(taskNo)){
            return R.fail("流程id不能为空");
        }
        List<BizTaskWorkVo> bizTaskWorkVos = iBizTaskWorkService.queryList(bizTaskWorkBo);
        return R.ok(bizTaskWorkVos);
    }

    @PostMapping("/redo")
    public R<Void> add(@RequestBody BizTaskBo bizTaskBo) {
        boolean success = ruansiMissionTaskService.redo(bizTaskBo);
        return success ? R.ok() : R.fail();
    }

    @GetMapping("/getBizTaskAndFeedbackByFeedbackId")
    public R<Map<String, Object>> getBizTaskAndFeedbackByFeedbackId(@RequestParam String taskNo,
                                                                    @RequestParam String id) {
        BizTaskFeedbackBo bizTaskFeedbackBo = new BizTaskFeedbackBo();
        bizTaskFeedbackBo.setTaskNo(taskNo);
        bizTaskFeedbackBo.setId(Long.valueOf(id));
        Map<String, Object> map = ruansiMissionTaskService.getBizTaskAndFeedbackByFeedbackId(bizTaskFeedbackBo);
        return  R.ok(map);
    }
}
