package com.xinelu.manage.controller.screeningrecord;

import com.alibaba.fastjson2.JSONObject;
import com.xinelu.common.annotation.RepeatSubmit;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.domain.SelectVo;
import com.xinelu.common.core.domain.TimelineVo;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.domain.screeningrecord.ScreeningRecord;
import com.xinelu.manage.dto.screeningrecord.ScreeningRecordDTO;
import com.xinelu.manage.dto.screeningrecord.ScreeningRecordSaveDTO;
import com.xinelu.manage.service.hospitalpersoninfo.IHospitalPersonInfoService;
import com.xinelu.manage.service.messagepush.MessagePushService;
import com.xinelu.manage.service.screeningrecord.IScreeningRecordService;
import com.xinelu.manage.vo.screeningrecord.ScreeningRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author mengkuiliang
 * @Description 筛查预约控制器
 * @Date 2023-01-29 13:57
 * @Param
 * @return
 **/
@RestController
@RequestMapping("/system/screening/record")
@Api(tags = "筛查预约控制器")
public class ScreeningRecordController extends BaseController {

    @Resource
    private IScreeningRecordService screeningRecordService;

	@Resource
	private IHospitalPersonInfoService personInfoService;

	@Resource
	private MessagePushService messagePushService;

	@GetMapping("/screeningList")
	@ApiOperation(value = "居民预约筛查列表")
	public TableDataInfo screeningList(ScreeningRecordDTO query) throws Exception {
		if (!getLoginUser().getUser().isAdmin()) {
			HospitalPersonInfo personInfo = personInfoService.selectHospitalPersonInfoById(getLoginUser().getUser().getHospitalPersonId());
			if (personInfo == null) {
				throw new Exception("该账号未绑定医生账号");
			}
			query.setHospitalId(personInfo.getHospitalId());
		}
		startPage();
		try {
			return getDataTable(screeningRecordService.screeningList(query));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

    @GetMapping("/list")
    @ApiOperation(value = "获取列表")
    public List<ScreeningRecordVo> list(ScreeningRecordDTO query) {
        return screeningRecordService.list(query);
    }

    @GetMapping("/detail/{screeningId}")
    @ApiOperation(value = "获取预约详情")
    public R<ScreeningRecordVo> detail(@PathVariable String screeningId) {
        return R.ok(screeningRecordService.detail(screeningId));
    }

    @GetMapping("/getScreening/{patientId}")
    @ApiOperation(value = "获取当前预约信息")
    public R<ScreeningRecordVo> getScreeningByPatientId(@PathVariable String patientId) {
        return R.ok(screeningRecordService.getScreeningByPatientId(patientId));
    }

    @GetMapping("/last/{patientId}")
    @ApiOperation(value = "获取最新一次筛查结果")
    public R<ScreeningRecordVo> last(@PathVariable String patientId) {
        return R.ok(screeningRecordService.last(patientId,null));
    }

	@GetMapping("/timelineList/{patientId}")
	@ApiOperation(value = "根据居民业务主键查询筛查记录时间轴", response = TimelineVo.class, notes = "根据居民业务主键查询筛查记录时间轴", httpMethod = "GET")
	@ApiImplicitParam(name = "patientId", value = "居民业务主键", required = true, dataTypeClass = String.class)
	public R<List<SelectVo>> TimelineList(@PathVariable("patientId") String patientId) {
		return R.ok(screeningRecordService.getTimelineList(patientId));
	}

	@GetMapping("/receive/{screeningId}")
	@ApiOperation(value = "登记")
	public R<String> receive(@PathVariable("screeningId") String screeningId) {
		if(StringUtils.isBlank(screeningId)) {
			return R.fail("请选择预约信息");
		}
		try {
			String registrationCode = screeningRecordService.receive(screeningId);
			return R.ok("预约已登记成功，条码:" + registrationCode + " 请及时进行筛查。");
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改")
	public R<String> update(@RequestBody ScreeningRecord body) {
		// 判断是否已登记
		ScreeningRecordVo screeningRecordVo = screeningRecordService.detail(body.getScreeningId());
		if (StringUtils.equals("1", screeningRecordVo.getScreeningStatus())) {
			return R.fail("该预约未登记，请先登记！");
		}
		JSONObject result = screeningRecordService.update(body);
		if(result != null && result.containsKey("openid")) {
			messagePushService.fdApprovePush(result);
			return R.ok();
		}
		return R.fail();
	}

	@PostMapping("/pushScreening")
	@ApiOperation(value = "推送筛查项目")
	@RepeatSubmit(interval = 2000, message = "请求过于频繁")
	public R<String> pushScreening(@RequestBody ScreeningRecordSaveDTO body) {
		try {
			if (StringUtils.isBlank(body.getPatientId())) {
				return R.fail("请选择居民！");
			}
			if (CollectionUtils.isEmpty(body.getProjectList())) {
				return R.fail("请选择筛查项目！");
			}
			int flag = screeningRecordService.pushScreening(body);
			return R.ok(flag > 0 ? "保存成功": "保存失败");
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}


}
