package com.xinelu.web.controller.fd;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.familydoctor.applet.pojo.body.ApprovalBody;
import com.xinelu.familydoctor.applet.pojo.body.SyncHospitalPersonInfoBody;
import com.xinelu.familydoctor.applet.pojo.query.ApplyQuery;
import com.xinelu.familydoctor.applet.pojo.vo.ResidentRescindApplyVo;
import com.xinelu.familydoctor.applet.pojo.vo.ResidentServiceApplyVo;
import com.xinelu.familydoctor.applet.pojo.vo.ResidentSignApplyVo;
import com.xinelu.familydoctor.applet.service.IResidentRescindApplyService;
import com.xinelu.familydoctor.applet.service.IResidentServiceAppletService;
import com.xinelu.familydoctor.applet.service.IResidentSignAppletService;
import com.xinelu.manage.domain.chatRecord.ChatRecord;
import com.xinelu.manage.domain.hospitalinfo.HospitalInfo;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.dto.chatrecord.MessageSearchDto;
import com.xinelu.manage.mapper.chatrecord.ChatRecordMapper;
import com.xinelu.manage.service.hospitalinfo.IHospitalInfoService;
import com.xinelu.manage.service.hospitalpersoninfo.IHospitalPersonInfoService;
import com.xinelu.manage.service.patientinfo.IPatientInfoService;
import com.xinelu.manage.vo.patientinfo.PatientInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "家医调用接口")
@RestController
@RequestMapping("/applet/fd")
public class FDController extends BaseController {
    @Resource
    private IResidentRescindApplyService residentRescindApplyService;
    @Resource
    private IResidentSignAppletService residentSignAppletService;
    @Resource
    private IResidentServiceAppletService residentServiceAppletService;
    @Resource
    private IPatientInfoService patientInfoService;
    @Resource
    private IHospitalPersonInfoService hospitalPersonInfoService;
    @Resource
    private IHospitalInfoService hospitalInfoService;
    @Resource
    private ChatRecordMapper chatRecordMapper;


    @ApiOperation("服务申请列表")
    @PostMapping("/performanceBooking/list")
    public R<PageInfo<ResidentServiceApplyVo>> performanceBookingList(@RequestBody ApplyQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        return R.ok(PageInfo.of(residentServiceAppletService.findList(query)));
    }

    @ApiOperation("服务申请详情")
    @GetMapping("/performanceBooking/detail/{bookingNo}")
    public R<ResidentServiceApplyVo> performanceBookingDetail(@PathVariable String bookingNo) {
        return R.ok(residentServiceAppletService.detail(bookingNo));
    }

    @ApiOperation("更新服务申请审批结果")
    @PostMapping("/performanceBooking/approval")
    public R<?> performanceBookingApproval(@RequestBody ApprovalBody body) {
        if (body == null) {
            return R.fail("请求参数不能为空");
        }
        residentServiceAppletService.updateFd(body);
        return R.ok();
    }

    @ApiOperation("更新服务完成时间")
    @GetMapping("/performanceBooking/approval/complete/{bookingNo}")
    public R<?> performanceBookingApprovalComplete(@PathVariable String bookingNo) {
        residentServiceAppletService.updateComplete(bookingNo);
        return R.ok();
    }

    @ApiOperation("解约申请列表")
    @PostMapping("/rescindApply/list")
    public R<PageInfo<ResidentRescindApplyVo>> rescindApplyList(@RequestBody ApplyQuery query) {
        if (StringUtils.isBlank(query.getUserNo())) {
            return R.fail("医生编号不能为空");
        }
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        query.setBookingStatus("0");
        return R.ok(PageInfo.of(residentRescindApplyService.findList(query)));
    }

    @ApiOperation("解约申请详情")
    @GetMapping("/rescindApply/detail/{applyNo}")
    public R<ResidentRescindApplyVo> rescindApplyDetail(@PathVariable String applyNo) {
        return R.ok(residentRescindApplyService.detail(applyNo));
    }

    @ApiOperation("更新解约申请审批结果")
    @PostMapping("/rescindApply/approval")
    public R<?> rescindApplyApproval(@RequestBody ApprovalBody body) {
        if (body == null || StringUtils.isBlank(body.getApplyNo())) {
            return R.fail("请求参数不能为空");
        }
        residentRescindApplyService.updateFd(body);
        return R.ok();
    }

    @ApiOperation("签约申请列表")
    @PostMapping("/signBooking/list")
    public R<PageInfo<ResidentSignApplyVo>> signBookingList(@RequestBody ApplyQuery query) {
        if (StringUtils.isBlank(query.getUserNo())) {
            return R.fail("医生编号不能为空");
        }
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        query.setBookingStatus("0");
        return R.ok(PageInfo.of(residentSignAppletService.findList(query)));
    }

    @ApiOperation("签约申请详情")
    @GetMapping("/signBooking/detail/{signCode}")
    public R<ResidentSignApplyVo> signBookingDetail(@PathVariable String signCode) {
        return R.ok(residentSignAppletService.detail(signCode));
    }

