package com.ruoyi.web.controller.care;

import java.io.File;
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.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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.CareNurseClocks;
import com.ruoyi.care.service.ICareNurseClocksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 护理任务打卡Controller
 * 
 * @author qy
 * @date 2024-11-26
 */
@RestController
@RequestMapping("/care/clocks")
public class CareNurseClocksController extends BaseController
{
    @Autowired
    private ICareNurseClocksService careNurseClocksService;
    @Autowired
    private ICareApplicationsService careApplicationsService;


    /**
     * 查询护理任务打卡列表
     */
    @PreAuthorize("@ss.hasPermi('care:clocks:list')")
    @GetMapping("/list")
    public TableDataInfo list(CareNurseClocks careNurseClocks)
    {
        startPage();
        List<CareNurseClocks> list = careNurseClocksService.selectCareNurseClocksList(careNurseClocks);
        return getDataTable(list);
    }

    /**
     * 导出护理任务打卡列表
     */
    @PreAuthorize("@ss.hasPermi('care:clocks:export')")
    @Log(title = "护理任务打卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CareNurseClocks careNurseClocks)
    {
        List<CareNurseClocks> list = careNurseClocksService.selectCareNurseClocksList(careNurseClocks);
        ExcelUtil<CareNurseClocks> util = new ExcelUtil<CareNurseClocks>(CareNurseClocks.class);
        util.exportExcel(response, list, "护理任务打卡数据");
    }

    /**
     * 获取护理任务打卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('care:clocks:query')")
    @GetMapping(value = "/{clockId}")
    public AjaxResult getInfo(@PathVariable("clockId") Long clockId)
    {
        return success(careNurseClocksService.selectCareNurseClocksByClockId(clockId));
    }

    /**
     * 新增护理任务打卡
     */
    @PreAuthorize("@ss.hasPermi('care:clocks:add')")
    @Log(title = "护理任务打卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CareNurseClocks careNurseClocks)
    {
        return toAjax(careNurseClocksService.insertCareNurseClocks(careNurseClocks));
    }

    /**
     * 修改护理任务打卡
     */
    @PreAuthorize("@ss.hasPermi('care:clocks:edit')")
    @Log(title = "护理任务打卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CareNurseClocks careNurseClocks)
    {
        return toAjax(careNurseClocksService.updateCareNurseClocks(careNurseClocks));
    }

    /**
     * 删除护理任务打卡
     */
    @PreAuthorize("@ss.hasPermi('care:clocks:remove')")
    @Log(title = "护理任务打卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clockIds}")
    public AjaxResult remove(@PathVariable Long[] clockIds)
    {
        return toAjax(careNurseClocksService.deleteCareNurseClocksByClockIds(clockIds));
    }

    /**
     * 上传打卡图片
     * applyId 护理ip
     * nurse_date 护理日期
     */
    @Log(title = "上传打卡图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file,String applyId,String nurseDate) throws Exception
    {
        //获取护理详细
        CareApplications careApplications=careApplicationsService.selectCareApplicationsByApplyId(Long.parseLong(applyId));
        String basePath=RuoYiConfig.getUploadPath()+"/"+careApplications.getApplyId()+"-"+careApplications.getPersonName()+"-"+careApplications.getApplyDate();
        if(StringUtils.isNotEmpty(nurseDate)){
            basePath=basePath+"/"+nurseDate;
        }
        if (!file.isEmpty())
        {
            String clockImg = FileUploadUtils.upload(basePath, file, MimeTypeUtils.IMAGE_EXTENSION);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", clockImg);
            // 更新缓存用户头像
            return ajax;
        }
        return error("上传图片异常，请联系管理员");
    }

    /**
     * 上传打卡图片
     * applyId 护理ip
     * nurse_date 护理日期
     */
    @Log(title = "删除打卡图片", businessType = BusinessType.UPDATE)
    @GetMapping("/removeImg")
    public AjaxResult removeImg(String address) throws Exception
    {
        //获取护理详细
        String basePath=RuoYiConfig.getProfile()+"/"+address.replaceAll(Constants.RESOURCE_PREFIX,"");
        File file = new File(basePath);
        // 尝试删除文件
        if (file.delete()) {
            return success("删除成功");
        } else {
            return error("上传图片异常，请联系管理员");
        }

    }


}
