package com.ruoyi.web.controller.care;

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

import com.ruoyi.care.domain.CareApplications;
import com.ruoyi.care.service.ICareApplicationsService;
import com.ruoyi.common.annotation.DataScope;
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.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 护理申请Controller
 * 
 * @author qy
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/care/applications")
public class CareApplicationsController extends BaseController
{
    @Autowired
    private ICareApplicationsService careApplicationsService;

    /**
     * 查询护理申请列表
     */
    @PreAuthorize("@ss.hasPermi('care:applications:list')")
    @DataScope(deptAlias = "u", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(CareApplications careApplications)
    {
        startPage();
        List<CareApplications> list = careApplicationsService.selectCareApplicationsList(careApplications);
        return getDataTable(list);
    }

    /**
     * 导出护理申请列表
     */
    @PreAuthorize("@ss.hasPermi('care:applications:export')")
    @Log(title = "护理申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CareApplications careApplications)
    {
        List<CareApplications> list = careApplicationsService.selectCareApplicationsList(careApplications);
        ExcelUtil<CareApplications> util = new ExcelUtil<CareApplications>(CareApplications.class);
        util.exportExcel(response, list, "护理申请数据");
    }

    /**
     * 获取护理申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('care:applications:query')")
    @GetMapping(value = "/{applyId}")
    public AjaxResult getInfo(@PathVariable("applyId") Long applyId)
    {
        return success(careApplicationsService.selectCareApplicationsByApplyId(applyId));
    }

    /**
     * 新增护理申请
     */
    @PreAuthorize("@ss.hasPermi('care:applications:add')")
    @Log(title = "护理申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CareApplications careApplications)
    {
        //判断当前申请日期是否有护理记录或正在进行的护理记录
        CareApplications careApplications1=new CareApplications();
        careApplications1.setPersonId(careApplications.getPersonId());
        careApplications1.setApplyDate(careApplications.getApplyDate());
        careApplications1.setNotDealStatus("complete");
        List<CareApplications> lst=careApplicationsService.selectCareApplicationsList(careApplications1);
        if(lst.size()>0){
            return AjaxResult.error("当前特困人员在该申请日期已有未完成的护理申请");
        }
        careApplications.setCreateBy(getUserId().toString());
        return toAjax(careApplicationsService.insertCareApplications(careApplications));
    }

    /**
     * 修改护理申请
     */
    @PreAuthorize("@ss.hasPermi('care:applications:edit')")
    @Log(title = "护理申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CareApplications careApplications)
    {
        //判断当前申请日期是否有护理记录或正在进行的护理记录
        CareApplications careApplications1=new CareApplications();
        careApplications1.setPersonId(careApplications.getPersonId());
        careApplications1.setApplyDate(careApplications.getApplyDate());
        careApplications1.setNotDealStatus("complete");
        List<CareApplications> lst=careApplicationsService.selectCareApplicationsList(careApplications1);
        if(lst.size()>0){
            return AjaxResult.error("当前特困人员在该申请日期已有未完成的护理申请");
        }

        careApplications.setUpdateBy(getUserId().toString());
        return toAjax(careApplicationsService.updateCareApplications(careApplications));
    }

    /**
     * 删除护理申请
     */
    @PreAuthorize("@ss.hasPermi('care:applications:remove')")
    @Log(title = "护理申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applyIds}")
    public AjaxResult remove(@PathVariable Long[] applyIds)
    {
        return toAjax(careApplicationsService.deleteCareApplicationsByApplyIds(applyIds));
    }

    /**
     * 修改护理申请
     */
    @Log(title = "护理指派公司", businessType = BusinessType.UPDATE)
    @PostMapping("/assign")
    public AjaxResult assign(@RequestBody CareApplications careApplications)
    {
        return toAjax(careApplicationsService.assign(careApplications));
    }
}
