package com.xinelu.manage.controller.nursestationperson;

import com.xinelu.common.annotation.Log;
import com.xinelu.common.constant.Constants;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.custominterface.Insert;
import com.xinelu.common.custominterface.Update;
import com.xinelu.common.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.nursestationperson.NurseStationPerson;
import com.xinelu.manage.dto.nursestationperson.NurseStationPersonCheckDTO;
import com.xinelu.manage.dto.nursestationperson.NurseStationPersonDTO;
import com.xinelu.manage.dto.nursestationperson.NurseStationPersonImportDTO;
import com.xinelu.manage.service.nursestationperson.INurseStationPersonService;
import com.xinelu.manage.vo.nursestationperson.NurseStationPersonVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 护理站人员信息Controller
 *
 * @author ljh
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/system/person")
public class NurseStationPersonController extends BaseController {
    @Resource
    private INurseStationPersonService nurseStationPersonService;

    /**
     * 查询护理站人员信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(NurseStationPerson nurseStationPerson) {
        startPage();
        List<NurseStationPerson> list = nurseStationPersonService.selectNurseStationPersonList(nurseStationPerson);
        return getDataTable(list);
    }

    /**
     * 导出护理站人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:person:export')")
    @Log(title = "护理站人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NurseStationPerson nurseStationPerson) {
        List<NurseStationPerson> list = nurseStationPersonService.selectNurseStationPersonList(nurseStationPerson);
        ExcelUtil<NurseStationPerson> util = new ExcelUtil<>(NurseStationPerson.class);
        util.exportExcel(response, list, "护理站人员信息数据");
    }

    /**
     * 获取护理站人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(nurseStationPersonService.selectNurseStationPersonById(id));
    }

    /**
     * 新增护理站人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:add')")
    @Log(title = "护理站人员信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(Insert.class) @RequestBody NurseStationPersonDTO nurseStationPerson) {
        return nurseStationPersonService.insertNurseStationPersonList(nurseStationPerson);
    }

    /**
     * 修改护理站人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:edit')")
    @Log(title = "护理站人员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Validated(Update.class) @RequestBody NurseStationPersonDTO nurseStationPerson) {
        return nurseStationPersonService.updateNurseStationPerson(nurseStationPerson);
    }

    /**
     * 删除护理站人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:remove')")
    @Log(title = "护理站人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return nurseStationPersonService.deleteNurseStationPersonByIds(ids);
    }

    /**
     * 查询护理站人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:person:list')")
    @GetMapping("/getNurseStationPersonList")
    public TableDataInfo getNurseStationPersonList(NurseStationPersonVO nurseStationPerson) {
        startPage();
        List<NurseStationPersonVO> nurseStationPersonList = nurseStationPersonService.getNurseStationPersonList(nurseStationPerson);
        return getDataTable(nurseStationPersonList);
    }

    /**
     * 导入护理站人员信息
     */
    @PreAuthorize("@ss.hasPermi('system:person:importStationPerson')")
    @PostMapping("/insertNurseStationPersonImportList")
    public AjaxResult insertNurseStationPersonImportList(MultipartFile file) throws Exception {
        //判断文件是否为空
        if (Objects.isNull(file) || StringUtils.isBlank(file.getOriginalFilename())) {
            return AjaxResult.error("请选择所要导入的护理站人员信息！");
        }
        if (!file.getOriginalFilename().endsWith(Constants.XLSX) && !file.getOriginalFilename().endsWith(Constants.XLS)) {
            return AjaxResult.error("文件格式不正确，请选择xlsx或者xls格式的文件！");
        }
        ExcelUtil<NurseStationPersonImportDTO> util = new ExcelUtil<>(NurseStationPersonImportDTO.class);
        List<NurseStationPersonImportDTO> list = util.importExcel(file.getInputStream());
        return nurseStationPersonService.insertNurseStationPersonImportList(list);
    }

    /**
     * 审核接口根据id查询护理站人员信息以及未审核的证书
     */
    @GetMapping(value = "/getNurseStationPersonCheck/{id}")
    public AjaxResult getNurseStationPersonCheck(@PathVariable("id") Long id) {
        return AjaxResult.success(nurseStationPersonService.getNurseStationPersonCheck(id));
    }

    /**
     * 管理端更新护理人员主表信息与证书信息
     */
    @PostMapping("/updateNurseStationPersonChecks")
    public AjaxResult updateNurseStationPersonChecks(@RequestBody NurseStationPersonCheckDTO nurseStationPersonCheckDTO) throws Exception {
        return nurseStationPersonService.updateNurseStationPersonChecks(nurseStationPersonCheckDTO);
    }
}