package com.hotent.enterpriseDeclare.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.model.CommonResult;
import com.hotent.base.util.StringUtil;
import com.hotent.constant.NodeConstant;
import com.hotent.enterpriseDeclare.req.CostTaskPageReq;
import com.hotent.enterpriseDeclare.req.CostTaskReviewPageReq;
import com.hotent.project.manager.*;
import com.hotent.project.model.CostProjectTask;
import com.hotent.project.model.CostProjectTaskNode;
import com.hotent.sys.persistence.manager.DataDictManager;
import com.hotent.sys.persistence.model.DataDict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Api(tags = "成本监审任务审核")
@RestController
@RequestMapping("/api/enterprise/reviewTask/")
@ApiGroup(group = {ApiGroupConsts.GROUP_COST})
public class CostAuditReviewController {

    @Autowired
    private CostProjectTaskManager costProjectTaskManager;

    @Autowired
    private CostProjectTaskNodeManager costProjectTaskNodeManager;

    @Autowired

    @Resource
    DataDictManager dataDictManager;


    /**
     * 分页查询待审核任务列表
     *
     * @param req 分页查询请求参数
     * @return 待审核任务分页列表
     */
    @PostMapping(value = "/pageList")
    @ApiOperation(value = "分页查询待审核任务列表", httpMethod = "POST", notes = "分页查询待审核任务列表")
    public CommonResult<IPage<CostProjectTask>> pageList(@RequestBody CostTaskReviewPageReq req) throws Exception {
        IPage<CostProjectTask> pageResult = costProjectTaskManager.pageListForReviewTask(req);
        return CommonResult.<IPage<CostProjectTask>>ok().value(pageResult);
    }


    /**
     * 分页查询待审核任务列表子任务
     *
     * @param req 分页查询请求参数
     * @return 待审核任务分页列表
     */
    @PostMapping(value = "/pageListChild")
    @ApiOperation(value = "分页查询待审核任务列表子任务", httpMethod = "POST", notes = "分页查询待审核任务列表子任务")
    public CommonResult<IPage<CostProjectTask>> pageListChild(@RequestBody CostTaskReviewPageReq req) {
        IPage<CostProjectTask> pageResult = costProjectTaskManager.pageListForReviewTaskChild(req);
        return CommonResult.<IPage<CostProjectTask>>ok().value(pageResult);
    }

    /**
     * 分页查询办结监审任务列表
     *
     * @param req 分页查询请求参数
     * @return 分页查询办结监审任务列表
     */
    @PostMapping(value = "/completedPageList")
    @ApiOperation(value = "分页查询办结监审任务列表", httpMethod = "POST", notes = "分页查询办结监审任务列表")
    public CommonResult<IPage<CostProjectTask>> completedPageList(@RequestBody CostTaskReviewPageReq req) throws Exception {
        req.setCurrentNode(NodeConstant.guidang.getNodeKey());
        IPage<CostProjectTask> pageResult = costProjectTaskManager.pageListForReviewTask(req);
        return CommonResult.<IPage<CostProjectTask>>ok().value(pageResult);
    }

    /**
     * 分页查询档案管理任务列表
     *
     * @param req 分页查询请求参数
     * @return 档案管理任务分页列表
     */
    @PostMapping(value = "/archivePageList")
    @ApiOperation(value = "分页查询档案管理任务列表", httpMethod = "POST", notes = "分页查询档案管理任务列表（只查询主任务，不查询子任务）")
    public CommonResult<IPage<CostProjectTask>> archivePageList(@RequestBody CostTaskReviewPageReq req) throws Exception {
        IPage<CostProjectTask> pageResult = costProjectTaskManager.pageListForReviewTask(req);
        return CommonResult.<IPage<CostProjectTask>>ok().value(pageResult);
    }


    @PostMapping(value = "/getProcessBtn")
    @ApiOperation(value = "获取流程按钮", httpMethod = "POST", notes = "获取流程按钮")
    public CommonResult<List<java.util.Map<String, Object>>> getProcessBtn(@RequestBody CostTaskPageReq req) {
        CostProjectTaskNode one = costProjectTaskNodeManager.getOne(
                new LambdaQueryWrapper<CostProjectTaskNode>()
                        .eq(CostProjectTaskNode::getTaskId, req.getTaskId())
                        .eq(CostProjectTaskNode::getProcessNodeKey, req.getProcessNodeKey())
        );

        if (one == null || StringUtil.isEmpty(one.getStatus())) {
            return CommonResult.<List<java.util.Map<String, Object>>>ok().value(new java.util.ArrayList<>());
        }

        String status = one.getStatus();
        String[] split = status.split(",");
        List<java.util.Map<String, Object>> buttonList = new java.util.ArrayList<>();

        for (String s : split) {
            if (StringUtil.isNotEmpty(s.trim())) {
                String key = s.trim();
                DataDict byDictKey = dataDictManager.getByDictKey("1977549156280991744", key);

                java.util.Map<String, Object> buttonMap = new java.util.HashMap<>();
                buttonMap.put("key", key);

                if (byDictKey != null && StringUtil.isNotEmpty(byDictKey.getName())) {
                    buttonMap.put("value", byDictKey.getName());
                } else {
                    buttonMap.put("value", key);
                }

                buttonList.add(buttonMap);
            }
        }

        return CommonResult.<List<java.util.Map<String, Object>>>ok().value(buttonList);
    }


    @PostMapping(value = "/doProcessBtn")
    @ApiOperation(value = "操作流程按钮", httpMethod = "POST", notes = "流程按钮操作")
    @org.springframework.transaction.annotation.Transactional
    public CommonResult<String> doProcessBtn(@RequestBody CostTaskPageReq req) {
        if (StringUtil.isEmpty(req.getTaskId()) || StringUtil.isEmpty(req.getKey())) {
            return CommonResult.<String>error().message("任务ID和操作key不能为空");
        }
        return CommonResult.<String>ok().value(costProjectTaskManager.doProcessBtn(req));
    }




}
