package com.emr.controller;

import com.emr.dto.FastBorrowingDto;
import com.emr.entity.BatchApplyDetails;
import com.emr.service.BatchApplyDetails.BatchApplyDetailsService;
import com.emr.util.ExceptionPrintUtil;
import com.emr.vo.BatchBorrowingVo;
import com.emr.vo.Msg;
import com.emr.vo.commomSearch.CommomVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName BatchApplyController
 * @Description 批量申请借阅控制层
 * @Author linjj
 * @Date 2023/8/28 15:56
 * @Version 1.0
 */
@Controller
@RequestMapping("applyDetails/")
public class BatchApplyDetailsController {

    @Autowired
    BatchApplyDetailsService batchApplyDetailsService;


    @RequestMapping(value = "batchApplyDetails")
    public String batchApplyDetails(Model model) {
        return "batchApply/batchApplyDetails";
    }


    /**
     * @description: 借阅详情接口
     * @params: CommomVo
     * @author linjj
     * @date: 2023/9/4 9:02
     */
    @RequestMapping("getCommomVo")
    @ResponseBody
    public PageInfo getCommomVo(Integer page, Integer limit, CommomVo commomVo) {
        if (null != page && null != limit) {
            PageHelper.startPage(page, limit);
        }
        try {
            //查询
            List<CommomVo> list = batchApplyDetailsService.addCommomVo(commomVo);
            return new PageInfo<>(list);
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description: 申请借阅列表
     * @params: batchId
     * @return: BatchApplyDetails
     * @author linjj
     * @date: 2023/9/4 15:16
     */
    @RequestMapping("getBatchApplyDetails")
    @ResponseBody
    public PageInfo getBatchApplyDetails(Integer page, Integer limit, int batchId, String inpatientNo) {
        if (null != page && null != limit) {
            PageHelper.startPage(page, limit);
        }
        try {
            //查询
            List<BatchApplyDetails> list = batchApplyDetailsService.getBatchApplyDetails(batchId, inpatientNo);
            return new PageInfo<>(list);
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description: 批量借阅接口
     * @params: BatchBorrowingVo
     * @author linjj
     * @date: 2023/9/6 15:15
     */
    @RequestMapping("addBatchBorrowing")
    @ResponseBody
    public Msg addBatchBorrowing(BatchBorrowingVo batchBorrowingVo) {
        if (StringUtils.isBlank(batchBorrowingVo.getPatientIds())) {
            return Msg.fail("病案主键为空");
        }
        Msg msg = batchApplyDetailsService.addBatchBorrowing(batchBorrowingVo);
        return msg;
    }

    /**
     * @description: 删除申请记录
     * @params: id
     * @return: int
     * @author linjj
     * @date: 2023/9/18 16:38
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public int deleteById(int id, int batchId) {
        return batchApplyDetailsService.deleteById(id, batchId);
    }

    /**
     * @description: 驳回申请记录
     * @params: id
     * @return: int
     * @author linjj
     * @date: 2023/9/18 16:38
     */
    @RequestMapping("rejectById")
    @ResponseBody
    public int rejectById(int id) {
        return batchApplyDetailsService.rejectById(id);
    }

    /**
     * @description: 审批接口
     * @params: ids
     * @return: int
     * @author linjj
     * @date: 2023/9/20 14:54
     */
    @RequestMapping("agreeById")
    @ResponseBody
    public int agreeById(String ids, Integer batchId) {
        return batchApplyDetailsService.agreeById(ids, batchId);
    }

    /**
     * @description: 快速归还接口
     * @params: barCode
     * @return: int
     * @author linjj
     * @date: 2023/12/27 17:05
     */
    @RequestMapping("updateReturn")
    @ResponseBody
    public int updateReturn(String barCode,String barCodeName) {
        return batchApplyDetailsService.updateReturn(barCode,barCodeName);
    }

    /**
     * @description: 快速借阅接口
     * @params: FastBorrowingDto
     * @return: Msg
     * @author linjj
     * @date: 2023/12/29 10:06
     */
    @RequestMapping("fastBorrowing")
    @ResponseBody
    public Msg fastBorrowing(FastBorrowingDto dto) {
        if (null==dto.getEffeDays()) {
            return Msg.BorrowingFail("申请天数不能为空！！");
        }
        if (StringUtils.isBlank(dto.getApplyType())) {
            return Msg.BorrowingFail("申请类型不能为空！！");
        }
        if (StringUtils.isBlank(dto.getApplyReason())) {
            return Msg.BorrowingFail("申请理由不能为空");
        }
        if (StringUtils.isBlank(dto.getBarCode())) {
            return Msg.BorrowingFail("条形码不能为空");
        }
        return batchApplyDetailsService.addFastBorrowing(dto);
    }


    /**
     * @description: 批次申请快速归还
     * @params:  barCode
     * @return:  barCodeName
     * @author linjj
     * @date: 2024/1/8 9:02
     */
    @RequestMapping("updateBatchApply")
    @ResponseBody
    public Msg updateBatchApply(String barCode,String barCodeName,Integer batchId){
        if (StringUtils.isBlank(barCode)){
            return Msg.BorrowingFail("条形码不能为空");
        }
        if (StringUtils.isBlank(barCodeName)){
            return Msg.BorrowingFail("归还人工号不能为空");
        }
        return batchApplyDetailsService.updateBatchApply(barCode,barCodeName,batchId);
    }
    /**
     * @description: 批次申请快速借阅
     * @params: fastBatchApply
     * @return:  FastBorrowingDto
     * @author linjj
     * @date: 2024/1/8 14:52
     */
    @RequestMapping("fastBatchApply")
    @ResponseBody
    public Msg fastBatchApply(FastBorrowingDto dto) {
        if (null==dto.getEffeDays()) {
            return Msg.BorrowingFail("申请天数不能为空！！");
        }
        if (StringUtils.isBlank(dto.getApplyType())) {
            return Msg.BorrowingFail("申请类型不能为空！！");
        }
        if (StringUtils.isBlank(dto.getApplyReason())) {
            return Msg.BorrowingFail("申请理由不能为空");
        }
        if (StringUtils.isBlank(dto.getBarCode())) {
            return Msg.BorrowingFail("条形码不能为空");
        }
        return batchApplyDetailsService.fastBatchApply(dto);
    }

}
