package com.ruoyi.web.controller.care;

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

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.CareCompanies;
import com.ruoyi.care.service.ICareCompaniesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 护理公司Controller
 * 
 * @author qy
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/care/companies")
public class CareCompaniesController extends BaseController
{
    @Autowired
    private ICareCompaniesService careCompaniesService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询护理公司列表
     */
    @PreAuthorize("@ss.hasPermi('care:companies:list')")
    @GetMapping("/list")
    public TableDataInfo list(CareCompanies careCompanies)
    {
        startPage();
        List<CareCompanies> list = careCompaniesService.selectCareCompaniesList(careCompanies);
        return getDataTable(list);
    }
    /**
     * 导出护理公司列表
     */
    @PreAuthorize("@ss.hasPermi('care:companies:export')")
    @Log(title = "护理公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CareCompanies careCompanies)
    {
        List<CareCompanies> list = careCompaniesService.selectCareCompaniesList(careCompanies);
        ExcelUtil<CareCompanies> util = new ExcelUtil<CareCompanies>(CareCompanies.class);
        util.exportExcel(response, list, "护理公司数据");
    }

    /**
     * 获取护理公司详细信息
     */
    @PreAuthorize("@ss.hasPermi('care:companies:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return success(careCompaniesService.selectCareCompaniesByCompanyId(companyId));
    }

    /**
     * 新增护理公司
     */
    @PreAuthorize("@ss.hasPermi('care:companies:add')")
    @Log(title = "护理公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CareCompanies careCompanies)
    {
        return toAjax(careCompaniesService.insertCareCompanies(careCompanies));
    }

    /**
     * 修改护理公司
     */
    @PreAuthorize("@ss.hasPermi('care:companies:edit')")
    @Log(title = "护理公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CareCompanies careCompanies)
    {
        return toAjax(careCompaniesService.updateCareCompanies(careCompanies));
    }

    /**
     * 删除护理公司
     */
    @PreAuthorize("@ss.hasPermi('care:companies:remove')")
    @Log(title = "护理公司", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(careCompaniesService.deleteCareCompaniesByCompanyIds(companyIds));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('care:companies: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));
    }

    /**
     * 获取所有的公司信息
     */
    @GetMapping("/listAll")
    public List<CareCompanies> listAll(CareCompanies careCompanies)
    {
        //判断当前用户是不是公司，如果是公司，则直接返回自己
        CareCompanies currCompany=new CareCompanies();
        currCompany.setUserId(getUserId());
        List<CareCompanies> lst=careCompaniesService.selectCareCompaniesList(currCompany);
        if(lst.size()>0){
            return lst;
        }
        List<CareCompanies> list = careCompaniesService.selectCareCompaniesList(careCompanies);
        return list;
    }

    /**
     * 查询护理公司列表
     */
    @PreAuthorize("@ss.hasPermi('care:companies:dispatch')")
    @GetMapping("/dispatch")
    public TableDataInfo dispatch(CareCompanies careCompanies)
    {
        startPage();
        List<CareCompanies> list = careCompaniesService.selectCareCompaniesList(careCompanies);
        return getDataTable(list);
    }

}
