package com.emr.controller;

import com.emr.entity.BatchApply;
import com.emr.entity.Power_User;
import com.emr.service.batchApply.BatchApplyService;
import com.emr.util.ExceptionPrintUtil;
import com.emr.vo.BatchApplyVo;
import com.emr.vo.Msg;
import com.emr.vo.commomSearch.CommomVo;
import com.github.pagehelper.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

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

    @Autowired
    BatchApplyService batchApplyService;


    @RequestMapping(value = "batchApplication")
    public String batchApply(Model model) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        // 从session获取用户名
        Power_User user = (Power_User) request.getSession().getAttribute("CURRENT_USER");
        if (user != null) {
            Integer roleId = user.getRoleId();
            if (roleId == 0 || roleId == -100) {
                model.addAttribute("createNameFlag", "0");
            }
        }
        return "batchApply/batchApply";
    }


    /**
     * @description: 查看批量借阅审批数据
     * @author linjj
     * @date: 2023/8/28 17:02
     */
    @RequestMapping("selectBatchApply")
    @ResponseBody
    public PageInfo selectBatchApply(Integer page, Integer limit, BatchApplyVo batchApplyVo) {
        if (null != page && null != limit) {
            PageHelper.startPage(page, limit);
        }
        try {
            //查询
            List<BatchApply> list = batchApplyService.selectBatchApply(batchApplyVo);
            return new PageInfo<>(list);
        } catch (Exception e) {
            ExceptionPrintUtil.printException(e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description: 新增批次
     * @params: batchApplyVo
     * @return: int
     * @author linjj
     * @date: 2023/8/31 14:29
     */
    @RequestMapping("insertBatch")
    @ResponseBody
    public int insertBatch(BatchApplyVo batchApplyVo) {
        return batchApplyService.insertBatch(batchApplyVo);
    }

    /**
     * @description: 修改批次
     * @params: batchApplyVo
     * @author linjj
     * @date: 2023/9/1 14:40
     */
    @RequestMapping("updateBatch")
    @ResponseBody
    public int updateBatch(BatchApplyVo batchApplyVo) {
        return batchApplyService.updateBatch(batchApplyVo);
    }

    /** 
     * @description: 删除批次
     * @params:   id
     * @return:  int
     * @author linjj
     * @date: 2023/9/18 15:30
     */
    @RequestMapping("deleteById")
    @ResponseBody
    public  int deleteById(int id){
       return batchApplyService.deleteById(id);
    }

}
