package digital.laboratory.platform.imr.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import digital.laboratory.platform.common.aop.annotation.DlpResultProc;
import digital.laboratory.platform.common.core.exception.ValidateCodeException;
import digital.laboratory.platform.common.core.util.R;
import digital.laboratory.platform.common.mybatis.security.service.DLPUser;
import digital.laboratory.platform.common.security.util.SecurityUtils;
import digital.laboratory.platform.imr.convert.DrugHandingOverApplyConvert;
import digital.laboratory.platform.imr.dto.DrugHandingOverReviewDTO;
import digital.laboratory.platform.imr.query.DrugHandingOverApplyQuery;
import digital.laboratory.platform.imr.service.DrugHandingOverApplyService;
import digital.laboratory.platform.imr.vo.DrugHandingOverApplyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.ValidationException;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@RequestMapping("/handingOverApply")
@Api(value = "DrugHandingOverApplyController", tags = "毒品送样申请相关接口")
public class DrugHandingOverApplyController {

    @Resource
    private DrugHandingOverApplyService drugHandingOverApplyService;

    @ApiOperation("创建毒品送缴申请-委托单位操作")
    @GetMapping("/query")
    @DlpResultProc
    public R query(@RequestParam("id") String id) {
        return R.ok(DrugHandingOverApplyConvert.entityToVO(drugHandingOverApplyService.getById(id)));
    }

    @ApiOperation("创建毒品送缴申请-委托单位操作")
    @PostMapping("/save")
    public R save() {
        DrugHandingOverApplyVO vo = drugHandingOverApplyService.create();
        if (vo == null) {
            return R.failed("保存数据失败！");
        }
        return R.ok(vo);
    }

    @ApiOperation("毒品送缴申请分页接口")
    @PostMapping("/page")
    public R<IPage<DrugHandingOverApplyVO>> page(@RequestBody DrugHandingOverApplyQuery query) {
        IPage<DrugHandingOverApplyVO> voiPage = drugHandingOverApplyService.voPage(query);
        return R.ok(voiPage);
    }

    @ApiOperation("提交毒品送缴申请-委托单位操作")
    @PutMapping("/submit")
    public R submit(@RequestBody @Valid @NotEmpty(message = "未选择需要提交的送缴申请！") List<String> applyIds) {
        boolean success = false;
        try {
            success = drugHandingOverApplyService.submit(applyIds);
        } catch (ValidateCodeException e) {
            return R.failed(e.getMessage());
        }
        return R.ok(success);
    }

    @ApiOperation("删除毒品送缴申请-委托单位操作")
    @PostMapping("/delete")
    public R delete(@RequestBody List<String> ids) {
        boolean success = drugHandingOverApplyService.delete(ids);
        return R.ok(success);
    }

    @ApiOperation("毒品送缴申请审核-受理单位操作")
    @PutMapping("/review")
    public R review(@RequestBody DrugHandingOverReviewDTO dto) {
        DLPUser user = SecurityUtils.getUser();
        if (!user.isStaff()) {
            return R.failed(String.format("用户 %s 没有权限进行该操作！", user.getName()));
        }
        if (dto.getIsPass() != 1 && dto.getIsPass() != -1) {
            return R.failed("操作错误！请选择是否同意！");
        }
        DrugHandingOverApplyVO vo = null;
        try {
            vo = drugHandingOverApplyService.review(dto, user);
        } catch (ValidateCodeException e) {
            return R.failed(e.getMessage());
        }
        return R.ok(vo);
    }

    @ApiOperation("判断申请中是否有待入库或者出库待归还的检材")
    @GetMapping("/whetherAllowInbound")
    public R whetherAllowInbound(@RequestParam("applyId") @NotBlank(message = "未选择完成收样的申请！") String applyId) {
        boolean allow = drugHandingOverApplyService.whetherAllowInbound(applyId);
        if (allow) {
            return R.ok(true, "申请中存在待入库的检材！");
        }
        return R.ok(false, "当前申请中不存在待入库检材！");
    }

    @ApiOperation("完成收样接口，计算该申请中所有毒品的固体总质量和液体总体积")
    @PutMapping("/finish")
    public R finish(@RequestParam("applyId") @NotBlank(message = "未选择完成收样的申请！") String applyId) {
        boolean success = drugHandingOverApplyService.finish(applyId);
        return R.ok();
    }
}
