package digital.laboratory.platform.entrustment.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.entrustment.dto.EntrustAlterApplyApproveDTO;
import digital.laboratory.platform.entrustment.dto.EntrustAlterApplyDTO;
import digital.laboratory.platform.entrustment.query.EntrustAlterApplyQuery;
import digital.laboratory.platform.entrustment.service.EntrustAlterApplyService;
import digital.laboratory.platform.entrustment.vo.EntrustAlterApplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 申请修改委托消息相关接口
 *
 * @author chenjiangbao
 * @describe 申请修改委托消息相关接口 前端控制器
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/papp/entrustAlterApply")
@Api(tags = "016-申请修改委托消息相关接口")
public class EntrustAlterApplyController {

    private final EntrustAlterApplyService entrustAlterApplyService;

    @ApiOperation(value = "查询所有申请修改委托消息分页接口", notes = "查询所有申请修改委托消息分页接口")
    @PostMapping("/page")
    @PreAuthorize("@pms.hasPermission('EntrustAlterApplyQuery')") // 委托申请修改消息查询权限
    public R page(@RequestBody EntrustAlterApplyQuery query) {
        IPage<EntrustAlterApplyVO> page = entrustAlterApplyService.voPage(query);
        return R.ok(page);
    }

    @ApiOperation(value = "保存或提交委托申请修改消息", notes = "保存委托申请修改消息")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('EntrustAlterApplySubmit')") // 委托申请修改消息保存提交权限
    public R save(@RequestBody @Valid EntrustAlterApplyDTO dto) {
        EntrustAlterApplyVO vo = null;
        try {
            vo = entrustAlterApplyService.save(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        }
        return R.ok(vo);
    }

    @ApiOperation(value = "修改委托申请修改消息", notes = "修改委托申请修改消息")
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('EntrustAlterApplyEdit')") // 委托申请修改消息修改权限
    public R update(@RequestBody @Valid EntrustAlterApplyDTO dto) {
        Boolean success = entrustAlterApplyService.update(dto);
        return R.ok(success).setMsg(success ? "修改成功" : "修改失败");
    }

    @ApiOperation(value = "审核提交委托申请修改消息", notes = "审核提交委托申请修改消息")
    @PutMapping("/approve")
    @PreAuthorize("@pms.hasPermission('EntrustAlterApplyApprove')") // 委托申请修改消息审核权限
    public R approve(@RequestBody @Valid EntrustAlterApplyApproveDTO approveDTO) {
        Boolean success = entrustAlterApplyService.approve(approveDTO);
        return R.ok(success).setMsg(success ? "审核成功" : "审核失败");
    }

    @ApiOperation(value = "删除委托申请修改消息", notes = "删除委托申请修改消息， 只能删除为提交状态的消息")
    @PostMapping("/delete")
    @PreAuthorize("@pms.hasPermission('EntrustAlterApplyDelete')") // 委托申请修改消息删除权限
    public R delete(@RequestBody List<String> ids) {
        Boolean success = entrustAlterApplyService.delete(ids);
        return R.ok(success).setMsg(success ? "审核成功" : "审核失败");
    }


}