    @ApiOperation("更新签约申请审批结果")
    @PostMapping("/signBooking/approval")
    public R<?> signBookingApproval(@RequestBody ApprovalBody body) {
        if (body == null) {
            return R.fail("请求参数不能为空");
        }
        residentSignAppletService.updateFd(body);
        return R.ok();
    }

    @ApiOperation("根据签约主键查询绑定编号")
    @GetMapping("/signBooking/getBySignNo/{cardNo}")
    public R<JSONObject> getBySignNo(@PathVariable String cardNo) {
        JSONObject retObj = new JSONObject();
        PatientInfoVO patientInfo = patientInfoService.selectPatientInfoByCardNo(cardNo);
        if (patientInfo != null) {
            retObj.fluentPut("bindingNo", patientInfo.getPatientCode())
                    .fluentPut("openid", patientInfo.getOpenid());
        }
        return R.ok(retObj);
    }

    @ApiOperation("家医医生同步科室人员信息")
    @PostMapping("/synHospatialPersonInfo")
    public R<?> synHospatialPersonInfo(@RequestBody SyncHospitalPersonInfoBody body) {
        if (body == null) {
            return R.fail("请求参数不能为空");
        }
        if (StringUtils.isBlank(body.getPersonCode())) {
            return R.fail("科室人员编号不能为空");
        }
        if (StringUtils.isBlank(body.getOrgCode())) {
            return R.fail("家医机构编号不能为空");
        }
        // 判断是否已存在
        HospitalPersonInfo hospitalPersonInfo = hospitalPersonInfoService.getByPersonCode(body.getPersonCode(), body.getStatus());
        // 根据家医机构查询医院信息
        HospitalInfo hospital = hospitalInfoService.getHosptalByOrgCode(body.getOrgCode());
        // 新增
        if (hospitalPersonInfo == null) {
            hospitalPersonInfo = new HospitalPersonInfo();
            BeanUtils.copyProperties(body, hospitalPersonInfo);
            hospitalPersonInfo.setCreateBy(body.getPersonCode());
            hospitalPersonInfo.setStatus("1");
            if (hospital != null) {
                hospitalPersonInfo.setHospitalId(hospital.getId());
            }
            hospitalPersonInfoService.insert(hospitalPersonInfo);

            // 修改
        } else {
//            if (!String.valueOf(hospitalPersonInfo.getPersonAccount()).equals(String.valueOf(body.getPersonAccount()))
//                    || !String.valueOf(hospitalPersonInfo.getPersonPhone()).equals(String.valueOf(body.getPersonPhone()))
//                    || !String.valueOf(hospitalPersonInfo.getPersonName()).equals(String.valueOf(body.getPersonName()))
//                    || !String.valueOf(hospitalPersonInfo.getPersonIntroduce()).equals(String.valueOf(body.getPersonIntroduce()))
//                    || !String.valueOf(hospitalPersonInfo.getCardNo()).equals(String.valueOf(body.getCardNo()))
//            ) {
                hospitalPersonInfo.setPersonName(body.getPersonName());
                hospitalPersonInfo.setPersonPhone(body.getPersonPhone());
                hospitalPersonInfo.setPersonAddress(body.getPersonAddress());
                hospitalPersonInfo.setPersonAccount(body.getPersonAccount());
                hospitalPersonInfo.setPersonIntroduce(body.getPersonIntroduce());
                hospitalPersonInfo.setCardNo(body.getCardNo());
                if (hospital != null) {
                    hospitalPersonInfo.setHospitalId(hospital.getId());
                }
                hospitalPersonInfo.setUpdateBy(body.getPersonCode());
                hospitalPersonInfoService.updateByPersonCode(hospitalPersonInfo);
//            }
        }
        return R.ok();
    }

    @ApiOperation(value = "获取居民消息推送列表", notes = "山东通APP消息通知模块中的消息提醒用")
    @GetMapping("/getNotice/{identity}")
    public TableDataInfo noticDel(@PathVariable String identity) {
        // 获取注册信息
        PatientInfoVO patientInfo = patientInfoService.selectPatientInfoByCardNo(identity);
        if (patientInfo == null) {
            return null;
        }
        MessageSearchDto query = new MessageSearchDto();
        query.setRecipientId(patientInfo.getId());
        query.setMessageCategory("4");
        startPage();
        return getDataTable(chatRecordMapper.selectMegList(query));
    }

    @ApiOperation(value = "获取居民消息推送最后一次记录", notes = "山东通APP消息通知模块中的消息提醒用")
    @GetMapping("/getLastNotice/{identity}")
    public R<ChatRecord> getLastNotice(@PathVariable String identity) {
        PatientInfoVO patientInfo = patientInfoService.selectPatientInfoByCardNo(identity);
        if (patientInfo == null) {
            return null;
        }
        MessageSearchDto query = new MessageSearchDto();
        query.setRecipientId(patientInfo.getId());
        query.setMessageCategory("4");
        return R.ok(chatRecordMapper.getLast(query));
    }
}
