package com.xinelu.applet.controller.nursepersonapplogin;


import com.xinelu.applet.dto.nursepersonapplogin.OrderFallbackDTO;
import com.xinelu.applet.service.nursepersonapplogin.INursePersonAppLoginService;
import com.xinelu.applet.vo.nursepersonapplogin.NurseAppLoginSysUserVO;
import com.xinelu.applet.vo.nursepersonapplogin.OrderAndItemVO;
import com.xinelu.applet.vo.nursepersonapplogin.PersonNurseStationAppVO;
import com.xinelu.common.annotation.MobileRequestAuthorization;
import com.xinelu.common.annotation.RepeatSubmit;
import com.xinelu.common.constant.Constants;
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.custominterface.Insert;
import com.xinelu.common.custominterface.TaskInsert;
import com.xinelu.common.utils.regex.RegexUtil;
import com.xinelu.manage.domain.appointmentorderdetails.AppointmentOrderDetails;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Description 护理人员APP登录注册控制器
 * @Author zh
 * @Date 2022-10-28
 */
@RestController
@RequestMapping("/nurseApp/personLogin")
public class NursePersonAppLoginController extends BaseController {

    @Resource
    private INursePersonAppLoginService nursePersonAppLoginService;
    @Resource
    private RegexUtil regexUtil;

    /**
     * 工单首页查询已完成以及未完成数量
     *
     * @param nurseStationPersonId 护理员id
     * @return 结果
     */
    @MobileRequestAuthorization
    @GetMapping("/selectOrderByNursePersonCount")
    public AjaxResult selectOrderByNursePersonCount(Long nurseStationPersonId) {
        return nursePersonAppLoginService.selectOrderByNursePersonCount(nurseStationPersonId);
    }

    /**
     * 服务人员我的任务界面 查询服务订单
     *
     * @param orderAndItem 预约订单信息
     * @return 结果
     */
    @MobileRequestAuthorization
    @GetMapping("/selectMission")
    public TableDataInfo selectMission(OrderAndItemVO orderAndItem) {
        startPage();
        List<OrderAndItemVO> appointmentOrder = nursePersonAppLoginService.selectAppointmentOrder(orderAndItem);
        return getDataTable(appointmentOrder);
    }

    /**
     * 服务人员-服务订单详情
     *
     * @param orderNo 订单编号
     * @return 结果
     */
    @MobileRequestAuthorization
    @GetMapping("/taskDetails")
    public AjaxResult selectOrderDetailsAndPatient(String orderNo) {
        if (Objects.isNull(orderNo)) {
            return AjaxResult.error("订单信息不能为空!");
        }
        return nursePersonAppLoginService.selectOrderDetailsAndPatient(orderNo);
    }

    /**
     * 服务人员 - 任务回退
     *
     * @param orderFallbackDTO 订单信息
     * @return 结果
     */
    @MobileRequestAuthorization
    @PostMapping("/orderFallback")
    public AjaxResult updateAppointmentOrder(@Validated(Insert.class) @RequestBody OrderFallbackDTO orderFallbackDTO) {
        return nursePersonAppLoginService.updateAppointmentOrder(orderFallbackDTO);
    }

    /**
     * 任务确认完成
     *
     * @param appointmentOrderDetails 图片路径
     * @return AjaxResult
     */
    @MobileRequestAuthorization
    @PostMapping("/orderConfirm")
    public AjaxResult updateOrderDetails(@Validated(TaskInsert.class) @RequestBody AppointmentOrderDetails appointmentOrderDetails) {
        return nursePersonAppLoginService.updateOrderDetails(appointmentOrderDetails);
    }

    /**
     * 护理人员 注册用户信息
     *
     * @param nurseAppLogin 账号信息
     * @return 结果
     */
    @PostMapping("/nurseAppLoginSysUser")
    public AjaxResult nurseAppLoginSysUser(@Validated(Insert.class) @RequestBody NurseAppLoginSysUserVO nurseAppLogin) {
        //校验手机号
        boolean regexPhone = regexUtil.regexPhone(nurseAppLogin.getPhonenumber());
        if (BooleanUtils.isFalse(regexPhone)) {
            return AjaxResult.error("您输入的手机号不正确，请重新输入！");
        }
        return nursePersonAppLoginService.nurseAppLoginSysUser(nurseAppLogin);
    }

