package cn.hnthyy.thmz.service.impl.his;

import cn.hnthyy.thmz.entity.his.Employee;
import cn.hnthyy.thmz.entity.his.MzyRequest;
import cn.hnthyy.thmz.entity.his.MzyZdChargeType;
import cn.hnthyy.thmz.mapper.his.EmployeeMapper;
import cn.hnthyy.thmz.mapper.his.MzyRequestMapper;
import cn.hnthyy.thmz.mapper.his.MzyZdChargeTypeMapper;
import cn.hnthyy.thmz.service.his.MzyRequestService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MzyRequestServiceImpl implements MzyRequestService {
    @SuppressWarnings("all")
    @Autowired
    private MzyRequestMapper mzyRequestMapper;
    @SuppressWarnings("all")
    @Autowired
    private EmployeeMapper employeeMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzyZdChargeTypeMapper mzyZdChargeTypeMapper;

    @Override
    public List<MzyRequest> queryMzyRequestByRequestDay(String requestDay) {
        return mzyRequestMapper.selectMzyRequestByRequestDay(requestDay);
    }

    @Override
    public List<String> queryUnitCodeByRequestDay(String requestDay, String ampm) {
        return mzyRequestMapper.selectUnitCodeByRequestDay(requestDay, ampm);
    }

    @Override
    public List<MzyRequest> queryDoctorCodeByRequestDay(String requestDay, String unitCode, String ampm, String chargeType) {
        List<MzyRequest> mzyRequests = mzyRequestMapper.selectDoctorCodeByRequestDay(requestDay, unitCode, ampm, chargeType);
        mzyRequests.forEach(mzyRequest -> {
            if (mzyRequest != null && StringUtils.isNotBlank(mzyRequest.getDoctorCode()) && mzyRequest.getDoctorCode().matches("[\u4E00-\u9FA5]+")) {
                Employee employee = employeeMapper.selectByUserName(mzyRequest.getDoctorCode());
                if (employee == null) {
                    mzyRequest.setDoctorCode(null);
                } else {
                    mzyRequest.setDoctorCode(employee.getEmployeeCode());
                }
            }
        });
        return mzyRequests;
    }

    @Override
    public List<MzyZdChargeType> queryChargeTypeByRequestDay(String requestDay, String unitCode, String ampm) {
        List<String> chargeTypes = mzyRequestMapper.selectChargeTypeByRequestDay(requestDay, unitCode, ampm);
        if (chargeTypes == null || chargeTypes.size() == 0) {
            return null;
        }
        List<MzyZdChargeType> list = new ArrayList<>();
        chargeTypes.forEach(ct -> {
            if (StringUtils.isNotBlank(ct)) {
                MzyZdChargeType mzct = mzyZdChargeTypeMapper.selectByCode(ct);
                if (mzct != null) {
                    list.add(mzct);
                }
            }
        });
        return list;
    }
}
