package cn.hnthyy.thmz.controller;

import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.entity.his.Employee;
import cn.hnthyy.thmz.entity.his.ZdChequeType;
import cn.hnthyy.thmz.entity.his.ZyDepositFile;
import cn.hnthyy.thmz.entity.his.ZyLedgerFile;
import cn.hnthyy.thmz.service.his.EmployeeService;
import cn.hnthyy.thmz.service.his.ZdChequeTypeService;
import cn.hnthyy.thmz.service.his.ZyDepositFileService;
import cn.hnthyy.thmz.service.his.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;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class ZyDepositFileController {
    @Autowired
    private ZyDepositFileService zyDepositFileService;
    @Autowired
    private zyLedgerFileService zyLedgerFileService;
    @Autowired
    private ZdChequeTypeService zdChequeTypeService;
    @Autowired
    private EmployeeService employeeService;

    /**
     * 根据住院号和次数查询缴费记录
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getZyDepositFileBYInpatientNoAndTimes", method = {RequestMethod.POST})
    public Map<String, Object> getZyDepositFileBYInpatientNoAndTimes(@RequestBody ZyDepositFile zyDepositFile) {
        log.info("根据住院号和次数查询缴费记录");
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (zyDepositFile == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(zyDepositFile.getInpatientNo())) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人住院号不能为空");

                return resultMap;
            }
            if (zyDepositFile.getAdmissTimes() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人住院次数不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据住院号和次数查询缴费记录成功");
            List<ZyDepositFile> zyDepositFiles = zyDepositFileService.queryZyDepositFileBYInpatientNoAndTimes(zyDepositFile);
            if(zyDepositFiles!=null && zyDepositFiles.size()>0){
                List<ZdChequeType> zdChequeTypes=zdChequeTypeService.queryAllZdChequeTypeForZy();
                Map<String,String> zdChequeTypeMap=zdChequeTypes.stream().collect(Collectors.toMap(ZdChequeType::getCode,ZdChequeType::getName));
                List<ZyLedgerFile> zyLedgerFiles = zyLedgerFileService.queryZyLedgerFileByInpatientNoAndTimes(zyDepositFile.getInpatientNo(), zyDepositFile.getAdmissTimes().intValue());
                Map<Integer, ZyLedgerFile> zyLedgerFileMap = new HashMap<>();
                if (zyLedgerFiles != null && zyLedgerFiles.size() > 0) {
                    for (ZyLedgerFile zyLedgerFile : zyLedgerFiles) {
                        zyLedgerFileMap.put(zyLedgerFile.getLedgerSn(), zyLedgerFile);
                    }
                }
                for (ZyDepositFile zdf:zyDepositFiles){
                    if(StringUtils.isNotBlank(zdf.getStatus())){
                        String status = zdf.getStatus().trim();
                        ZyLedgerFile zlf = zyLedgerFileMap.get(zdf.getLedgerSn());
                        if("1".equals(status)){
                            zdf.setStatusName("预交");
                        }else if("2".equals(status)){
                            if(zlf==null || zlf.getAccountDate()==null || zlf.getAccountDate().after(zdf.getDepoDate())){
                                zdf.setStatusName("直退");
                            }else {
                                zdf.setStatusName("结算退款");
                            }
                        }else if("4".equals(status)){
                            zdf.setStatusName("结算补交");
                        }else{
                            zdf.setStatusName("未知事物");
                        }
                    }
                    zdf.setDepoTypeName(zdChequeTypeMap.get(zdf.getDepoType()));
                    Employee opEmployee = employeeService.queryByUserCode(zdf.getOpIdCode());
                    if (opEmployee != null) {
                        zdf.setOpIdName(opEmployee.getEmployeeName());
                    }
                }
            }
            resultMap.put("data", zyDepositFiles);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "根据住院号和次数查询缴费记录出错，请联系管理员");
            log.error("根据住院号和次数查询缴费记录异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }

}