    /**
     * 护理人员 app登录
     *
     * @param phonenumber           账号
     * @param stationPersonPassword 密码
     * @return 结果
     */
    @PostMapping("/appLogin")
    public AjaxResult logins(String phonenumber, String stationPersonPassword) {
        if (StringUtils.isBlank(phonenumber)) {
            return AjaxResult.error("请输入账号！");
        }
        if (StringUtils.isBlank(stationPersonPassword)) {
            return AjaxResult.error("请输入密码！");
        }
        return nursePersonAppLoginService.logins(phonenumber, stationPersonPassword);
    }

    /**
     * 忘记密码
     *
     * @param phonenumber           手机号
     * @param stationPersonPassword 密码
     * @return 结果
     */
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(String phonenumber, String stationPersonPassword, String verification) {
        if (StringUtils.isBlank(phonenumber)) {
            return AjaxResult.error("手机号不能为空！");
        }
        if (StringUtils.isBlank(stationPersonPassword)) {
            return AjaxResult.error("密码不能为空！");
        }
        return nursePersonAppLoginService.updatePassword(phonenumber, stationPersonPassword, verification);
    }

    /**
     * 查询护理站人员的个人信息
     *
     * @param nurseAppLoginSysUserVO 个人信息
     * @return com.xinyilu.nurseapplet.domain.vo.nursepersonapplogin.NursePersonAppLoginsUserVO
     **/
    @MobileRequestAuthorization
    @GetMapping("/nursePerson")
    public AjaxResult nursePerson(NurseAppLoginSysUserVO nurseAppLoginSysUserVO) {
        if (StringUtils.isBlank(nurseAppLoginSysUserVO.getPhonenumber())) {
            return AjaxResult.error("账号异常，请联系管理员！");
        }
        if (StringUtils.isBlank(nurseAppLoginSysUserVO.getStationPersonPassword())) {
            return AjaxResult.error("账号密码异常，请联系管理员！");
        }
        return AjaxResult.success(nursePersonAppLoginService.getNursePersonPersonalInformation(nurseAppLoginSysUserVO));
    }

    /**
     * 修改用户护理人员的账户密码
     *
     * @param nurseAppLoginSysUserVO 用户信息
     * @return int
     **/
    @MobileRequestAuthorization
    @RepeatSubmit
    @PostMapping("/userPassWord")
    public AjaxResult userPassWord(@RequestBody NurseAppLoginSysUserVO nurseAppLoginSysUserVO) {
        return nursePersonAppLoginService.updateUserPassWord(nurseAppLoginSysUserVO);
    }

    /**
     * 修改被护理人人员上传文件头像
     *
     * @param multipartFile 文件
     * @param userId        userId护理人人员id
     * @return com.xinyilu.common.core.domain.AjaxResult
     * @throws Exception 异常信息
     **/
    @PostMapping("/updateHeadAvatarHead")
    public AjaxResult updateHeadAvatarHead(@RequestParam("file") MultipartFile multipartFile, Long userId) throws Exception {
        if (Objects.isNull(userId)) {
            return AjaxResult.error("护理人人员id不能为空！");
        }
        if (Objects.isNull(multipartFile) || StringUtils.isBlank(multipartFile.getOriginalFilename())) {
            return AjaxResult.error("当前文件不存在，无法上传！");
        }
        if (multipartFile.getOriginalFilename().contains(Constants.EMPTY)) {
            return AjaxResult.error("当前文件名含有空格，请先去除空格在上传！");
        }
        return nursePersonAppLoginService.updateHeadAvatarHead(multipartFile, userId);
    }

    /**
     * 查询护理站信息列表
     */
    @GetMapping("/personNurseStationLists")
    public TableDataInfo personNurseStationLists(PersonNurseStationAppVO nurseStation) {
        startPage();
        List<PersonNurseStationAppVO> personNurseStationLists = nursePersonAppLoginService.getPersonNurseStationLists(nurseStation);
        return getDataTable(personNurseStationLists);
    }
}
