package digital.laboratory.platform.inspection.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.inspection.dto.AssignmentInfoDto;
import digital.laboratory.platform.inspection.entity.AssignmentInfo;
import digital.laboratory.platform.inspection.entity.TaskInfo;
import digital.laboratory.platform.inspection.service.AssignmentInfoService;
import digital.laboratory.platform.inspection.service.TaskInfoService;
import digital.laboratory.platform.inspection.vo.AssignmentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;

/*
 *@title TaskInfoController
 *@description
 *@author xy
 *@version 1.0
 *@create 2023/12/8 11:24
 */
@RestController
@RequestMapping("/assignmentInfo")
@Api(tags = "05-分配信息管理", description = "分配信息管理")
public class AssignmentInfoController {
    @Resource
    private AssignmentInfoService assignmentInfoService;

    //添加接口
    @PostMapping("/addAssignmentInfo")
    @ApiOperation(value = "添加分配信息", notes = "添加分配信息")
    public R addAssignmentInfo(@RequestBody List<AssignmentInfoDto> assignmentInfoDtoList, HttpServletRequest httpServletRequest) {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        assignmentInfoDtoList.forEach(item -> {
            item.setAssignUser(dlpUser.getId());
            item.setAssignUserName(dlpUser.getName());
        });

        List<AssignmentInfo> assignmentInfoList = null;
        try {
            assignmentInfoList = assignmentInfoService.addAssignmentInfo(assignmentInfoDtoList);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }

        if (assignmentInfoList != null) {
            return R.ok(assignmentInfoList, "添加成功");
        } else {
            return R.failed("false", "添加失败");
        }
    }

    //修改接口
    @PostMapping("/cancelAssignmentInfo")
    @ApiOperation(value = "撤销分配信息", notes = "")
    public R cancelAssignmentInfo(@RequestBody List<AssignmentInfo> assignmentInfoList) {
        boolean ret = assignmentInfoService.cancelAssignmentInfo(assignmentInfoList);
        if (ret) {
            return R.ok(ret, "撤销成功");
        } else {
            return R.failed("false", "撤销失败");
        }
    }

    //删除数据
    @GetMapping("/deleteAssignmentInfo")
    @ApiOperation(value = "删除分配信息", notes = "")
    public R deleteAssignmentInfo(String id) {
        Assert.notBlank(id, "参数id不能为空");
        boolean ret = assignmentInfoService.deleteAssignmentInfo(id);
        if (ret) {
            return R.ok("true", "删除成功");
        } else {
            return R.failed("false", "删除成功");
        }
    }

    //显示列表分页
    @GetMapping("/getAssignmentInfoPageList")
    @ApiOperation(value = "获取分页数据", notes = "opCode  0：分配者查询已分配记录  1：接收者查询下发给自己的任务；")
    public R getTaskPageList(Page page, AssignmentInfoDto assignmentInfoDto, HttpServletRequest httpServletRequest) {
        //opCode  0：分配者查询已分配记录  1：接收者查询下发给自己的任务；
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        if (assignmentInfoDto.getOpCode() == 0) {
            assignmentInfoDto.setAssignUser(dlpUser.getId());
        } else {
            assignmentInfoDto.setTestUser(dlpUser.getId());
        }
        return R.ok(assignmentInfoService.getAssignmentInfoPageList(page, assignmentInfoDto), "获取数据成功");
    }

    //显示列表
    @GetMapping("/getAssignmentInfoList")
    @ApiOperation(value = "获取列表", notes = "opCode  0：分配者查询已分配记录  1：接收者查询下发给自己的任务；")
    public R getAssignmentInfoList(AssignmentInfoDto assignmentInfoDto, HttpServletRequest httpServletRequest) {
        Principal principal = httpServletRequest.getUserPrincipal();
        DLPUser dlpUser = (DLPUser) ((OAuth2Authentication) principal).getUserAuthentication().getPrincipal();
        if (assignmentInfoDto.getOpCode() == 0) {
            assignmentInfoDto.setAssignUser(dlpUser.getId());
        } else {
            assignmentInfoDto.setTestUser(dlpUser.getId());
        }
        return R.ok(assignmentInfoService.getAssignmentInfoList(assignmentInfoDto), "获取数据成功");
    }

    /**
     * 根据业务ID批量撤销分配信息
     *
     * @param bussinessIdList
     * @return
     */
    @PostMapping("/cancelByBusiness")
    @ApiOperation(value = "根据业务ID批量撤销分配信息", notes = "根据业务ID批量撤销分配信息")
    public R cancelByBusiness(@RequestBody List<String> bussinessIdList) {
        boolean ret = assignmentInfoService.cancelByBusiness(bussinessIdList);
        if (ret) {
            return R.ok(ret, "撤销成功");
        } else {
            return R.failed("false", "撤销失败");
        }
    }

    /**
     * 查询已分配的类别为委托的任务列表
     *
     * @param page
     * @param keywords 查询参数
     * @return
     */
    @GetMapping("/getCancelEntrustPage")
    @ApiOperation(value = "查询已分配的类别为委托的任务列表", notes = "查询已分配的类别为委托的任务列表")
    public R<IPage<AssignmentInfoVo>> getAssignedEntrustPage(Page page, String keywords) {
        return R.ok(assignmentInfoService.getAssignedEntrustPage(page, keywords), "查询成功!");
    }
}
