package cn.hnthyy.thmz.controller.zy;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.mz.Employee;
import cn.hnthyy.thmz.entity.his.zy.ZyLedgerFile;
import cn.hnthyy.thmz.service.his.mz.EmployeeService;
import cn.hnthyy.thmz.service.his.yz.YzActOrderService;
import cn.hnthyy.thmz.service.his.zy.ZyLedgerFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class ZyLederFileController {

    @Autowired
    private ZyLedgerFileService zyLedgerFileService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private YzActOrderService yzActOrderService;

    /**
     * 查询病人当次住院的账页信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getZyLedgerFileByInpatientNoAndTimes", method = {RequestMethod.GET})
    public Map<String, Object> getZyLedgerFileByInpatientNoAndTimes(@RequestParam("inpatientNo") String inpatientNo,@RequestParam("admissTimes") Integer admissTimes) {
        log.info("查询病人当次住院的账页信息");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(inpatientNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "住院号不能为空");
                return resultMap;
            }
            if(admissTimes==null){
                resultMap.put("code", -1);
                resultMap.put("message", "住院次数不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询病人当次住院的账页信息成功");
            List<ZyLedgerFile> zyLedgerFiles= zyLedgerFileService.queryZyLedgerFileByInpatientNoAndTimes(inpatientNo, admissTimes);
            for (ZyLedgerFile zyLedgerFile:zyLedgerFiles){
                Employee employee= employeeService.queryByUserCode(zyLedgerFile.getOpIdCode());
                if(employee!=null){
                    zyLedgerFile.setOpIdCode(employee.getEmployeeName());
                }
            }
            resultMap.put("data", zyLedgerFiles);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询病人当次住院的账页信息出错，请联系管理员");
            log.error("查询病人当次住院的账页信息异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }





    /**
     * 查看病人的出纳信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getTellerInfo", method = {RequestMethod.GET})
    public Map<String, Object> getTellerInfo(@RequestParam("inpatientNo") String inpatientNo,@RequestParam("admissTimes") Integer admissTimes) {
        log.info("查看病人的出纳信息");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(inpatientNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "住院号不能为空");
                return resultMap;
            }
            if(admissTimes==null){
                resultMap.put("code", -1);
                resultMap.put("message", "住院次数不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查看病人的出纳信息成功");
            resultMap.put("data", zyLedgerFileService.queryTellerInfo(inpatientNo, admissTimes));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查看病人的出纳信息出错，请联系管理员");
            log.error("查看病人的出纳信息异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }




    /**
     * 查看病人的结算信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/settlementInfo", method = {RequestMethod.GET})
    public Map<String, Object> settlementInfo(@RequestParam("inpatientNo") String inpatientNo,@RequestParam("admissTimes") Integer admissTimes) {
        log.info("查看病人的结算信息");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(inpatientNo)) {
                resultMap.put("code", -1);
                resultMap.put("message", "住院号不能为空");
                return resultMap;
            }
            if(admissTimes==null){
                resultMap.put("code", -1);
                resultMap.put("message", "住院次数不能为空");
                return resultMap;
            }
            String yzDate= yzActOrderService.queryLeaveOrderDate(inpatientNo,admissTimes);
            if(StringUtils.isBlank(yzDate)){
                resultMap.put("code", -1);
                resultMap.put("message", "当前病人没有出院医嘱，不可进行出院处理！");
                return resultMap;
            }
            Integer ledgerSn= zyLedgerFileService.queryCurrentLedgerSn(inpatientNo, admissTimes);
            if(ledgerSn!=null){
                resultMap.put("code", -2);
                resultMap.put("message", "病人有尚未结算的费用，确定出院吗？");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查看病人的结算信息成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查看病人的结算信息出错，请联系管理员");
            log.error("查看病人的结算信息异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }



}
