package com.ruoyi.web.controller.care;

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

import com.ruoyi.care.domain.CareCompanies;
import com.ruoyi.care.service.ICareCompaniesService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.care.domain.CareNurses;
import com.ruoyi.care.service.ICareNursesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 护理人员Controller
 * 
 * @author qy
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/care/nurses")
public class CareNursesController extends BaseController
{
    @Autowired
    private ICareNursesService careNursesService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICareCompaniesService careCompaniesService;
    /**
     * 查询护理人员列表
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:list')")
    @GetMapping("/list")
    public TableDataInfo list(CareNurses careNurses)
    {
        startPage();
        //判断当前用户是不是公司，如果是公司，则只获取自己公司的
        CareCompanies careCompanies=new CareCompanies();
        careCompanies.setUserId(getUserId());
        List<CareCompanies> lst=careCompaniesService.selectCareCompaniesList(careCompanies);
        if(lst.size()>0){
            careNurses.setCompanyId(lst.get(0).getCompanyId());
        }

        List<CareNurses> list = careNursesService.selectCareNursesList(careNurses);
        return getDataTable(list);
    }

    /**
     * 获取所有的护理人员信息
     */
    @GetMapping("/listAll")
    public List<CareNurses> listAll(CareNurses careNurses)
    {
        List<CareNurses> list = careNursesService.selectCareNursesList(careNurses);
        return list;
    }

    /**
     * 获取当前公司护理人员信息
     */
    @GetMapping("/listCompanyNurses")
    public List<CareNurses> listCompanyNurses(CareNurses careNurses)
    {
        CareCompanies careCompanies=new CareCompanies();
        careCompanies.setUserId(getUserId());
        List<CareCompanies> lst=careCompaniesService.selectCareCompaniesList(careCompanies);
        if(lst.size()>0){
            careNurses.setCompanyId(lst.get(0).getCompanyId());
        }
        List<CareNurses> list = careNursesService.selectCareNursesList(careNurses);
        return list;
    }

    /**
     * 导出护理人员列表
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:export')")
    @Log(title = "护理人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CareNurses careNurses)
    {
        List<CareNurses> list = careNursesService.selectCareNursesList(careNurses);
        ExcelUtil<CareNurses> util = new ExcelUtil<CareNurses>(CareNurses.class);
        util.exportExcel(response, list, "护理人员数据");
    }

    /**
     * 获取护理人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:query')")
    @GetMapping(value = "/{nurseId}")
    public AjaxResult getInfo(@PathVariable("nurseId") Long nurseId)
    {
        return success(careNursesService.selectCareNursesByNurseId(nurseId));
    }

    /**
     * 新增护理人员
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:add')")
    @Log(title = "护理人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CareNurses careNurses)
    {
        return toAjax(careNursesService.insertCareNurses(careNurses));
    }

    /**
     * 修改护理人员
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:edit')")
    @Log(title = "护理人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CareNurses careNurses)
    {
        return toAjax(careNursesService.updateCareNurses(careNurses));
    }

    /**
     * 删除护理人员
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:remove')")
    @Log(title = "护理人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{nurseIds}")
    public AjaxResult remove(@PathVariable Long[] nurseIds)
    {
        return toAjax(careNursesService.deleteCareNursesByNurseIds(nurseIds));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('care:nurses:resetPassword')")
    @Log(title = "护理人员重置密码", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPassword/{userId}")
    public AjaxResult resetPassword(@PathVariable("userId") Long userId)
    {
        SysUser user = userService.selectUserById(userId);
        user.setPassword(SecurityUtils.encryptPassword("123456"));
        user.setUpdateBy(getUsername());
        return toAjax(userService.resetPwd(user));
    }
}
