package com.xinelu.manage.controller.patientinfo;

import com.xinelu.common.annotation.Log;
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.enums.BusinessType;
import com.xinelu.common.utils.poi.ExcelUtil;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.dto.cancelpicture.CancelPictureDTO;
import com.xinelu.manage.service.patientinfo.IPatientInfoService;
import com.xinelu.manage.vo.patientinfo.PatientInfoVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 被护理人基本信息Controller
 *
 * @author xinyilu
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/system/patientArchives")
public class PatientArchivesInfoController extends BaseController {

    @Resource
    private IPatientInfoService patientInfoService;

    /**
     * 查询被护理人基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:patientArchives:list')")
    @GetMapping("/list")
    public TableDataInfo list(PatientInfoVO patientInfo) {
        startPage();
        List<PatientInfoVO> list = patientInfoService.selectPatientInfoList(patientInfo);
        return getDataTable(list);
    }

    /**
     * 导出被护理人基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:patientArchives:export')")
    @Log(title = "被护理人基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PatientInfoVO patientInfo) {
        List<PatientInfoVO> list = patientInfoService.selectPatientInfoList(patientInfo);
        ExcelUtil<PatientInfoVO> util = new ExcelUtil<>(PatientInfoVO.class);
        util.exportExcel(response, list, "被护理人基本信息数据");
    }

    /**
     * 获取被护理人基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:patientArchives:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(patientInfoService.selectPatientInfoById(id));
    }

    /**
     * 新增被护理人基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:patientArchives:add')")
    @Log(title = "被护理人基本信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PatientInfo patientInfo) {
        return toAjax(patientInfoService.insertPatientInfo(patientInfo));
    }

    /**
     * 修改被护理人基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:patientArchives:edit')")
    @Log(title = "被护理人基本信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "edit")
    public AjaxResult edit(@RequestBody PatientInfo patientInfo) {
        return patientInfoService.updatePatientInfo(patientInfo);
    }

    /**
     * 删除被护理人基本信息
     */
    @PreAuthorize("@ss.hasPermi('system:patientArchives:remove')")
    @Log(title = "被护理人基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(patientInfoService.deletePatientInfoByIds(ids));
    }

    /**
     * 删除旧图片
     *
     * @param cancelPictureDTO 路径参数
     * @return 结果
     */
    @PostMapping("/updatePicture")
    public AjaxResult updatePicture(@RequestBody CancelPictureDTO cancelPictureDTO) {
        if (CollectionUtils.isEmpty(cancelPictureDTO.getPictureUrlList())) {
            return AjaxResult.success();
        }
        return patientInfoService.updatePicture(cancelPictureDTO.getPictureUrlList());
    }

    /**
     * PC端重置密码
     *
     * @param id       会员id
     * @param password 密码
     * @return AjaxResult
     */
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(Long id, String password) {
        if (Objects.isNull(id)) {
            return AjaxResult.error("用户信息有误，请联系管理员！");
        }
        if (StringUtils.isBlank(password)) {
            return AjaxResult.error("请输入密码！");
        }
        return patientInfoService.updatePasswordById(id, password);
    }
}

