package org.springblade.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springblade.business.entity.UserOpinionFlow;
import org.springblade.business.service.IUserOpinionFlowService;
import org.springblade.business.service.IUserOpinionService;
import org.springblade.business.vo.BusinessUserOpinionVO;
import org.springblade.business.vo.UserOpinionFlowVO;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/businessUserOpinion")
@Api(tags = "维护人员工单列表")
public class BusinessUserOpinionController {

    private final IUserOpinionService userOpinionService;

    private final IUserOpinionFlowService userOpinionFlowService;

    /**
     * 获取所有工单及处理进度
     * @param query 分页信息
     * @return 结果
     */
    @GetMapping("/queryBusinessUserOpinionListAll")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "所有工单处理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "分页信息", required = true)
    })
    public R<IPage<BusinessUserOpinionVO>> queryBusinessUserOpinionListAll(@RequestBody Query query){
        return R.data(this.userOpinionService.queryBusinessUserOpinionListAll(query));
    }

    /**
     * 业务人员提交环节操作
     * @return 结果
     */
    @PostMapping("/manageUserOperationStatus")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "业务人员提交环节操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userOpinionId", value = "工单ID", required = true),
            @ApiImplicitParam(name = "newNumber", value = "当前流程次数", required = true),
            @ApiImplicitParam(name = "currentLinkId", value = "当前环节ID", required = true)
    })
    public R<Boolean> manageUserOperationStatus(@RequestParam String userOpinionId, @RequestParam Integer newNumber, @RequestParam String currentLinkId){
        if(StringUtils.isEmpty(currentLinkId)){
            return R.data(-1, false, "缺少currentLinkId参数");
        }
        //获取当前操作用户
        BladeUser user = AuthUtil.getUser();
        //获取当前最新流程
        List<UserOpinionFlowVO> flowVOS = this.userOpinionFlowService.queryCurrentUserOpinionFlowByUserOpinionId(userOpinionId, newNumber);
        //找到当前环节的下一环节
        Long nextLink = null, nextLinkEnd = null;
        for(int i = 0, l = flowVOS.size(); i < l; i ++){
            if(new Long(currentLinkId).equals(flowVOS.get(i).getId())){
                //找到下一个环节ID
                switch (i){
                    case 1:
                        //说明当前环节是提交"进入人工处理环节"操作
                        nextLink = flowVOS.get(i + 1).getId();
                        break;
                    case 2:
                        //说明当前环节是提交"已解决"操作
                        nextLinkEnd = flowVOS.get(i + 1).getId();
                        break;
                    default:
                        break;
                }
                break;
            }
        }
        //将当前环节更改为已完成
        boolean result = this.userOpinionFlowService.update(Wrappers.<UserOpinionFlow>lambdaUpdate().set(UserOpinionFlow::getIsCurrent, 2).set(UserOpinionFlow::getUpdateUser, user.getUserId()).set(UserOpinionFlow::getUpdateTime, new Date()).eq(UserOpinionFlow::getId, currentLinkId));
        if(result){
            if(nextLink != null && StringUtils.isNotEmpty(String.valueOf(nextLink))){
                //提交"进入人工处理环节"操作
                result = this.userOpinionFlowService.update(Wrappers.<UserOpinionFlow>lambdaUpdate().set(UserOpinionFlow::getIsCurrent, 1).eq(UserOpinionFlow::getId, nextLink));
            }
            if(nextLinkEnd != null && StringUtils.isNotEmpty(String.valueOf(nextLinkEnd))){
                //提交"已解决"操作
                result = this.userOpinionFlowService.update(Wrappers.<UserOpinionFlow>lambdaUpdate().set(UserOpinionFlow::getIsCurrent, 2).eq(UserOpinionFlow::getId, nextLinkEnd));
            }
            return R.data(result);
        }

        return R.data(false);
    }

    /**
     * 获取分配给当前登录用户的工单记录
     * @return 结果集
     */
    @GetMapping("/queryBusinessUserOpinionList")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "获取分配给当前登录用户的工单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true),
            @ApiImplicitParam(name = "size", value = "当前页显示数量", required = true)
    })
    public R<IPage<BusinessUserOpinionVO>> queryBusinessUserOpinionList(@RequestBody Query query){
        BladeUser user = AuthUtil.getUser();
        //获取推送给当前用户的工单信息
        return R.data(this.userOpinionService.queryBusinessUserOpinionList(String.valueOf(user.getUserId()),query));
    }

}
