package com.fs.app.controller;


import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.fs.app.annotation.Login;
import com.fs.common.core.domain.R;
import com.fs.common.utils.ParseUtils;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.*;
import com.fs.his.dto.FsInquiryOrderPatientDTO;
import com.fs.his.param.*;
import com.fs.his.service.*;
import com.fs.his.vo.FsDoctorPrescribeListDVO;
import com.fs.his.vo.FsPrescribeListDVO;
import com.fs.his.vo.FsPrescribeListUVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Synchronized;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api("处方接口")
@RestController
@RequestMapping(value="/app/prescribe")
public class PrescribeController extends  AppBaseController {

    @Autowired
    private IFsPrescribeService prescribeService;


    @Autowired
    private IFsPrescribeDrugService prescribeDrugService;
    @Autowired
    private IFsDoctorService doctorService;


    @Login
    @GetMapping("/getPrescribeList")
    public R getPrescribeList(FsPrescribeListUParam param)
    {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        param.setUserId(Long.parseLong(getUserId()));
        List<FsPrescribeListUVO> list=prescribeService.selectFsPrescribeListUVO(param);
        PageInfo<FsPrescribeListUVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }


    @Login
    @GetMapping("/getPrescribeById")
    public R getPrescribeById(@RequestParam("prescribeId")Long prescribeId)
    {
        FsPrescribe prescribe=prescribeService.selectFsPrescribeByPrescribeId(prescribeId);
//        if(!prescribe.getUserId().equals(Long.parseLong(getUserId()))){
//            return R.error("非法操作");
//        }

        if(prescribe!=null&& StringUtils.isNotEmpty(prescribe.getPatientTel())){
            prescribe.setPatientTel(ParseUtils.parsePhone(prescribe.getPatientTel()));
        }
        FsPrescribeDrug map=new FsPrescribeDrug();
        map.setPrescribeId(prescribeId);
        List<FsPrescribeDrug> drugs=prescribeDrugService.selectFsPrescribeDrugList(map);
        FsDoctor doctor=doctorService.selectFsDoctorByDoctorId(prescribe.getDoctorId());
        Map<String,Object> maps=new HashMap<>();
        maps.put("prescribe",prescribe);
        maps.put("drugs",drugs);
        maps.put("doctor",doctor);
        return R.ok().put("data",maps);
    }


}
