package thyyxxk.webserver.service.medicalinsurance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.sidicts.SiFunction;
import thyyxxk.webserver.dao.his.medicalinsurance.SiLogDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.log.FetchLog;
import thyyxxk.webserver.entity.medicalinsurance.log.SiLog;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SiLogService {
    private final SiLogDao dao;
    private final UserCache userCache;

    @Autowired
    public SiLogService(SiLogDao dao, UserCache userCache) {
        this.dao = dao;
        this.userCache = userCache;
    }

    public ResultVo<Map<String, Object>> selectSiLogs(FetchLog ftchlg) {
        IPage<SiLog> iPage = new Page<>(ftchlg.getCurrentPage(), ftchlg.getPageSize());
        iPage = dao.selectSiLogs(iPage, ftchlg);
        if (iPage.getTotal() == 0) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", iPage.getTotal());
        List<SiLog> list = iPage.getRecords();
        for (SiLog siLog : list) {
            siLog.setInfname(SiFunction.getName(siLog.getInfno()));
            siLog.setOpterName(userCache.getEmployeeName(siLog.getOpter()));
        }
        map.put("list", list);
        if (null != ftchlg.getNeedCalTips() && ftchlg.getNeedCalTips()) {
            Integer successCount = dao.selectSuccessSiLogCount(ftchlg);
            if (null == successCount) {
                successCount = 0;
            }
            NumberFormat numberFormat = NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);
            String percentage = numberFormat.format((float) successCount / (float) iPage.getTotal() * 100) + "%";
            map.put("tipMessage", "条件范围内接口调用共 " + iPage.getTotal() + " 次，成功 " + successCount + " 次，成功率：" + percentage);
        }
        return ResultVoUtil.success(map);
    }

    public ResultVo<SiLog> selectSiLogBody(String msgid) {
        return ResultVoUtil.success(dao.selectById(msgid));
    }

    public void delAndSave(SiLog siLog) {
        AssertUtil.isnotBlank(siLog.getPatNo(),"病案号不能为空");
        AssertUtil.isnotBlank(siLog.getTimes(),"住院次数不能为空");
        AssertUtil.isnotBlank(siLog.getLedgerSn(),"账页号不能为空");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("pat_no",siLog.getPatNo());
        queryWrapper.eq("times",siLog.getTimes());
        queryWrapper.eq("ledger_sn",siLog.getLedgerSn());
        queryWrapper.eq("infno",siLog.getInfno());
        dao.delete(queryWrapper);
        dao.insert(siLog);
    }
}
