package thyyxxk.webserver.service.medicalinsurance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.medicalinsurance.DismissRulesDao;
import thyyxxk.webserver.entity.medicalinsurance.manage.dismissrules.MedinsDismissRules;
import thyyxxk.webserver.entity.medicalinsurance.manage.dismissrules.RuleElement;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.SnowFlakeId;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

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

@Service
public class DismissRulesService {
    private final DismissRulesDao dao;
    private final UserCache userCache;

    @Autowired
    public DismissRulesService(DismissRulesDao dao, UserCache userCache) {
        this.dao = dao;
        this.userCache = userCache;
    }

    public List<MedinsDismissRules> getAllRules() {
        return dao.selectList(null);
    }

    public Map<String, List<RuleElement>> getBaseDict() {
        Map<String, List<RuleElement>> map = new HashMap<>();
        map.put("operatorSymbols", dao.selectOperatorSymbols());
        map.put("attributes", dao.selectAttributeOrComparison("ATTRIBUTE"));
        map.put("comparisons", dao.selectAttributeOrComparison("COMPARISON"));
        return map;
    }

    public int saveEditResult(MedinsDismissRules request) {
        if (StringUtil.isBlank(request.getId())) {
            request.setId(SnowFlakeId.instance().nextUUID());
            request.setCreator(TokenUtil.getInstance().getTokenUserId());
            request.setCreatorName(userCache.getEmployeeName(request.getCreator()));
            return dao.insert(request);
        }
        return dao.updateById(request);
    }

    public int deleteRule(String id) {
        return dao.deleteById(id);
    }
}
