package com.fs.his.service.impl;

import java.util.List;

import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsCity;
import com.fs.his.domain.FsMedicatedFood;
import com.fs.his.mapper.FsCityMapper;
import com.fs.his.mapper.FsHospitalMapper;
import com.fs.his.param.FsHospitalListUParam;
import com.fs.his.vo.FsHospitalListUVO;
import com.fs.his.vo.OptionsVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fs.his.domain.FsHospital;
import com.fs.his.service.IFsHospitalService;

/**
 * 医院Service业务层处理
 *
 * @author fs
 * @date 2023-06-01
 */
@Service
public class FsHospitalServiceImpl implements IFsHospitalService
{
    @Autowired
    private FsHospitalMapper fsHospitalMapper;
    @Autowired
    private FsCityMapper fsCityMapper;
    /**
     * 查询医院
     *
     * @param hospitalId 医院主键
     * @return 医院
     */
    @Override
    public FsHospital selectFsHospitalByHospitalId(Long hospitalId)
    {
        return fsHospitalMapper.selectFsHospitalByHospitalId(hospitalId);
    }

    /**
     * 查询医院列表
     *
     * @param fsHospital 医院
     * @return 医院
     */
    @Override
    public List<FsHospital> selectFsHospitalList(FsHospital fsHospital)
    {
        return fsHospitalMapper.selectFsHospitalList(fsHospital);
    }

    /**
     * 新增医院
     *
     * @param fsHospital 医院
     * @return 结果
     */
    @Override
    public int insertFsHospital(FsHospital fsHospital)
    {

        return fsHospitalMapper.insertFsHospital(fsHospital);
    }

    /**
     * 修改医院
     *
     * @param fsHospital 医院
     * @return 结果
     */
    @Override
    public int updateFsHospital(FsHospital fsHospital)
    {

        return fsHospitalMapper.updateFsHospital(fsHospital);
    }

//    private void addcity(FsHospital fsHospital) {
//        if (fsHospital.getCityIds()!=null&&fsHospital.getCityIds()!=""){
//            String cityIds = fsHospital.getCityIds();
//            if(cityIds.indexOf(",")!=-1){
//                String[] split = cityIds.split(",");
//                String fs1 = fsCityMapper.selectFsCityone(split[0]);
//                String fs2 = fsCityMapper.selectFsCityone(split[1]);
//                String fs3 = fsCityMapper.selectFsCityone(split[2]);
//                fsHospital.setProvince(fs1);
//                fsHospital.setCity(fs2);
//                fsHospital.setDistrict(fs3);
//                fsHospital.setCityIds(fs1+fs2+fs3);
//            }
//        }
//    }

    /**
     * 批量删除医院
     *
     * @param hospitalIds 需要删除的医院主键
     * @return 结果
     */
    @Override
    public int deleteFsHospitalByHospitalIds(Long[] hospitalIds)
    {
        return fsHospitalMapper.deleteFsHospitalByHospitalIds(hospitalIds);
    }

    /**
     * 删除医院信息
     *
     * @param hospitalId 医院主键
     * @return 结果
     */
    @Override
    public int deleteFsHospitalByHospitalId(Long hospitalId)
    {
        return fsHospitalMapper.deleteFsHospitalByHospitalId(hospitalId);
    }

    @Override
    public List<FsHospitalListUVO> selectFsHospitalListUVO(FsHospitalListUParam param) {
        return fsHospitalMapper.selectFsHospitalListUVO(param);
    }

    @Override
    public List<OptionsVO> selectAllFsHospitalList() {
        return fsHospitalMapper.selectAllFsHospitalList();
    }

    @Override
    public String importData(List<FsHospital> list) {
        if (com.fs.common.utils.StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FsHospital vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsHospitalMapper.insertFsHospital(vo);

                successNum++;
                successMsg.append("<br/>" + successNum + "、名称 " + vo.getHospitalName() + " 导入成功");

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + vo.getHospitalName()  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
